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