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 DictGenerator(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) | |
| 28 self._namespace = model.Namespace(json, json['namespace']) | |
| 29 | |
| 30 def _GenerateType(self, t): | |
|
not at google - send to devlin
2012/06/19 20:33:10
generally I prefer to list functions top-down. So
cduvall
2012/06/19 22:21:02
Done.
| |
| 31 type_dict = {} | |
| 32 type_dict['name'] = t.name | |
|
not at google - send to devlin
2012/06/19 20:33:10
"t" -> "type_"
I'd have called "type_dict" just "
cduvall
2012/06/19 22:21:02
Done.
| |
| 33 if t.description: | |
| 34 type_dict['description'] = t.description | |
| 35 type_dict['properties'] = self._GenerateProperties(t.properties) | |
| 36 if getattr(t, 'functions', None): | |
| 37 type_dict['functions'] = self._GenerateFunctions(t.functions) | |
| 38 type_dict['type'] = t.type_.name | |
| 39 if getattr(t, 'item_type', None): | |
| 40 type_dict['item_type'] = self._GenerateType(t.item_type) | |
|
not at google - send to devlin
2012/06/19 20:33:10
It looks like model.py normalises this and always
cduvall
2012/06/19 22:21:02
Done.
| |
| 41 return type_dict | |
| 42 | |
| 43 def _GenerateTypes(self, types): | |
| 44 if len(types) == 0: | |
| 45 return None | |
|
not at google - send to devlin
2012/06/19 20:33:10
hmmmm I'm not sure whether no-types should be norm
cduvall
2012/06/19 22:21:02
Done.
| |
| 46 types_list = [] | |
| 47 for t in types: | |
| 48 types_list.append(self._GenerateType(types[t])) | |
|
not at google - send to devlin
2012/06/19 20:33:10
try to have names like "type_name" rather than "t"
cduvall
2012/06/19 22:21:02
Done.
| |
| 49 return types_list | |
| 50 | |
| 51 def _GenerateCallback(self, c): | |
| 52 callback_dict = {} | |
| 53 callback_dict['name'] = 'callback' | |
| 54 if len(c.params) != 0: | |
|
not at google - send to devlin
2012/06/19 20:33:10
> 0
cduvall
2012/06/19 22:21:02
I just made the params empty if it doesn't have th
| |
| 55 callback_dict['parameters'] = [] | |
| 56 for param in c.params: | |
| 57 callback_dict['parameters'].append(self._GenerateProperty(param)) | |
| 58 return callback_dict | |
| 59 | |
| 60 def _GenerateFunction(self, f): | |
| 61 function_dict = {} | |
| 62 function_dict['name'] = f.name | |
| 63 if f.description: | |
| 64 function_dict['description'] = f.description | |
| 65 if len(f.params) != 0: | |
| 66 function_dict['parameters'] = [] | |
| 67 for param in f.params: | |
| 68 function_dict['parameters'].append(self._GenerateProperty(param)) | |
| 69 if f.callback: | |
| 70 function_dict['callback'] = self._GenerateCallback(f.callback) | |
| 71 return function_dict | |
| 72 | |
| 73 def _GenerateFunctions(self, functions): | |
| 74 if len(functions) == 0: | |
| 75 return None | |
| 76 functions_list = [] | |
| 77 for f in functions: | |
| 78 functions_list.append(self._GenerateFunction(functions[f])) | |
| 79 return functions_list | |
| 80 | |
| 81 def _GenerateProperty(self, p): | |
| 82 property_dict = {} | |
| 83 property_dict['name'] = p.name | |
| 84 property_dict['optional'] = p.optional | |
| 85 if p.description: | |
| 86 property_dict['description'] = p.description | |
| 87 property_dict['type'] = p.type_.name | |
| 88 if getattr(p, 'choices', None): | |
| 89 property_dict['choices'] = [] | |
| 90 for c in p.choices: | |
| 91 property_dict['choices'].append(self._GenerateProperty(p.choices[c])) | |
| 92 if getattr(p, 'ref_type', None): | |
| 93 property_dict['ref_type'] = p.ref_type | |
| 94 if getattr(p, 'item_type', None): | |
| 95 property_dict['item_type'] = self._GenerateType(p.item_type) | |
| 96 property_dict['properties'] = self._GenerateProperties(p.properties) | |
| 97 return property_dict | |
| 98 | |
| 99 def _GenerateProperties(self, properties): | |
| 100 if len(properties) == 0: | |
| 101 return None | |
| 102 properties_list = [] | |
| 103 for p in properties: | |
| 104 properties_list.append(self._GenerateProperty(properties[p])) | |
| 105 return properties_list | |
| 106 | |
| 107 def Generate(self): | |
| 108 d = {} | |
| 109 d['name'] = self._namespace.name | |
| 110 d['types'] = self._GenerateTypes(self._namespace.types) | |
| 111 d['functions'] = self._GenerateFunctions(self._namespace.functions) | |
| 112 d['properties'] = self._GenerateProperties(self._namespace.properties) | |
|
not at google - send to devlin
2012/06/19 20:33:10
return {
'name': self._namespace.name,
'types'
cduvall
2012/06/19 22:21:02
Done.
| |
| 113 return d | |
| OLD | NEW |