Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 copy | 5 import copy |
| 6 import logging | 6 import logging |
| 7 import os | 7 import os |
| 8 | 8 |
| 9 import third_party.json_schema_compiler.model as model | 9 import third_party.json_schema_compiler.model as model |
| 10 | 10 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 def _FormatValue(value): | 37 def _FormatValue(value): |
| 38 """Inserts commas every three digits for integer values. It is magic. | 38 """Inserts commas every three digits for integer values. It is magic. |
| 39 """ | 39 """ |
| 40 s = str(value) | 40 s = str(value) |
| 41 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) | 41 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) |
| 42 | 42 |
| 43 class HandlebarDictGenerator(object): | 43 class HandlebarDictGenerator(object): |
| 44 """Uses a Model from the JSON Schema Compiler and generates a dict that | 44 """Uses a Model from the JSON Schema Compiler and generates a dict that |
| 45 a Handlebar template can use for a data source. | 45 a Handlebar template can use for a data source. |
| 46 """ | 46 """ |
| 47 def __init__(self, json): | 47 def __init__(self, json, samples): |
| 48 self._samples = samples | |
| 48 clean_json = copy.deepcopy(json) | 49 clean_json = copy.deepcopy(json) |
| 49 _RemoveNoDocs(clean_json) | 50 _RemoveNoDocs(clean_json) |
| 50 try: | 51 try: |
| 51 self._namespace = model.Namespace(clean_json, clean_json['namespace']) | 52 self._namespace = model.Namespace(clean_json, clean_json['namespace']) |
| 52 except Exception as e: | 53 except Exception as e: |
| 53 logging.info(e) | 54 logging.info(e) |
| 54 | 55 |
| 55 def Generate(self): | 56 def Generate(self): |
| 56 try: | 57 try: |
| 57 return { | 58 return { |
| 58 'name': self._namespace.name, | 59 'name': self._namespace.name, |
| 59 'types': map(self._GenerateType, self._namespace.types.values()), | 60 'types': map(self._GenerateType, self._namespace.types.values()), |
| 60 'functions': self._GenerateFunctions(self._namespace.functions), | 61 'functions': self._GenerateFunctions(self._namespace.functions), |
| 61 'events': map(self._GenerateEvent, self._namespace.events.values()), | 62 'events': map(self._GenerateEvent, self._namespace.events.values()), |
| 62 'properties': self._GenerateProperties(self._namespace.properties) | 63 'properties': self._GenerateProperties(self._namespace.properties), |
| 64 'samples': self._FilterSamples(self._samples, self._namespace.name) | |
| 63 } | 65 } |
| 64 except Exception as e: | 66 except Exception as e: |
| 65 logging.info(e) | 67 logging.info(e) |
| 66 | 68 |
| 69 def _FilterSamples(self, samples, api_name): | |
| 70 return_samples = [] | |
| 71 for sample in samples: | |
| 72 for api in sample['api_calls']: | |
| 73 if '.' + api_name + '.' in api['name']: | |
| 74 return_samples.append(sample) | |
| 75 break | |
| 76 return return_samples | |
|
not at google - send to devlin
2012/07/30 12:23:03
Perhaps a list comprehension would be more concise
chebert
2012/07/31 22:41:33
Done.
| |
| 77 | |
| 67 def _GenerateType(self, type_): | 78 def _GenerateType(self, type_): |
| 68 type_dict = { | 79 type_dict = { |
| 69 'name': type_.name, | 80 'name': type_.name, |
| 70 'description': type_.description, | 81 'description': type_.description, |
| 71 'properties': self._GenerateProperties(type_.properties), | 82 'properties': self._GenerateProperties(type_.properties), |
| 72 'functions': self._GenerateFunctions(type_.functions), | 83 'functions': self._GenerateFunctions(type_.functions), |
| 73 'events': map(self._GenerateEvent, type_.events.values()) | 84 'events': map(self._GenerateEvent, type_.events.values()) |
| 74 } | 85 } |
| 75 self._RenderTypeInformation(type_, type_dict) | 86 self._RenderTypeInformation(type_, type_dict) |
| 76 return type_dict | 87 return type_dict |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 elif property_.type_ == model.PropertyType.ENUM: | 171 elif property_.type_ == model.PropertyType.ENUM: |
| 161 dst_dict['enum_values'] = [] | 172 dst_dict['enum_values'] = [] |
| 162 for enum_value in property_.enum_values: | 173 for enum_value in property_.enum_values: |
| 163 dst_dict['enum_values'].append({'name': enum_value}) | 174 dst_dict['enum_values'].append({'name': enum_value}) |
| 164 if len(dst_dict['enum_values']) > 0: | 175 if len(dst_dict['enum_values']) > 0: |
| 165 dst_dict['enum_values'][-1]['last'] = True | 176 dst_dict['enum_values'][-1]['last'] = True |
| 166 elif property_.instance_of: | 177 elif property_.instance_of: |
| 167 dst_dict['simple_type'] = property_.instance_of.lower() | 178 dst_dict['simple_type'] = property_.instance_of.lower() |
| 168 else: | 179 else: |
| 169 dst_dict['simple_type'] = property_.type_.name.lower() | 180 dst_dict['simple_type'] = property_.type_.name.lower() |
| OLD | NEW |