27761a0578299565fab527811220fce3193d026d
[plcapi.git] / php / methods.py
1 #!/usr/bin/python
2 #
3 # Generates the PLCAPI interface for the website PHP code.
4 #
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2005 The Trustess of Princeton University
7 #
8 # $Id: methods.py,v 1.3 2006/11/21 20:00:53 mlhuang Exp $
9 #
10
11 import os, sys
12 import time
13
14 from PLC.API import PLCAPI
15 from PLC.Method import *
16 from PLC.Auth import Auth
17
18 def php_cast(value):
19     """
20     Casts Python values to PHP values.
21     """
22     
23     if value is None:
24         return "NULL"
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)):
31         return str(value)
32     else:
33         unicode_repr = repr(unicode(value))
34         # Truncate the leading 'u' prefix
35         return unicode_repr[1:]
36
37 # Class functions
38 api = PLCAPI(None)
39
40 api.methods.sort()
41 for method in api.methods:
42     # Skip system. methods
43     if "system." in method:
44         continue
45
46     function = api.callable(method)
47
48     # Commented documentation
49     lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
50     print "\n".join(lines)
51     print
52
53     # Function declaration
54     print "function " + function.name,
55
56     # PHP function arguments
57     args = []
58     (min_args, max_args, defaults) = function.args()
59     parameters = zip(max_args, function.accepts, defaults)
60
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)):
66             continue
67
68         # Declare parameter
69         arg = "$" + name
70
71         # Set optional parameters to their defaults
72         if name not in min_args:
73             arg += " = " + php_cast(default)
74
75         args.append(arg)
76
77     # Write function declaration
78     print "(" + ", ".join(args) + ")"
79
80     # Begin function body
81     print "{"
82
83     # API function arguments
84     i = 0
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;"
91             continue
92
93         print " ",
94         if name not in min_args:
95             print "if (func_num_args() > %d)" % i, 
96         print "$args[] = $%s;" % name
97
98         i += 1
99
100     # Call API function
101     print "  return $this->call('%s', $args);" % method
102
103     # End function body
104     print "}"
105     print