Chromium Code Reviews| Index: tools/json_schema_compiler/model.py |
| diff --git a/tools/json_schema_compiler/model.py b/tools/json_schema_compiler/model.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a59e76bbd7bcc3609bcf8beafbe4df08cc518082 |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/model.py |
| @@ -0,0 +1,116 @@ |
| +# Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import os.path |
| + |
| +class ModelT(object): |
|
not at google - send to devlin
2012/01/12 06:01:05
consider putting this in a model/ directory, with
calamity
2012/01/16 04:01:06
Any particular reason for doing this?
not at google - send to devlin
2012/01/17 01:59:24
Yes, so that you can have
- a different file for
|
| + """Model of the API.""" |
|
not at google - send to devlin
2012/01/12 06:01:05
also, why is every model object suffixed with T?
calamity
2012/01/12 22:59:20
It was done so I had some sort of naming conventio
|
| + def __init__(self): |
| + self.namespaces = {} |
| + self.types = {} |
| + |
| + def add_namespace(self, json, root_namespace, parent_path, filename_suffix): |
| + """Add a namespace's json to the model.""" |
| + if not json.get('generate'): |
| + return None |
| + namespace = NamespaceT(json, root_namespace, |
| + parent_path, self, filename_suffix) |
| + self.namespaces[namespace.name] = namespace |
| + for tipe in namespace.types.values(): |
| + self.types[tipe.name] = namespace |
| + return namespace |
| + |
| +class NamespaceT(object): |
| + """An API namespace.""" |
| + def __init__(self, json, root_namespace, |
| + parent_path, parent_model, filename_suffix): |
| + self.name = json['namespace'] |
| + self.root_namespace = root_namespace |
| + self.parent_path = parent_path |
| + self.parent_dir, self.parent_filename = os.path.split(parent_path) |
| + self.parent_model = parent_model |
| + self.filename = self.name + filename_suffix |
| + self.type_dependencies = {} |
| + self.types = {} |
| + self.functions = {} |
| + for type_json in json['types']: |
| + tipe = TypeT(type_json) |
| + self.types[tipe.name] = tipe |
| + for function_json in json['functions']: |
| + if not function_json.get('nogenerate'): |
| + function = FunctionT(function_json) |
| + self.functions[function.name] = function |
| + |
| +class TypeT(object): |
| + """A Type defined in the json.""" |
| + def __init__(self, json): |
| + self.name = json['id'] |
| + self.description = json.get('description') |
| + self.properties = {} |
| + for prop_name, prop_json in json['properties'].items(): |
| + self.properties[prop_name] = PropertyT(prop_name, prop_json) |
| + |
| +class CallbackT(object): |
| + """A callback parameter to a Function.""" |
| + def __init__(self, json): |
| + self.param = None |
| + assert len(json['parameters']) <= 1 |
| + for param in json['parameters']: |
| + self.param = PropertyT(param['name'], param) |
| + |
| +class FunctionT(object): |
| + """A Function defined in the API.""" |
| + def __init__(self, json): |
| + self.name = json['name'] |
| + self.params = [] |
| + self.description = json['description'] |
| + self.callback = None |
| + self.type_dependencies = {} |
| + for param in json['parameters']: |
| + if param.get('type') == 'function': |
| + # TODO(calamity): Deal with this |
| + self.callback = CallbackT(param) |
| + continue |
| + self.params.append(PropertyT(param['name'], param)) |
| + if not self.callback: |
| + raise KeyError |
| + |
| +# TODO(calamity): handle Enum/choices |
| +class PropertyT(object): |
| + """A property of a type OR a parameter to a function. |
| + |
| + A polymorphic type, check self.type to determine which |
| + members actually exist. |
| + """ |
| + def __init__(self, name, json): |
| + self.name = name |
| + self.optional = json.get('optional', False) |
| + self.description = json.get('description') |
| + simple_types = ['boolean', 'integer', 'string', 'double'] |
| + # TODO(calamity) maybe check for circular refs? could that be a problem? |
| + if '$ref' in json: |
| + self.json_type = json['$ref'] |
| + self.type = PropertyType.REF |
| + elif 'type' in json: |
| + json_type = json['type'] |
| + if json_type in simple_types: |
| + self.json_type = json_type |
| + self.type = PropertyType.FUNDAMENTAL |
| + elif json_type == 'array': |
| + self.item_type = PropertyT(name + "_inner", json['items']) |
| + self.type = PropertyType.ARRAY |
| + elif json_type == 'object': |
| + self.properties = {} |
| + self.type = PropertyType.OBJECT |
| + for key, val in json['properties'].items(): |
| + self.properties[key] = PropertyT(key, val) |
| + else: |
| + raise NotImplementedError(json_type) |
| + elif 'choices' in json: |
| + self.type = PropertyType.CHOICES |
| + self.choices = {} |
| + |
| +class PropertyType(object): |
| + """Enum of different types of properties/parameters.""" |
| + STRING, FUNDAMENTAL, ARRAY, REF, CHOICES, OBJECT = range(6) |