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 38a70acd65912dfc39ec0614f81b94f458677a88..c952a5e395c667c8742872506978da0db3b9c9cc 100644 |
| --- a/tools/json_schema_compiler/model.py |
| +++ b/tools/json_schema_compiler/model.py |
| @@ -51,10 +51,10 @@ class Namespace(object): |
| self.source_file = source_file |
| self.source_file_dir, self.source_file_filename = os.path.split(source_file) |
| self.parent = None |
| - _AddTypes(self, json) |
| - _AddFunctions(self, json) |
| - _AddEvents(self, json) |
| - _AddProperties(self, json) |
| + _AddTypes(self, json, self.name) |
|
not at google - send to devlin
2012/10/05 01:30:16
I think passing around the namespace itself rather
cduvall
2012/10/05 01:44:57
Done.
|
| + _AddFunctions(self, json, self.name) |
| + _AddEvents(self, json, self.name) |
| + _AddProperties(self, json, self.name) |
| class Type(object): |
| """A Type defined in the json. |
| @@ -73,10 +73,13 @@ class Type(object): |
| - |type_| the PropertyType of this Type |
| - |item_type| if this is an array, the type of items in the array |
|
not at google - send to devlin
2012/10/05 01:30:16
add simple_name to this list?
cduvall
2012/10/05 01:44:57
Done.
|
| """ |
| - def __init__(self, parent, name, json): |
| + def __init__(self, parent, name, json, namespace_name): |
| if json.get('type') == 'array': |
| self.type_ = PropertyType.ARRAY |
| - self.item_type = Property(self, name + "Element", json['items'], |
| + self.item_type = Property(self, |
| + name + "Element", |
| + json['items'], |
| + namespace_name, |
| from_json=True, |
| from_client=True) |
| elif 'enum' in json: |
| @@ -95,15 +98,16 @@ class Type(object): |
| raise ParseException(self, name + " has no properties or functions") |
| self.type_ = PropertyType.OBJECT |
| self.name = name |
| + self.simple_name = _StripNamespace(self.name, namespace_name) |
| self.unix_name = UnixName(self.name) |
| self.description = json.get('description') |
| self.from_json = True |
| self.from_client = True |
| self.parent = parent |
| self.instance_of = json.get('isInstanceOf', None) |
| - _AddFunctions(self, json) |
| - _AddEvents(self, json) |
| - _AddProperties(self, json, from_json=True, from_client=True) |
| + _AddFunctions(self, json, namespace_name) |
| + _AddEvents(self, json, namespace_name) |
| + _AddProperties(self, json, namespace_name, from_json=True, from_client=True) |
| additional_properties_key = 'additionalProperties' |
| additional_properties = json.get(additional_properties_key) |
| @@ -112,6 +116,7 @@ class Type(object): |
| self, |
| additional_properties_key, |
| additional_properties, |
| + namespace_name, |
| is_additional_properties=True) |
| class Function(object): |
| @@ -127,8 +132,14 @@ class Function(object): |
| - |optional| whether the Function is "optional"; this only makes sense to be |
| present when the Function is representing a callback property. |
| """ |
| - def __init__(self, parent, json, from_json=False, from_client=False): |
| + def __init__(self, |
| + parent, |
| + json, |
| + namespace_name, |
| + from_json=False, |
| + from_client=False): |
| self.name = json['name'] |
| + self.simple_name = _StripNamespace(self.name, namespace_name) |
| self.params = [] |
| self.description = json.get('description') |
| self.callback = None |
| @@ -143,6 +154,7 @@ class Function(object): |
| def GeneratePropertyFromParam(p): |
| return Property(self, |
| p['name'], p, |
| + namespace_name, |
| from_json=from_json, |
| from_client=from_client) |
| @@ -161,11 +173,14 @@ class Function(object): |
| self.params.append(GeneratePropertyFromParam(param)) |
| if callback_param: |
| - self.callback = Function(self, callback_param, from_client=True) |
| + self.callback = Function(self, |
| + callback_param, |
| + namespace_name, |
| + from_client=True) |
| self.returns = None |
| if 'returns' in json: |
| - self.returns = Property(self, 'return', json['returns']) |
| + self.returns = Property(self, 'return', json['returns'], namespace_name) |
| class Property(object): |
| """A property of a type OR a parameter to a function. |
| @@ -191,9 +206,16 @@ class Property(object): |
| parameters |
| """ |
| - def __init__(self, parent, name, json, is_additional_properties=False, |
| - from_json=False, from_client=False): |
| + def __init__(self, |
| + parent, |
| + name, |
| + json, |
| + namespace_name, |
| + is_additional_properties=False, |
| + from_json=False, |
| + from_client=False): |
| self.name = name |
| + self.simple_name = _StripNamespace(self.name, namespace_name) |
| self._unix_name = UnixName(self.name) |
| self._unix_name_used = False |
| self.optional = json.get('optional', False) |
| @@ -204,7 +226,7 @@ class Property(object): |
| self.from_json = from_json |
| self.from_client = from_client |
| self.instance_of = json.get('isInstanceOf', None) |
| - _AddProperties(self, json) |
| + _AddProperties(self, json, namespace_name) |
| if is_additional_properties: |
| self.type_ = PropertyType.ADDITIONAL_PROPERTIES |
| elif '$ref' in json: |
| @@ -220,12 +242,15 @@ class Property(object): |
| elif 'type' in json: |
| self.type_ = self._JsonTypeToPropertyType(json['type']) |
| if self.type_ == PropertyType.ARRAY: |
| - self.item_type = Property(self, name + "Element", json['items'], |
| - from_json=from_json, |
| - from_client=from_client) |
| + self.item_type = Property(self, |
| + name + "Element", |
| + json['items'], |
| + namespace_name, |
| + from_json=from_json, |
| + from_client=from_client) |
| elif self.type_ == PropertyType.OBJECT: |
| # These members are read when this OBJECT Property is used as a Type |
| - type_ = Type(self, self.name, json) |
| + type_ = Type(self, self.name, json, namespace_name) |
| # self.properties will already have some value from |_AddProperties|. |
| self.properties.update(type_.properties) |
| self.functions = type_.functions |
| @@ -236,9 +261,12 @@ class Property(object): |
| self.type_ = PropertyType.CHOICES |
| self.compiled_type = self.type_ |
| for choice_json in json['choices']: |
| - choice = Property(self, self.name, choice_json, |
| - from_json=from_json, |
| - from_client=from_client) |
| + choice = Property(self, |
| + self.name, |
| + choice_json, |
| + namespace_name, |
| + from_json=from_json, |
| + from_client=from_client) |
| choice.unix_name = UnixName(self.name + choice.type_.name) |
| # The existence of any single choice is optional |
| choice.optional = True |
| @@ -353,6 +381,11 @@ def UnixName(name): |
| # Finally, replace any remaining periods, and make lowercase. |
| return s2.replace('.', '_').lower() |
| +def _StripNamespace(name, namespace_name): |
| + if name.startswith(namespace_name + '.'): |
| + return name[len(namespace_name + '.'):] |
| + return name |
| + |
| def _GetModelHierarchy(entity): |
| """Returns the hierarchy of the given model entity.""" |
| hierarchy = [] |
| @@ -365,32 +398,36 @@ def _GetModelHierarchy(entity): |
| hierarchy.reverse() |
| return hierarchy |
| -def _AddTypes(model, json): |
| +def _AddTypes(model, json, namespace_name): |
| """Adds Type objects to |model| contained in the 'types' field of |json|. |
| """ |
| model.types = {} |
| for type_json in json.get('types', []): |
| - type_ = Type(model, type_json['id'], type_json) |
| + type_ = Type(model, type_json['id'], type_json, namespace_name) |
| model.types[type_.name] = type_ |
| -def _AddFunctions(model, json): |
| +def _AddFunctions(model, json, namespace_name): |
| """Adds Function objects to |model| contained in the 'functions' field of |
| |json|. |
| """ |
| model.functions = {} |
| for function_json in json.get('functions', []): |
| - function = Function(model, function_json, from_json=True) |
| + function = Function(model, function_json, namespace_name, from_json=True) |
| model.functions[function.name] = function |
| -def _AddEvents(model, json): |
| +def _AddEvents(model, json, namespace_name): |
| """Adds Function objects to |model| contained in the 'events' field of |json|. |
| """ |
| model.events = {} |
| for event_json in json.get('events', []): |
| - event = Function(model, event_json, from_client=True) |
| + event = Function(model, event_json, namespace_name, from_client=True) |
| model.events[event.name] = event |
| -def _AddProperties(model, json, from_json=False, from_client=False): |
| +def _AddProperties(model, |
| + json, |
| + namespace_name, |
| + from_json=False, |
| + from_client=False): |
| """Adds model.Property objects to |model| contained in the 'properties' field |
| of |json|. |
| """ |
| @@ -400,5 +437,6 @@ def _AddProperties(model, json, from_json=False, from_client=False): |
| model, |
| name, |
| property_json, |
| + namespace_name, |
| from_json=from_json, |
| from_client=from_client) |