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
8 # $Id: methods.py,v 1.3 2006/11/21 20:00:53 mlhuang Exp $
14 from PLC.API import PLCAPI
15 from PLC.Method import *
16 from PLC.Auth import Auth
20 Casts Python values to PHP values.
25 elif isinstance(value, (list, tuple, set)):
26 return "array(%s)" % ", ".join([php_cast(v) for v in value])
27 elif isinstance(value, dict):
28 items = ["%s => %s" % (php_cast(k), php_cast(v)) for (k, v) in value.items()]
29 return "array(%s)" % ", ".join(items)
30 elif isinstance(value, (int, long, bool, float)):
33 unicode_repr = repr(unicode(value))
34 # Truncate the leading 'u' prefix
35 return unicode_repr[1:]
41 for method in api.methods:
42 # Skip system. methods
43 if "system." in method:
46 function = api.callable(method)
48 # Commented documentation
49 lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
50 print "\n".join(lines)
53 # Function declaration
54 print "function " + function.name,
56 # PHP function arguments
58 (min_args, max_args, defaults) = function.args()
59 parameters = zip(max_args, function.accepts, defaults)
61 for name, expected, default in parameters:
62 # Skip auth structures (added automatically)
63 if isinstance(expected, Auth) or \
64 (isinstance(expected, Mixed) and \
65 filter(lambda sub: isinstance(sub, Auth), expected)):
71 # Set optional parameters to their defaults
72 if name not in min_args:
73 arg += " = " + php_cast(default)
77 # Write function declaration
78 print "(" + ", ".join(args) + ")"
83 # API function arguments
85 for name, expected, default in parameters:
86 # Automatically added auth structures
87 if isinstance(expected, Auth) or \
88 (isinstance(expected, Mixed) and \
89 filter(lambda sub: isinstance(sub, Auth), expected)):
90 print " $args[] = $this->auth;"
94 if name not in min_args:
95 print "if (func_num_args() > %d)" % i,
96 print "$args[] = $%s;" % name
101 print " return $this->call('%s', $args);" % method