Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1385)

Unified Diff: tools/json_schema_compiler/model.py

Issue 9114036: Code generation for extensions api (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: some rework Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..e192003bb51750db1ec21fd2237bf15dc082d56b
--- /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 Model(object):
+ """Model of the API."""
+ 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('compile'):
+ return None
+ namespace = Namespace(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 Namespace(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 = Type(type_json)
+ self.types[tipe.name] = tipe
+ for function_json in json['functions']:
+ if not function_json.get('nocompile'):
+ function = Function(function_json)
+ self.functions[function.name] = function
+
+class Type(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] = Property(prop_name, prop_json)
+
+class Callback(object):
+ """A callback parameter to a Function."""
+ def __init__(self, json):
+ self.param = None
+ assert len(json['parameters']) <= 1
not at google - send to devlin 2012/01/13 06:45:55 add a message like, "Callbacks can have at most a
calamity 2012/01/16 04:01:06 Done.
+ for param in json['parameters']:
+ self.param = Property(param['name'], param)
+
+class Function(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 = Callback(param)
+ continue
+ self.params.append(Property(param['name'], param))
+ if not self.callback:
+ raise KeyError
+
+# TODO(calamity): handle Enum/choices
+class Property(object):
+ """A property of a type OR a parameter to a function.
+
+ A polymorphic type, check self.type to determine which
+ members actually exist.
not at google - send to devlin 2012/01/13 02:14:09 not sure what this sentence means...
not at google - send to devlin 2012/01/17 05:42:32 Ah I see. How about like "This is a union type, c
+ """
+ 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 = Property(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] = Property(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)
calamity 2012/01/16 04:01:06 PropertyType.STRING is unused. On dev branch, FUND
not at google - send to devlin 2012/01/17 01:59:24 I'm missing the context of this comment.
calamity 2012/01/18 05:43:08 Just a note in case you were wondering why Propert

Powered by Google App Engine
This is Rietveld 408576698