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 |
| index 07ead3ddaf4522c9ac21a51d3f095e5098781062..43b9aebe258839c58d4f693adb822cb28309b10d 100644 |
| --- a/tools/json_schema_compiler/model.py |
| +++ b/tools/json_schema_compiler/model.py |
| @@ -51,11 +51,12 @@ class Namespace(object): |
| for property_json in json.get('properties', []): |
| pass |
| for type_json in json.get('types', []): |
|
Yoyo Zhou
2012/04/09 23:48:47
This looks fragile. You're building the types and
cduvall
2012/04/10 03:06:08
Fixed this to not pass types around everywhere.
|
| - type_ = Type(self, type_json['id'], type_json) |
| + type_ = Type(self, type_json['id'], type_json, self.types) |
| self.types[type_.name] = type_ |
| for function_json in json.get('functions', []): |
| if not function_json.get('nocompile', False): |
| - self.functions[function_json['name']] = Function(self, function_json) |
| + self.functions[function_json['name']] = Function(self, function_json, |
|
Yoyo Zhou
2012/04/09 23:48:47
nit: line continuation should be 4 spaces if you p
cduvall
2012/04/10 03:06:08
Done.
|
| + self.types) |
| class Type(object): |
| """A Type defined in the json. |
| @@ -71,12 +72,19 @@ class Type(object): |
| JSON (as described by the schema), such as top-level types and function |
| parameters |
| """ |
| - def __init__(self, parent, name, json): |
| - if not ( |
| - 'properties' in json or |
| - 'additionalProperties' in json or |
| - 'functions' in json): |
| - raise ParseException(name + " has no properties or functions") |
| + def __init__(self, parent, name, json, types): |
| + if json.get('type') == 'array': |
| + self.type_ = PropertyType.ARRAY |
| + self.item_type = Property(self, name + "Element", json['items'], types, |
| + from_json=True, |
| + from_client=True) |
| + else: |
| + if not ( |
| + 'properties' in json or |
| + 'additionalProperties' in json or |
| + 'functions' in json): |
| + raise ParseException(name + " has no properties or functions") |
| + self.type_ = PropertyType.OBJECT |
| self.name = name |
| self.description = json.get('description') |
| self.from_json = True |
| @@ -86,7 +94,8 @@ class Type(object): |
| self.parent = parent |
| for function_json in json.get('functions', []): |
| if not function_json.get('nocompile', False): |
| - self.functions[function_json['name']] = Function(self, function_json) |
| + self.functions[function_json['name']] = Function(self, function_json, |
| + types) |
| props = [] |
| for prop_name, prop_json in json.get('properties', {}).items(): |
| # TODO(calamity): support functions (callbacks) as properties. The model |
| @@ -99,14 +108,14 @@ class Type(object): |
| # C++ let alone the browser). |
| if prop_json.get('type') == 'function': |
| continue |
| - props.append(Property(self, prop_name, prop_json, |
| + props.append(Property(self, prop_name, prop_json, types, |
| from_json=True, |
| from_client=True)) |
| additional_properties = json.get('additionalProperties') |
| if additional_properties: |
| props.append(Property(self, 'additionalProperties', additional_properties, |
| - is_additional_properties=True)) |
| + types, is_additional_properties=True)) |
| for prop in props: |
| if prop.unix_name in self.properties: |
| @@ -121,7 +130,7 @@ class Callback(object): |
| Properties: |
| - |params| the parameters to this callback. |
| """ |
| - def __init__(self, parent, json): |
| + def __init__(self, parent, json, types): |
| params = json['parameters'] |
| self.parent = parent |
| self.params = [] |
| @@ -129,7 +138,7 @@ class Callback(object): |
| return |
| elif len(params) == 1: |
| param = params[0] |
| - self.params.append(Property(self, param['name'], param, |
| + self.params.append(Property(self, param['name'], param, types, |
| from_client=True)) |
| else: |
| raise ParseException("Callbacks can have at most a single parameter") |
| @@ -145,7 +154,7 @@ class Function(object): |
| - |callback| the callback parameter to the function. There should be exactly |
| one |
| """ |
| - def __init__(self, parent, json): |
| + def __init__(self, parent, json, types): |
| self.name = json['name'] |
| self.params = [] |
| self.description = json.get('description') |
| @@ -155,9 +164,9 @@ class Function(object): |
| if param.get('type') == 'function': |
| if self.callback: |
| raise ParseException(self.name + " has more than one callback") |
| - self.callback = Callback(self, param) |
| + self.callback = Callback(self, param, types) |
| else: |
| - self.params.append(Property(self, param['name'], param, |
| + self.params.append(Property(self, param['name'], param, types, |
| from_json=True)) |
| class Property(object): |
| @@ -177,7 +186,7 @@ class Property(object): |
| - |properties| the properties of an OBJECT parameter |
| """ |
| - def __init__(self, parent, name, json, is_additional_properties=False, |
| + def __init__(self, parent, name, json, types, is_additional_properties=False, |
| from_json=False, from_client=False): |
| """ |
| Parameters: |
| @@ -193,11 +202,17 @@ class Property(object): |
| self.optional = json.get('optional', False) |
| self.description = json.get('description') |
| self.parent = parent |
| + self.is_array = False |
| if is_additional_properties: |
| self.type_ = PropertyType.ADDITIONAL_PROPERTIES |
| elif '$ref' in json: |
| self.ref_type = json['$ref'] |
| self.type_ = PropertyType.REF |
| + if self.ref_type in types: |
| + type_reffed = types[self.ref_type] |
| + self.is_array = type_reffed.type_ == PropertyType.ARRAY |
| + if self.is_array: |
| + self.item_type = type_reffed.item_type |
| elif 'enum' in json: |
| self.enum_values = [] |
| for value in json['enum']: |
| @@ -216,7 +231,7 @@ class Property(object): |
| elif json_type == 'number': |
| self.type_ = PropertyType.DOUBLE |
| elif json_type == 'array': |
| - self.item_type = Property(self, name + "Element", json['items'], |
| + self.item_type = Property(self, name + "Element", json['items'], types, |
| from_json=from_json, |
| from_client=from_client) |
| self.type_ = PropertyType.ARRAY |
| @@ -226,7 +241,7 @@ class Property(object): |
| self.properties = {} |
| self.from_json = from_json |
| self.from_client = from_client |
| - type_ = Type(self, self.name, json) |
| + type_ = Type(self, self.name, json, types) |
| self.properties = type_.properties |
| self.functions = type_.functions |
| else: |
| @@ -237,7 +252,7 @@ class Property(object): |
| self.choices = {} |
| self.type_ = PropertyType.CHOICES |
| for choice_json in json['choices']: |
| - choice = Property(self, self.name, choice_json, |
| + choice = Property(self, self.name, choice_json, types, |
| from_json=from_json, |
| from_client=from_client) |
| # A choice gets its unix_name set in |