OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import os.path |
| 6 |
| 7 class Model(object): |
| 8 """Model of all namespaces that comprise an API. |
| 9 """ |
| 10 def __init__(self): |
| 11 self.namespaces = {} |
| 12 |
| 13 def AddNamespace(self, json, source_file): |
| 14 """Add a namespace's json to the model if it has a "compile" property set |
| 15 to true. Returns the new namespace or None if a namespace wasn't added. |
| 16 """ |
| 17 if not json.get('compile'): |
| 18 return None |
| 19 namespace = Namespace(json, source_file) |
| 20 self.namespaces[namespace.name] = namespace |
| 21 return namespace |
| 22 |
| 23 class Namespace(object): |
| 24 """An API namespace. |
| 25 """ |
| 26 def __init__(self, json, source_file): |
| 27 self.name = json['namespace'] |
| 28 self.source_file = source_file |
| 29 self.source_file_dir, self.source_file_filename = os.path.split(source_file) |
| 30 self.type_dependencies = {} |
| 31 self.types = {} |
| 32 self.functions = {} |
| 33 for type_json in json['types']: |
| 34 type_ = Type(type_json) |
| 35 self.types[type_.name] = type_ |
| 36 for function_json in json['functions']: |
| 37 if not function_json.get('nocompile'): |
| 38 function = Function(function_json) |
| 39 self.functions[function.name] = function |
| 40 |
| 41 class Type(object): |
| 42 """A Type defined in the json. |
| 43 """ |
| 44 def __init__(self, json): |
| 45 self.name = json['id'] |
| 46 self.description = json.get('description') |
| 47 self.properties = {} |
| 48 for prop_name, prop_json in json['properties'].items(): |
| 49 self.properties[prop_name] = Property(prop_name, prop_json) |
| 50 |
| 51 class Callback(object): |
| 52 """A callback parameter to a Function. |
| 53 """ |
| 54 def __init__(self, json): |
| 55 params = json['parameters'] |
| 56 if len(params) == 0: |
| 57 self.param = None |
| 58 elif len(params) == 1: |
| 59 param = params[0] |
| 60 self.param = Property(param['name'], param) |
| 61 else: |
| 62 raise AssertionError("Callbacks can have at most a single parameter") |
| 63 |
| 64 class Function(object): |
| 65 """A Function defined in the API. |
| 66 """ |
| 67 def __init__(self, json): |
| 68 self.name = json['name'] |
| 69 self.params = [] |
| 70 self.description = json['description'] |
| 71 self.callback = None |
| 72 self.type_dependencies = {} |
| 73 for param in json['parameters']: |
| 74 if param.get('type') == 'function': |
| 75 assert (not self.callback), "Function has more than one callback" |
| 76 self.callback = Callback(param) |
| 77 else: |
| 78 self.params.append(Property(param['name'], param)) |
| 79 assert (self.callback), "Function does not support callback" |
| 80 |
| 81 # TODO(calamity): handle Enum/choices |
| 82 class Property(object): |
| 83 """A property of a type OR a parameter to a function. |
| 84 |
| 85 Members will change based on PropertyType. Check self.type_ to determine which |
| 86 members actually exist. |
| 87 """ |
| 88 def __init__(self, name, json): |
| 89 self.name = name |
| 90 self.optional = json.get('optional', False) |
| 91 self.description = json.get('description') |
| 92 # TODO(calamity) maybe check for circular refs? could that be a problem? |
| 93 if '$ref' in json: |
| 94 self.ref_type = json['$ref'] |
| 95 self.type_ = PropertyType.REF |
| 96 elif 'type' in json: |
| 97 json_type = json['type'] |
| 98 if json_type == 'string': |
| 99 self.type_ = PropertyType.STRING |
| 100 elif json_type == 'boolean': |
| 101 self.type_ = PropertyType.BOOLEAN |
| 102 elif json_type == 'integer': |
| 103 self.type_ = PropertyType.INTEGER |
| 104 elif json_type == 'double': |
| 105 self.type_ = PropertyType.DOUBLE |
| 106 elif json_type == 'array': |
| 107 self.item_type = Property(name + "_inner", json['items']) |
| 108 self.type_ = PropertyType.ARRAY |
| 109 elif json_type == 'object': |
| 110 self.properties = {} |
| 111 self.type_ = PropertyType.OBJECT |
| 112 for key, val in json['properties'].items(): |
| 113 self.properties[key] = Property(key, val) |
| 114 else: |
| 115 raise NotImplementedError(json_type) |
| 116 elif 'choices' in json: |
| 117 self.type_ = PropertyType.CHOICES |
| 118 self.choices = {} |
| 119 |
| 120 class PropertyType(object): |
| 121 """Enum of different types of properties/parameters. |
| 122 """ |
| 123 class _Info(object): |
| 124 def __init__(self, is_fundamental): |
| 125 self.is_fundamental = is_fundamental |
| 126 |
| 127 INTEGER = _Info(True) |
| 128 DOUBLE = _Info(True) |
| 129 BOOLEAN = _Info(True) |
| 130 STRING = _Info(True) |
| 131 ARRAY = _Info(False) |
| 132 REF = _Info(False) |
| 133 CHOICES = _Info(False) |
| 134 OBJECT = _Info(False) |
OLD | NEW |