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

Unified Diff: tools/json_schema_compiler/model.py

Issue 10701012: JSON Schema Compiler: Added event compilation. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Reworked Create functions and lots of tests. Created 8 years, 5 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
index 63973d43405e939a1416c0b51d339807005d8a16..751348a54621557edb2a5a9f938a143224457fd2 100644
--- a/tools/json_schema_compiler/model.py
+++ b/tools/json_schema_compiler/model.py
@@ -42,6 +42,7 @@ class Namespace(object):
- |source_file_filename| the filename component of |source_file|
- |types| a map of type names to their model.Type
- |functions| a map of function names to their model.Function
+ - |events| a map of event names to their model.Function
- |properties| a map of property names to their model.Property
"""
def __init__(self, json, source_file):
@@ -52,6 +53,7 @@ class Namespace(object):
self.parent = None
_AddTypes(self, json)
_AddFunctions(self, json)
+ _AddEvents(self, json)
_AddProperties(self, json)
class Type(object):
@@ -102,29 +104,6 @@ class Type(object):
additional_properties,
is_additional_properties=True)
-class Callback(object):
- """A callback parameter to a Function.
-
- Properties:
- - |params| the parameters to this callback.
- """
- def __init__(self, parent, json):
- params = json['parameters']
- self.parent = parent
- self.description = json.get('description')
- self.optional = json.get('optional', False)
- self.params = []
- if len(params) == 0:
- return
- elif len(params) == 1:
- param = params[0]
- self.params.append(Property(self, param['name'], param,
- from_client=True))
- else:
- raise ParseException(
- self,
- "Callbacks can have at most a single parameter")
-
class Function(object):
"""A Function defined in the API.
@@ -136,21 +115,21 @@ 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, from_json=False, from_client=False):
self.name = json['name']
self.params = []
self.description = json.get('description')
self.callback = None
self.parent = parent
self.nocompile = json.get('nocompile')
- for param in json['parameters']:
+ for param in json.get('parameters', []):
if param.get('type') == 'function':
if self.callback:
raise ParseException(self, self.name + " has more than one callback")
- self.callback = Callback(self, param)
+ self.callback = Function(self, param, from_client=True)
else:
self.params.append(Property(self, param['name'], param,
- from_json=True))
+ from_json=from_json, from_client=from_client))
class Property(object):
"""A property of a type OR a parameter to a function.
@@ -167,18 +146,15 @@ class Property(object):
- |item_type| a model.Property representing the type of each element in an
ARRAY
- |properties| the properties of an OBJECT parameter
+ - |from_client| indicates that instances of the Type can originate from the
+ users of generated code, such as top-level types and function results
+ - |from_json| indicates that instances of the Type can originate from the
+ JSON (as described by the schema), such as top-level types and function
+ parameters
"""
def __init__(self, parent, name, json, is_additional_properties=False,
from_json=False, from_client=False):
- """
- Parameters:
- - |from_json| indicates that instances of the Type can originate from the
- JSON (as described by the schema), such as top-level types and function
- parameters
- - |from_client| indicates that instances of the Type can originate from the
- users of generated code, such as top-level types and function results
- """
self.name = name
self._unix_name = UnixName(self.name)
self._unix_name_used = False
@@ -187,6 +163,8 @@ class Property(object):
self.has_value = False
self.description = json.get('description')
self.parent = parent
+ self.from_json = from_json
+ self.from_client = from_client
_AddProperties(self, json)
if is_additional_properties:
self.type_ = PropertyType.ADDITIONAL_PROPERTIES
@@ -218,8 +196,6 @@ class Property(object):
elif json_type == 'object':
self.type_ = PropertyType.OBJECT
# These members are read when this OBJECT Property is used as a Type
- self.from_json = from_json
- self.from_client = from_client
type_ = Type(self, self.name, json)
# self.properties will already have some value from |_AddProperties|.
self.properties.update(type_.properties)
@@ -229,7 +205,7 @@ class Property(object):
else:
raise ParseException(self, 'type ' + json_type + ' not recognized')
elif 'choices' in json:
- if not json['choices']:
+ if not json['choices'] or len(json['choices']) == 0:
raise ParseException(self, 'Choices has no choices')
self.choices = {}
self.type_ = PropertyType.CHOICES
@@ -237,9 +213,7 @@ class Property(object):
choice = Property(self, self.name, choice_json,
from_json=from_json,
from_client=from_client)
- # A choice gets its unix_name set in
- # cpp_type_generator.GetExpandedChoicesInParams
- choice._unix_name = None
+ choice.unix_name = UnixName(self.name + choice.type_.name)
# The existence of any single choice is optional
choice.optional = True
self.choices[choice.type_] = choice
@@ -342,13 +316,22 @@ def _AddTypes(model, json):
model.types[type_.name] = type_
def _AddFunctions(model, json):
- """Adds Function objects to |model| contained in the 'types' field of |json|.
+ """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)
+ function = Function(model, function_json, from_json=True)
model.functions[function.name] = function
+def _AddEvents(model, json):
+ """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)
+ model.events[event.name] = event
+
def _AddProperties(model, json, from_json=False, from_client=False):
"""Adds model.Property objects to |model| contained in the 'properties' field
of |json|.

Powered by Google App Engine
This is Rietveld 408576698