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: gen_php_api.py,v 1.13 2006/03/23 04:29:08 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 special value NULL
53 if name not in min_args:
58 # Write function declaration
59 print "(" + ", ".join(args) + ")"
64 # API function arguments
65 for name, expected, default in parameters:
66 # Automatically added auth structures
67 if isinstance(expected, Auth) or \
68 (isinstance(expected, Mixed) and \
69 filter(lambda sub: isinstance(sub, Auth), expected)):
70 print " $args[] = $this->auth;"
74 print " $args[] = $%s;" % name
76 print " if ($%s !== NULL) { $args[] = $%s; }" % (name, name)
79 print " return $this->call('%s', $args);" % method