Fix version output when missing.
[plcapi.git] / php / methods.py
index 0b54370..621cc91 100755 (executable)
@@ -5,7 +5,8 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2005 The Trustess of Princeton University
 #
-# $Id: gen_php_api.py,v 1.13 2006/03/23 04:29:08 mlhuang Exp $
+# $Id$
+# $URL$
 #
 
 import os, sys
@@ -15,11 +16,36 @@ from PLC.API import PLCAPI
 from PLC.Method import *
 from PLC.Auth import Auth
 
+try:
+    set
+except NameError:
+    from sets import Set
+    set = Set
+
+def php_cast(value):
+    """
+    Casts Python values to PHP values.
+    """
+    
+    if value is None:
+        return "NULL"
+    elif isinstance(value, (list, tuple, set)):
+        return "array(%s)" % ", ".join([php_cast(v) for v in value])
+    elif isinstance(value, dict):
+        items = ["%s => %s" % (php_cast(k), php_cast(v)) for (k, v) in value.items()]
+        return "array(%s)" % ", ".join(items)
+    elif isinstance(value, (int, long, bool, float)):
+        return str(value)
+    else:
+        unicode_repr = repr(unicode(value))
+        # Truncate the leading 'u' prefix
+        return unicode_repr[1:]
+
 # Class functions
 api = PLCAPI(None)
 
-api.methods.sort()
-for method in api.methods:
+api.all_methods.sort()
+for method in api.all_methods:
     # Skip system. methods
     if "system." in method:
         continue
@@ -49,9 +75,9 @@ for method in api.methods:
         # Declare parameter
         arg = "$" + name
 
-        # Set optional parameters to special value NULL
+        # Set optional parameters to their defaults
         if name not in min_args:
-            arg += " = NULL"
+            arg += " = " + php_cast(default)
 
         args.append(arg)
 
@@ -62,6 +88,7 @@ for method in api.methods:
     print "{"
 
     # API function arguments
+    i = 0
     for name, expected, default in parameters:
         # Automatically added auth structures
         if isinstance(expected, Auth) or \
@@ -70,10 +97,12 @@ for method in api.methods:
             print "  $args[] = $this->auth;"
             continue
 
-        if name in min_args:
-            print "  $args[] = $%s;" % name
-        else:
-            print "  if ($%s !== NULL) { $args[] = $%s; }" % (name, name)
+        print " ",
+        if name not in min_args:
+            print "if (func_num_args() > %d)" % i, 
+        print "$args[] = $%s;" % name
+
+        i += 1
 
     # Call API function
     print "  return $this->call('%s', $args);" % method