diff options
Diffstat (limited to 'chromium/tools/json_schema_compiler/model.py')
-rw-r--r-- | chromium/tools/json_schema_compiler/model.py | 90 |
1 files changed, 86 insertions, 4 deletions
diff --git a/chromium/tools/json_schema_compiler/model.py b/chromium/tools/json_schema_compiler/model.py index 082fbc60f85..dd68e9b1056 100644 --- a/chromium/tools/json_schema_compiler/model.py +++ b/chromium/tools/json_schema_compiler/model.py @@ -7,6 +7,7 @@ import os.path from json_parse import OrderedDict from memoize import memoize + class ParseException(Exception): """Thrown when data in the model is invalid. """ @@ -16,6 +17,7 @@ class ParseException(Exception): Exception.__init__( self, 'Model parse exception at:\n' + '\n'.join(hierarchy)) + class Model(object): """Model of all namespaces that comprise an API. @@ -34,6 +36,45 @@ class Model(object): self.namespaces[namespace.name] = namespace return namespace + +def CreateFeature(name, model): + if isinstance(model, dict): + return SimpleFeature(name, model) + return ComplexFeature(name, [SimpleFeature(name, child) for child in model]) + + +class ComplexFeature(object): + """A complex feature which may be made of several simple features. + + Properties: + - |name| the name of the feature + - |unix_name| the unix_name of the feature + - |feature_list| a list of simple features which make up the feature + """ + def __init__(self, feature_name, features): + self.name = feature_name + self.unix_name = UnixName(self.name) + self.feature_list = features + +class SimpleFeature(object): + """A simple feature, which can make up a complex feature, as specified in + files such as chrome/common/extensions/api/_permission_features.json. + + Properties: + - |name| the name of the feature + - |unix_name| the unix_name of the feature + - |channel| the channel where the feature is released + - |extension_types| the types which can use the feature + - |whitelist| a list of extensions allowed to use the feature + """ + def __init__(self, feature_name, feature_def): + self.name = feature_name + self.unix_name = UnixName(self.name) + self.channel = feature_def['channel'] + self.extension_types = feature_def['extension_types'] + self.whitelist = feature_def.get('whitelist') + + class Namespace(object): """An API namespace. @@ -71,8 +112,12 @@ class Namespace(object): self.functions = _GetFunctions(self, json, self) self.events = _GetEvents(self, json, self) self.properties = _GetProperties(self, json, self, toplevel_origin) - self.compiler_options = (json.get('compiler_options', {}) - if include_compiler_options else {}) + if include_compiler_options: + self.compiler_options = json.get('compiler_options', {}) + else: + self.compiler_options = {} + self.documentation_options = json.get('documentation_options', {}) + class Origin(object): """Stores the possible origin of model object as a pair of bools. These are: @@ -92,6 +137,7 @@ class Origin(object): self.from_client = from_client self.from_json = from_json + class Type(object): """A Type defined in the json. @@ -140,7 +186,7 @@ class Type(object): self.ref_type = json['$ref'] elif 'enum' in json and json_type == 'string': self.property_type = PropertyType.ENUM - self.enum_values = [value for value in json['enum']] + self.enum_values = [EnumValue(value) for value in json['enum']] elif json_type == 'any': self.property_type = PropertyType.ANY elif json_type == 'binary': @@ -198,6 +244,7 @@ class Type(object): else: raise ParseException(self, 'Unsupported JSON type %s' % json_type) + class Function(object): """A Function defined in the API. @@ -207,6 +254,7 @@ class Function(object): available to - |params| a list of parameters to the function (order matters). A separate parameter is used for each choice of a 'choices' parameter + - |deprecated| a reason and possible alternative for a deprecated function - |description| a description of the function (if provided) - |callback| the callback parameter to the function. There should be exactly one @@ -227,6 +275,7 @@ class Function(object): self.platforms = _GetPlatforms(json) self.params = [] self.description = json.get('description') + self.deprecated = json.get('deprecated') self.callback = None self.optional = json.get('optional', False) self.parent = parent @@ -236,6 +285,7 @@ class Function(object): self.actions = options.get('actions', []) self.supports_listeners = options.get('supportsListeners', True) self.supports_rules = options.get('supportsRules', False) + self.supports_dom = options.get('supportsDom', False) def GeneratePropertyFromParam(p): return Property(self, p['name'], p, namespace, origin) @@ -268,6 +318,7 @@ class Function(object): namespace, origin) + class Property(object): """A property of a type OR a parameter to a function. Properties: @@ -278,6 +329,7 @@ class Property(object): - |description| a description of the property (if provided) - |type_| the model.Type of this property - |simple_name| the name of this Property without a namespace + - |deprecated| a reason and possible alternative for a deprecated property """ def __init__(self, parent, name, json, namespace, origin): """Creates a Property from JSON. @@ -291,6 +343,7 @@ class Property(object): self.description = json.get('description', None) self.optional = json.get('optional', None) self.instance_of = json.get('isInstanceOf', None) + self.deprecated = json.get('deprecated') # HACK: only support very specific value types. is_allowed_value = ( @@ -339,6 +392,20 @@ class Property(object): unix_name = property(GetUnixName, SetUnixName) +class EnumValue(object): + """A single value from an enum. + Properties: + - |name| name of the property as in the json. + - |description| a description of the property (if provided) + """ + def __init__(self, json): + if isinstance(json, dict): + self.name = json['name'] + self.description = json.get('description') + else: + self.name = json + self.description = None + class _Enum(object): """Superclass for enum types with a "name" field, setting up repr/eq/ne. Enums need to do this so that equality/non-equality work over pickling. @@ -366,11 +433,13 @@ class _Enum(object): def __str__(self): return repr(self) + class _PropertyTypeInfo(_Enum): def __init__(self, is_fundamental, name): _Enum.__init__(self, name) self.is_fundamental = is_fundamental + class PropertyType(object): """Enum of different types of properties/parameters. """ @@ -388,6 +457,7 @@ class PropertyType(object): REF = _PropertyTypeInfo(False, "ref") STRING = _PropertyTypeInfo(True, "string") + @memoize def UnixName(name): '''Returns the unix_style name for a given lowerCamelCase string. @@ -409,11 +479,13 @@ def UnixName(name): unix_name.append(c.lower()) return ''.join(unix_name) + def _StripNamespace(name, namespace): if name.startswith(namespace.name + '.'): return name[len(namespace.name + '.'):] return name + def _GetModelHierarchy(entity): """Returns the hierarchy of the given model entity.""" hierarchy = [] @@ -425,6 +497,7 @@ def _GetModelHierarchy(entity): hierarchy.reverse() return hierarchy + def _GetTypes(parent, json, namespace, origin): """Creates Type objects extracted from |json|. """ @@ -434,6 +507,7 @@ def _GetTypes(parent, json, namespace, origin): types[type_.name] = type_ return types + def _GetFunctions(parent, json, namespace): """Creates Function objects extracted from |json|. """ @@ -447,6 +521,7 @@ def _GetFunctions(parent, json, namespace): functions[function.name] = function return functions + def _GetEvents(parent, json, namespace): """Creates Function objects generated from the events in |json|. """ @@ -460,6 +535,7 @@ def _GetEvents(parent, json, namespace): events[event.name] = event return events + def _GetProperties(parent, json, namespace, origin): """Generates Property objects extracted from |json|. """ @@ -468,10 +544,12 @@ def _GetProperties(parent, json, namespace, origin): properties[name] = Property(parent, name, property_json, namespace, origin) return properties + class _PlatformInfo(_Enum): def __init__(self, name): _Enum.__init__(self, name) + class Platforms(object): """Enum of the possible platforms. """ @@ -481,9 +559,13 @@ class Platforms(object): MAC = _PlatformInfo("mac") WIN = _PlatformInfo("win") + def _GetPlatforms(json): - if 'platforms' not in json: + if 'platforms' not in json or json['platforms'] == None: return None + # Sanity check: platforms should not be an empty list. + if not json['platforms']: + raise ValueError('"platforms" cannot be an empty list') platforms = [] for platform_name in json['platforms']: for platform_enum in _Enum.GetAll(Platforms): |