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
14 from PLC.API import PLCAPI
15 from PLC.Method import *
16 from PLC.Auth import Auth
26 Casts Python values to PHP values.
31 elif isinstance(value, (list, tuple, set)):
32 return "array(%s)" % ", ".join([php_cast(v) for v in value])
33 elif isinstance(value, dict):
34 items = ["%s => %s" % (php_cast(k), php_cast(v)) for (k, v) in value.items()]
35 return "array(%s)" % ", ".join(items)
36 elif isinstance(value, (int, long, bool, float)):
39 unicode_repr = repr(unicode(value))
40 # Truncate the leading 'u' prefix
41 return unicode_repr[1:]
47 for method in api.methods:
48 # Skip system. methods
49 if "system." in method:
52 function = api.callable(method)
54 # Commented documentation
55 lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
56 print "\n".join(lines)
59 # Function declaration
60 print "function " + function.name,
62 # PHP function arguments
64 (min_args, max_args, defaults) = function.args()
65 parameters = zip(max_args, function.accepts, defaults)
67 for name, expected, default in parameters:
68 # Skip auth structures (added automatically)
69 if isinstance(expected, Auth) or \
70 (isinstance(expected, Mixed) and \
71 filter(lambda sub: isinstance(sub, Auth), expected)):
77 # Set optional parameters to their defaults
78 if name not in min_args:
79 arg += " = " + php_cast(default)
83 # Write function declaration
84 print "(" + ", ".join(args) + ")"
89 # API function arguments
91 for name, expected, default in parameters:
92 # Automatically added auth structures
93 if isinstance(expected, Auth) or \
94 (isinstance(expected, Mixed) and \
95 filter(lambda sub: isinstance(sub, Auth), expected)):
96 print " $args[] = $this->auth;"
100 if name not in min_args:
101 print "if (func_num_args() > %d)" % i,
102 print "$args[] = $%s;" % name
107 print " return $this->call('%s', $args);" % method