Fix version output when missing.
[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$
9 # $URL$
10 #
11
12 import os, sys
13 import time
14
15 from PLC.API import PLCAPI
16 from PLC.Method import *
17 from PLC.Auth import Auth
18
19 try:
20     set
21 except NameError:
22     from sets import Set
23     set = Set
24
25 def php_cast(value):
26     """
27     Casts Python values to PHP values.
28     """
29     
30     if value is None:
31         return "NULL"
32     elif isinstance(value, (list, tuple, set)):
33         return "array(%s)" % ", ".join([php_cast(v) for v in value])
34     elif isinstance(value, dict):
35         items = ["%s => %s" % (php_cast(k), php_cast(v)) for (k, v) in value.items()]
36         return "array(%s)" % ", ".join(items)
37     elif isinstance(value, (int, long, bool, float)):
38         return str(value)
39     else:
40         unicode_repr = repr(unicode(value))
41         # Truncate the leading 'u' prefix
42         return unicode_repr[1:]
43
44 # Class functions
45 api = PLCAPI(None)
46
47 api.all_methods.sort()
48 for method in api.all_methods:
49     # Skip system. methods
50     if "system." in method:
51         continue
52
53     function = api.callable(method)
54
55     # Commented documentation
56     lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
57     print "\n".join(lines)
58     print
59
60     # Function declaration
61     print "function " + function.name,
62
63     # PHP function arguments
64     args = []
65     (min_args, max_args, defaults) = function.args()
66     parameters = zip(max_args, function.accepts, defaults)
67
68     for name, expected, default in parameters:
69         # Skip auth structures (added automatically)
70         if isinstance(expected, Auth) or \
71            (isinstance(expected, Mixed) and \
72             filter(lambda sub: isinstance(sub, Auth), expected)):
73             continue
74
75         # Declare parameter
76         arg = "$" + name
77
78         # Set optional parameters to their defaults
79         if name not in min_args:
80             arg += " = " + php_cast(default)
81
82         args.append(arg)
83
84     # Write function declaration
85     print "(" + ", ".join(args) + ")"
86
87     # Begin function body
88     print "{"
89
90     # API function arguments
91     i = 0
92     for name, expected, default in parameters:
93         # Automatically added auth structures
94         if isinstance(expected, Auth) or \
95            (isinstance(expected, Mixed) and \
96             filter(lambda sub: isinstance(sub, Auth), expected)):
97             print "  $args[] = $this->auth;"
98             continue
99
100         print " ",
101         if name not in min_args:
102             print "if (func_num_args() > %d)" % i, 
103         print "$args[] = $%s;" % name
104
105         i += 1
106
107     # Call API function
108     print "  return $this->call('%s', $args);" % method
109
110     # End function body
111     print "}"
112     print