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.1 2006/10/25 20:32:44 mlhuang Exp $
14 from PLC.API import PLCAPI
15 from PLC.Method import *
16 from PLC.Auth import Auth
22 for method in api.methods:
23 # Skip system. methods
24 if "system." in method:
27 function = api.callable(method)
29 # Commented documentation
30 lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
31 print "\n".join(lines)
34 # Function declaration
35 print "function " + function.name,
37 # PHP function arguments
39 (min_args, max_args, defaults) = function.args()
40 parameters = zip(max_args, function.accepts, defaults)
42 for name, expected, default in parameters:
43 # Skip auth structures (added automatically)
44 if isinstance(expected, Auth) or \
45 (isinstance(expected, Mixed) and \
46 filter(lambda sub: isinstance(sub, Auth), expected)):
52 # Set optional parameters to their defaults
53 if name not in min_args:
57 elif isinstance(default, (list, tuple, set)):
58 arg += "array(%s)" % ", ".join(default)
59 elif isinstance(default, dict):
60 items = ["%s => %s" % (key, value) for (key, value) in default.items()]
61 arg += "array(%s)" % ", ".join(items)
65 # Write function declaration
66 print "(" + ", ".join(args) + ")"
71 # API function arguments
72 for name, expected, default in parameters:
73 # Automatically added auth structures
74 if isinstance(expected, Auth) or \
75 (isinstance(expected, Mixed) and \
76 filter(lambda sub: isinstance(sub, Auth), expected)):
77 print " $args[] = $this->auth;"
80 print " $args[] = $%s;" % name
83 print " return $this->call('%s', $args);" % method