Chromium Code Reviews| 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 | |
| 6 | |
| 7 import third_party.json_schema_compiler.model as model | |
| 8 | |
| 9 def _RemoveNoDocs(item): | |
| 10 if type(item) == dict: | |
| 11 if item.get('nodoc', False): | |
| 12 return True | |
| 13 for key, value in item.items(): | |
| 14 if _RemoveNoDocs(value): | |
| 15 del item[key] | |
| 16 elif type(item) == list: | |
| 17 for i in item: | |
| 18 if _RemoveNoDocs(i): | |
| 19 item.remove(i) | |
| 20 return False | |
| 21 | |
| 22 class HandlebarDictGenerator(object): | |
| 23 """Uses a Model from the JSON Schema Compiler and generates a dict that | |
| 24 a Handlebar template can use for a data source. | |
| 25 """ | |
| 26 def __init__(self, json): | |
| 27 _RemoveNoDocs(json) | |
|
not at google - send to devlin
2012/06/20 18:11:53
Would prefer not to modify the input json here. Ma
cduvall
2012/06/20 18:24:49
Done.
| |
| 28 self._namespace = model.Namespace(json, json['namespace']) | |
| 29 | |
| 30 def Generate(self): | |
| 31 return { | |
| 32 'name': self._namespace.name, | |
| 33 'types': self._GenerateTypes(self._namespace.types), | |
| 34 'functions': self._GenerateFunctions(self._namespace.functions) | |
| 35 } | |
| 36 | |
| 37 def _GenerateTypes(self, types): | |
| 38 types_list = [] | |
| 39 for type_name in types: | |
| 40 types_list.append(self._GenerateType(types[type_name])) | |
| 41 return types_list | |
| 42 | |
| 43 def _GenerateType(self, type_): | |
| 44 type_dict = { | |
| 45 'name': type_.name, | |
| 46 'type': type_.type_.name, | |
| 47 'description': type_.description, | |
| 48 'properties': self._GenerateProperties(type_.properties), | |
| 49 'functions': self._GenerateFunctions(type_.functions) | |
| 50 } | |
| 51 # Only Array types have 'item_type'. | |
| 52 if type_.type_ == model.PropertyType.ARRAY: | |
| 53 type_dict['item_type'] = self._GenerateType(type_.item_type) | |
|
not at google - send to devlin
2012/06/20 18:11:53
It's this kind of stuff that I think will end up c
cduvall
2012/06/20 18:24:49
Done.
| |
| 54 return type_dict | |
| 55 | |
| 56 def _GenerateFunctions(self, functions): | |
| 57 functions_list = [] | |
| 58 for function_name in functions: | |
| 59 functions_list.append(self._GenerateFunction(functions[function_name])) | |
| 60 return functions_list | |
| 61 | |
| 62 def _GenerateFunction(self, function): | |
| 63 function_dict = { | |
| 64 'name': function.name, | |
| 65 'description': function.description, | |
| 66 'callback': self._GenerateCallback(function.callback), | |
| 67 'parameters': [] | |
| 68 } | |
| 69 for param in function.params: | |
| 70 function_dict['parameters'].append(self._GenerateProperty(param)) | |
| 71 return function_dict | |
| 72 | |
| 73 def _GenerateCallback(self, callback): | |
| 74 callback_dict = { | |
| 75 'name': 'callback', | |
| 76 'parameters': [] | |
| 77 } | |
| 78 for param in callback.params: | |
| 79 callback_dict['parameters'].append(self._GenerateProperty(param)) | |
| 80 return callback_dict | |
| 81 | |
| 82 def _GenerateProperties(self, properties): | |
| 83 properties_list = [] | |
| 84 for property_name in properties: | |
| 85 properties_list.append(self._GenerateProperty(properties[property_name])) | |
| 86 return properties_list | |
| 87 | |
| 88 def _GenerateProperty(self, property_): | |
| 89 property_dict = { | |
| 90 'name': property_.name, | |
| 91 'type': property_.type_.name, | |
| 92 'optional': property_.optional, | |
| 93 'description': property_.description, | |
| 94 'properties': self._GenerateProperties(property_.properties) | |
| 95 } | |
| 96 if property_.type_ == model.PropertyType.CHOICES: | |
| 97 property_dict['choices'] = [] | |
| 98 for choice_name in property_.choices: | |
| 99 property_dict['choices'].append( | |
| 100 self._GenerateProperty(property_.choices[choice_name])) | |
| 101 elif property_.type_ == model.PropertyType.REF: | |
| 102 property_dict['ref_type'] = property_.ref_type | |
| 103 elif property_.type_ == model.PropertyType.ARRAY: | |
| 104 property_dict['item_type'] = self._GenerateProperty(property_.item_type) | |
|
not at google - send to devlin
2012/06/20 18:11:53
ditto, my comment above applies to all these "type
cduvall
2012/06/20 18:24:49
Done.
| |
| 105 return property_dict | |
| OLD | NEW |