Hook up Unit.run() to api
[osm/N2VC.git] / juju / client / facade.py
index 9fc3160..41166c0 100644 (file)
@@ -232,8 +232,8 @@ class {}(Type):
                     else:
                         source.append("{}self.{} = {}".format(INDENT * 2, arg_name, arg_name))
                 elif type(arg_type) is typing.TypeVar:
-                    source.append("{}self.{} = {}.from_json({})".format(
-                        INDENT * 2, arg_name, arg_type_name, arg_name))
+                    source.append("{}self.{} = {}.from_json({}) if {} else None".format(
+                        INDENT * 2, arg_name, arg_type_name, arg_name, arg_name))
                 else:
                     source.append("{}self.{} = {}".format(INDENT * 2, arg_name, arg_name))
 
@@ -395,7 +395,7 @@ def buildFacade(schema):
                                           version=schema.version,
                                           schema=schema))
     source = """
-class {name}(Type):
+class {name}Facade(Type):
     name = '{name}'
     version = {version}
     schema = {schema}
@@ -428,7 +428,11 @@ class Type:
         for k, v in (data or {}).items():
             d[cls._toPy.get(k, k)] = v
 
-        return cls(**d)
+        try:
+            return cls(**d)
+        except TypeError:
+            print(cls)
+            raise
 
     def serialize(self):
         d = {}
@@ -526,7 +530,10 @@ class Schema(dict):
                 add((name, self.buildArray(pprop, d + 1)))
             else:
                 add((name, Mapping[str, SCHEMA_TO_PYTHON[ppkind]]))
-            #print("{}{}".format(d * "   ", struct))
+
+        if not struct and node.get('additionalProperties', False):
+            add((name, Mapping[str, SCHEMA_TO_PYTHON['object']]))
+
         return struct
 
     def buildArray(self, obj, d=0):
@@ -558,9 +565,13 @@ def generate_facacdes(options):
     global classes
     schemas = json.loads(Path(options.schema).read_text("utf-8"))
     capture = codegen.CodeWriter()
-    capture.write("""
-from juju.client.facade import Type, ReturnMapping
-                  """)
+    capture.write(textwrap.dedent("""\
+        # DO NOT CHANGE THIS FILE! This file is auto-generated by facade.py.
+        # Changes will be overwritten/lost when the file is regenerated.
+
+        from juju.client.facade import Type, ReturnMapping
+
+    """))
     schemas = [Schema(s) for s in schemas]
 
     for schema in schemas: