X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=juju%2Fclient%2Ffacade.py;h=5ed53202ca968f8f5af2633fa021d32c8062089c;hb=cde1c24a5157d1a7c308d5c2d3436fae86be24fc;hp=048e5c5ad02c4dac6e3b5e7a4620efeed4ece21e;hpb=edf3beda420d3a2c66d6ef8fe9b5e45a76d052b9;p=osm%2FN2VC.git diff --git a/juju/client/facade.py b/juju/client/facade.py index 048e5c5..5ed5320 100644 --- a/juju/client/facade.py +++ b/juju/client/facade.py @@ -124,7 +124,7 @@ class Args(list): self.append((name, rtype)) def do_explode(self, kind): - if kind in basic_types: + if kind in basic_types or type(kind) is typing.TypeVar: return False if not issubclass(kind, (typing.Sequence, typing.Mapping)): @@ -188,9 +188,9 @@ def buildTypes(schema, capture): for kind in sorted((k for k in _types if not isinstance(k, str)), key=lambda x: str(x)): name = _types[kind] - args = Args(kind) if name in classes: continue + args = Args(kind) source = [""" class {}(Type): _toSchema = {} @@ -200,8 +200,9 @@ class {}(Type): {} '''""".format( name, - args.PyToSchemaMapping(), - args.SchemaToPyMapping(), + # pprint these to get stable ordering across regens + pprint.pformat(args.PyToSchemaMapping(), width=999), + pprint.pformat(args.SchemaToPyMapping(), width=999), ", " if args else "", args.as_kwargs(), textwrap.indent(args.get_doc(), INDENT * 2)) @@ -218,14 +219,22 @@ class {}(Type): if arg_type in basic_types: source.append("{}self.{} = {}".format(INDENT * 2, arg_name, arg_name)) elif issubclass(arg_type, typing.Sequence): - value_type = arg_type_name.__parameters__[0] + value_type = ( + arg_type_name.__parameters__[0] + if len(arg_type_name.__parameters__) + else None + ) if type(value_type) is typing.TypeVar: source.append("{}self.{} = [{}.from_json(o) for o in {} or []]".format( INDENT * 2, arg_name, strcast(value_type), arg_name)) else: source.append("{}self.{} = {}".format(INDENT * 2, arg_name, arg_name)) elif issubclass(arg_type, typing.Mapping): - value_type = arg_type_name.__parameters__[1] + value_type = ( + arg_type_name.__parameters__[1] + if len(arg_type_name.__parameters__) > 1 + else None + ) if type(value_type) is typing.TypeVar: source.append("{}self.{} = {{k: {}.from_json(v) for k, v in ({} or dict()).items()}}".format( INDENT * 2, arg_name, strcast(value_type), arg_name)) @@ -308,15 +317,22 @@ def ReturnMapping(cls): reply = await f(*args, **kwargs) if cls is None: return reply - if 'Error' in reply: + if 'error' in reply: cls = classes['Error'] if issubclass(cls, typing.Sequence): result = [] item_cls = cls.__parameters__[0] for item in reply: result.append(item_cls.from_json(item)) + """ + if 'error' in item: + cls = classes['Error'] + else: + cls = item_cls + result.append(cls.from_json(item)) + """ else: - result = cls.from_json(reply['Response']) + result = cls.from_json(reply['response']) return result return wrapper @@ -329,9 +345,9 @@ def makeFunc(cls, name, params, result, async=True): assignments = [] toschema = args.PyToSchemaMapping() for arg in args._get_arg_str(False, False): - assignments.append("{}params[\'{}\'] = {}".format(INDENT, - toschema[arg], - arg)) + assignments.append("{}_params[\'{}\'] = {}".format(INDENT, + toschema[arg], + arg)) assignments = "\n".join(assignments) res = retspec(result) source = """ @@ -343,8 +359,8 @@ def makeFunc(cls, name, params, result, async=True): Returns -> {res} ''' # map input types to rpc msg - params = dict() - msg = dict(Type='{cls.name}', Request='{name}', Version={cls.version}, Params=params) + _params = dict() + msg = dict(type='{cls.name}', request='{name}', version={cls.version}, params=_params) {assignments} reply = {await}self.rpc(msg) return reply @@ -395,7 +411,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 +444,10 @@ 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: + raise def serialize(self): d = {} @@ -526,7 +545,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,12 +580,13 @@ def generate_facacdes(options): global classes schemas = json.loads(Path(options.schema).read_text("utf-8")) capture = codegen.CodeWriter() - capture.write(""" -# DO NOT CHANGE THIS FILE! This file is auto-generated by facade.py. -# Changes will be overwritten/lost when the file is regenerated. + 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 -from juju.client.facade import Type, ReturnMapping - """) + """)) schemas = [Schema(s) for s in schemas] for schema in schemas: