| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import mojom | 5 import mojom |
| 6 | 6 |
| 7 # mojom_data provides a mechanism to turn mojom Modules to dictionaries and | 7 # mojom_data provides a mechanism to turn mojom Modules to dictionaries and |
| 8 # back again. This can be used to persist a mojom Module created progromatically | 8 # back again. This can be used to persist a mojom Module created progromatically |
| 9 # or to read a dictionary from code or a file. | 9 # or to read a dictionary from code or a file. |
| 10 # Example: | 10 # Example: |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 parameter.default = data.get('default') | 126 parameter.default = data.get('default') |
| 127 return parameter | 127 return parameter |
| 128 | 128 |
| 129 def MethodToData(method): | 129 def MethodToData(method): |
| 130 data = { | 130 data = { |
| 131 istr(0, 'name'): method.name, | 131 istr(0, 'name'): method.name, |
| 132 istr(1, 'parameters'): map(ParameterToData, method.parameters) | 132 istr(1, 'parameters'): map(ParameterToData, method.parameters) |
| 133 } | 133 } |
| 134 if method.ordinal != None: | 134 if method.ordinal != None: |
| 135 data[istr(2, 'ordinal')] = method.ordinal | 135 data[istr(2, 'ordinal')] = method.ordinal |
| 136 if method.response_parameters != None: |
| 137 data[istr(3, 'response_parameters')] = map( |
| 138 ParameterToData, method.response_parameters) |
| 136 return data | 139 return data |
| 137 | 140 |
| 138 def MethodFromData(kinds, data): | 141 def MethodFromData(kinds, data): |
| 139 method = mojom.Method() | 142 method = mojom.Method() |
| 140 method.name = data['name'] | 143 method.name = data['name'] |
| 141 method.ordinal = data.get('ordinal') | 144 method.ordinal = data.get('ordinal') |
| 142 method.default = data.get('default') | 145 method.default = data.get('default') |
| 143 method.parameters = map( | 146 method.parameters = map( |
| 144 lambda parameter: ParameterFromData(kinds, parameter), data['parameters']) | 147 lambda parameter: ParameterFromData(kinds, parameter), data['parameters']) |
| 148 if data.has_key('response_parameters'): |
| 149 method.response_parameters = map( |
| 150 lambda parameter: ParameterFromData(kinds, parameter), |
| 151 data['response_parameters']) |
| 145 return method | 152 return method |
| 146 | 153 |
| 147 def InterfaceToData(interface): | 154 def InterfaceToData(interface): |
| 148 return { | 155 return { |
| 149 istr(0, 'name'): interface.name, | 156 istr(0, 'name'): interface.name, |
| 150 istr(1, 'peer'): interface.peer, | 157 istr(1, 'peer'): interface.peer, |
| 151 istr(2, 'methods'): map(MethodToData, interface.methods) | 158 istr(2, 'methods'): map(MethodToData, interface.methods) |
| 152 } | 159 } |
| 153 | 160 |
| 154 def InterfaceFromData(module, data): | 161 def InterfaceFromData(module, data): |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 def ModuleFromData(data): | 195 def ModuleFromData(data): |
| 189 module = mojom.Module() | 196 module = mojom.Module() |
| 190 module.kinds = {} | 197 module.kinds = {} |
| 191 for kind in mojom.PRIMITIVES: | 198 for kind in mojom.PRIMITIVES: |
| 192 module.kinds[kind.spec] = kind | 199 module.kinds[kind.spec] = kind |
| 193 | 200 |
| 194 module.name = data['name'] | 201 module.name = data['name'] |
| 195 module.namespace = data['namespace'] | 202 module.namespace = data['namespace'] |
| 196 # Imports must come first, because they add to module.kinds which is used | 203 # Imports must come first, because they add to module.kinds which is used |
| 197 # by by the others. | 204 # by by the others. |
| 198 module.imports = map( | 205 if data.has_key('imports'): |
| 199 lambda import_data: ImportFromData(module, import_data), | 206 module.imports = map( |
| 200 data['imports']) | 207 lambda import_data: ImportFromData(module, import_data), |
| 208 data['imports']) |
| 201 module.structs = map( | 209 module.structs = map( |
| 202 lambda struct: StructFromData(module, struct), data['structs']) | 210 lambda struct: StructFromData(module, struct), data['structs']) |
| 203 module.interfaces = map( | 211 module.interfaces = map( |
| 204 lambda interface: InterfaceFromData(module, interface), | 212 lambda interface: InterfaceFromData(module, interface), |
| 205 data['interfaces']) | 213 data['interfaces']) |
| 206 if data.has_key('enums'): | 214 if data.has_key('enums'): |
| 207 module.enums = map( | 215 module.enums = map( |
| 208 lambda enum: EnumFromData(module.kinds, enum), data['enums']) | 216 lambda enum: EnumFromData(module.kinds, enum), data['enums']) |
| 209 return module | 217 return module |
| 210 | 218 |
| 211 def OrderedModuleFromData(data): | 219 def OrderedModuleFromData(data): |
| 212 module = ModuleFromData(data) | 220 module = ModuleFromData(data) |
| 213 next_interface_ordinal = 0 | 221 next_interface_ordinal = 0 |
| 214 for interface in module.interfaces: | 222 for interface in module.interfaces: |
| 215 next_ordinal = 0 | 223 next_ordinal = 0 |
| 216 for method in interface.methods: | 224 for method in interface.methods: |
| 217 if method.ordinal is None: | 225 if method.ordinal is None: |
| 218 method.ordinal = next_ordinal | 226 method.ordinal = next_ordinal |
| 219 next_ordinal = method.ordinal + 1 | 227 next_ordinal = method.ordinal + 1 |
| 220 return module | 228 return module |
| OLD | NEW |