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