| 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 | |
| 7 import os | 6 import os |
| 8 | 7 |
| 9 from docs_server_utils import GetLinkToRefType | 8 from docs_server_utils import GetLinkToRefType |
| 10 import third_party.json_schema_compiler.model as model | 9 import third_party.json_schema_compiler.model as model |
| 11 | 10 |
| 12 def _RemoveNoDocs(item): | 11 def _RemoveNoDocs(item): |
| 13 if type(item) == dict: | 12 if type(item) == dict: |
| 14 if item.get('nodoc', False): | 13 if item.get('nodoc', False): |
| 15 return True | 14 return True |
| 16 for key, value in item.items(): | 15 for key, value in item.items(): |
| (...skipping 10 matching lines...) Expand all Loading... |
| 27 """ | 26 """ |
| 28 s = str(value) | 27 s = str(value) |
| 29 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) | 28 return ','.join([s[max(0, i - 3):i] for i in range(len(s), 0, -3)][::-1]) |
| 30 | 29 |
| 31 class HandlebarDictGenerator(object): | 30 class HandlebarDictGenerator(object): |
| 32 """Uses a Model from the JSON Schema Compiler and generates a dict that | 31 """Uses a Model from the JSON Schema Compiler and generates a dict that |
| 33 a Handlebar template can use for a data source. | 32 a Handlebar template can use for a data source. |
| 34 """ | 33 """ |
| 35 def __init__(self, json): | 34 def __init__(self, json): |
| 36 clean_json = copy.deepcopy(json) | 35 clean_json = copy.deepcopy(json) |
| 37 _RemoveNoDocs(clean_json) | 36 if _RemoveNoDocs(clean_json): |
| 38 try: | 37 self._namespace = None |
| 38 else: |
| 39 self._namespace = model.Namespace(clean_json, clean_json['namespace']) | 39 self._namespace = model.Namespace(clean_json, clean_json['namespace']) |
| 40 except Exception as e: | |
| 41 logging.error(e) | |
| 42 raise | |
| 43 | 40 |
| 44 def _StripPrefix(self, name): | 41 def _StripPrefix(self, name): |
| 45 if name.startswith(self._namespace.name + '.'): | 42 if name.startswith(self._namespace.name + '.'): |
| 46 return name[len(self._namespace.name + '.'):] | 43 return name[len(self._namespace.name + '.'):] |
| 47 return name | 44 return name |
| 48 | 45 |
| 49 def _FormatDescription(self, description): | 46 def _FormatDescription(self, description): |
| 50 if description is None or '$ref:' not in description: | 47 if description is None or '$ref:' not in description: |
| 51 return description | 48 return description |
| 52 refs = description.split('$ref:') | 49 refs = description.split('$ref:') |
| 53 formatted_description = [refs[0]] | 50 formatted_description = [refs[0]] |
| 54 for ref in refs[1:]: | 51 for ref in refs[1:]: |
| 55 parts = ref.split(' ', 1) | 52 parts = ref.split(' ', 1) |
| 56 if len(parts) == 1: | 53 if len(parts) == 1: |
| 57 ref = parts[0] | 54 ref = parts[0] |
| 58 rest = '' | 55 rest = '' |
| 59 else: | 56 else: |
| 60 ref, rest = parts | 57 ref, rest = parts |
| 61 rest = ' ' + rest | 58 rest = ' ' + rest |
| 62 if not ref[-1].isalnum(): | 59 if not ref[-1].isalnum(): |
| 63 rest = ref[-1] + rest | 60 rest = ref[-1] + rest |
| 64 ref = ref[:-1] | 61 ref = ref[:-1] |
| 65 ref_dict = GetLinkToRefType(self._namespace.name, ref) | 62 ref_dict = GetLinkToRefType(self._namespace.name, ref) |
| 66 formatted_description.append('<a href="%(href)s">%(text)s</a>%(rest)s' % | 63 formatted_description.append('<a href="%(href)s">%(text)s</a>%(rest)s' % |
| 67 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) | 64 { 'href': ref_dict['href'], 'text': ref_dict['text'], 'rest': rest }) |
| 68 return ''.join(formatted_description) | 65 return ''.join(formatted_description) |
| 69 | 66 |
| 70 def Generate(self, samples): | 67 def Generate(self, samples): |
| 71 try: | 68 if self._namespace is None: |
| 72 return { | 69 return { 'samples': samples } |
| 73 'name': self._namespace.name, | 70 return { |
| 74 'types': map(self._GenerateType, self._namespace.types.values()), | 71 'name': self._namespace.name, |
| 75 'functions': self._GenerateFunctions(self._namespace.functions), | 72 'types': map(self._GenerateType, self._namespace.types.values()), |
| 76 'events': map(self._GenerateEvent, self._namespace.events.values()), | 73 'functions': self._GenerateFunctions(self._namespace.functions), |
| 77 'properties': self._GenerateProperties(self._namespace.properties), | 74 'events': map(self._GenerateEvent, self._namespace.events.values()), |
| 78 'samples': samples, | 75 'properties': self._GenerateProperties(self._namespace.properties), |
| 79 } | 76 'samples': samples, |
| 80 except Exception as e: | 77 } |
| 81 logging.error(e) | |
| 82 raise | |
| 83 | 78 |
| 84 def _GenerateType(self, type_): | 79 def _GenerateType(self, type_): |
| 85 type_dict = { | 80 type_dict = { |
| 86 'name': self._StripPrefix(type_.name), | 81 'name': self._StripPrefix(type_.name), |
| 87 'description': self._FormatDescription(type_.description), | 82 'description': self._FormatDescription(type_.description), |
| 88 'properties': self._GenerateProperties(type_.properties), | 83 'properties': self._GenerateProperties(type_.properties), |
| 89 'functions': self._GenerateFunctions(type_.functions), | 84 'functions': self._GenerateFunctions(type_.functions), |
| 90 'events': map(self._GenerateEvent, type_.events.values()) | 85 'events': map(self._GenerateEvent, type_.events.values()) |
| 91 } | 86 } |
| 92 self._RenderTypeInformation(type_, type_dict) | 87 self._RenderTypeInformation(type_, type_dict) |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 elif property_.type_ == model.PropertyType.ENUM: | 172 elif property_.type_ == model.PropertyType.ENUM: |
| 178 dst_dict['enum_values'] = [] | 173 dst_dict['enum_values'] = [] |
| 179 for enum_value in property_.enum_values: | 174 for enum_value in property_.enum_values: |
| 180 dst_dict['enum_values'].append({'name': enum_value}) | 175 dst_dict['enum_values'].append({'name': enum_value}) |
| 181 if len(dst_dict['enum_values']) > 0: | 176 if len(dst_dict['enum_values']) > 0: |
| 182 dst_dict['enum_values'][-1]['last'] = True | 177 dst_dict['enum_values'][-1]['last'] = True |
| 183 elif property_.instance_of: | 178 elif property_.instance_of: |
| 184 dst_dict['simple_type'] = property_.instance_of.lower() | 179 dst_dict['simple_type'] = property_.instance_of.lower() |
| 185 else: | 180 else: |
| 186 dst_dict['simple_type'] = property_.type_.name.lower() | 181 dst_dict['simple_type'] = property_.type_.name.lower() |
| OLD | NEW |