0b543701f9a85da5f923e8e6d326fca09d735c5b
[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: gen_php_api.py,v 1.13 2006/03/23 04:29:08 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 # Class functions
19 api = PLCAPI(None)
20
21 api.methods.sort()
22 for method in api.methods:
23     # Skip system. methods
24     if "system." in method:
25         continue
26
27     function = api.callable(method)
28
29     # Commented documentation
30     lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
31     print "\n".join(lines)
32     print
33
34     # Function declaration
35     print "function " + function.name,
36
37     # PHP function arguments
38     args = []
39     (min_args, max_args, defaults) = function.args()
40     parameters = zip(max_args, function.accepts, defaults)
41
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)):
47             continue
48
49         # Declare parameter
50         arg = "$" + name
51
52         # Set optional parameters to special value NULL
53         if name not in min_args:
54             arg += " = NULL"
55
56         args.append(arg)
57
58     # Write function declaration
59     print "(" + ", ".join(args) + ")"
60
61     # Begin function body
62     print "{"
63
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;"
71             continue
72
73         if name in min_args:
74             print "  $args[] = $%s;" % name
75         else:
76             print "  if ($%s !== NULL) { $args[] = $%s; }" % (name, name)
77
78     # Call API function
79     print "  return $this->call('%s', $args);" % method
80
81     # End function body
82     print "}"
83     print