3 # Generates the PLCAPI interface for the website PHP code.
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2005 The Trustess of Princeton University
15 from PLC.API import PLCAPI
16 from PLC.Method import *
17 from PLC.Auth import Auth
27 Casts Python values to PHP values.
32 elif isinstance(value, (list, tuple, set)):
33 return "array(%s)" % ", ".join([php_cast(v) for v in value])
34 elif isinstance(value, dict):
35 items = ["%s => %s" % (php_cast(k), php_cast(v)) for (k, v) in value.items()]
36 return "array(%s)" % ", ".join(items)
37 elif isinstance(value, (int, long, bool, float)):
40 unicode_repr = repr(unicode(value))
41 # Truncate the leading 'u' prefix
42 return unicode_repr[1:]
47 api.all_methods.sort()
48 for method in api.all_methods:
49 # Skip system. methods
50 if "system." in method:
53 function = api.callable(method)
55 # Commented documentation
56 lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
57 print "\n".join(lines)
60 # Function declaration
61 print "function " + function.name,
63 # PHP function arguments
65 (min_args, max_args, defaults) = function.args()
66 parameters = zip(max_args, function.accepts, defaults)
68 for name, expected, default in parameters:
69 # Skip auth structures (added automatically)
70 if isinstance(expected, Auth) or \
71 (isinstance(expected, Mixed) and \
72 filter(lambda sub: isinstance(sub, Auth), expected)):
78 # Set optional parameters to their defaults
79 if name not in min_args:
80 arg += " = " + php_cast(default)
84 # Write function declaration
85 print "(" + ", ".join(args) + ")"
90 # API function arguments
92 for name, expected, default in parameters:
93 # Automatically added auth structures
94 if isinstance(expected, Auth) or \
95 (isinstance(expected, Mixed) and \
96 filter(lambda sub: isinstance(sub, Auth), expected)):
97 print " $args[] = $this->auth;"
101 if name not in min_args:
102 print "if (func_num_args() > %d)" % i,
103 print "$args[] = $%s;" % name
108 print " return $this->call('%s', $args);" % method