OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Class for parsing metadata about extension samples.""" | 6 """Class for parsing metadata about extension samples.""" |
7 | 7 |
8 import locale | 8 import locale |
9 import os | 9 import os |
10 import os.path | 10 import os.path |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 try: | 50 try: |
51 json_obj = json.load(json_file, encoding) | 51 json_obj = json.load(json_file, encoding) |
52 except ValueError, msg: | 52 except ValueError, msg: |
53 raise Exception("Failed to parse JSON out of file %s: %s" % (path, msg)) | 53 raise Exception("Failed to parse JSON out of file %s: %s" % (path, msg)) |
54 finally: | 54 finally: |
55 json_file.close() | 55 json_file.close() |
56 | 56 |
57 return json_obj | 57 return json_obj |
58 | 58 |
59 class ApiManifest(object): | 59 class ApiManifest(object): |
60 """ Represents the list of API methods contained in extension_api.json """ | 60 """ Represents the list of API methods contained in the extension API JSON """ |
61 | 61 |
62 _MODULE_DOC_KEYS = ['functions', 'events'] | 62 _MODULE_DOC_KEYS = ['functions', 'events'] |
63 """ Keys which may be passed to the _parseModuleDocLinksByKey method.""" | 63 """ Keys which may be passed to the _parseModuleDocLinksByKey method.""" |
64 | 64 |
65 def __init__(self, manifest_paths): | 65 def __init__(self, manifest_paths): |
66 """ Read the supplied manifest file and parse its contents. | 66 """ Read the supplied manifest file and parse its contents. |
67 | 67 |
68 Args: | 68 Args: |
69 manifest_paths: Array of paths to API schemas (extension_api.json etc). | 69 manifest_paths: Array of paths to API schemas. |
70 """ | 70 """ |
71 self._manifest = []; | 71 self._manifest = []; |
72 for path in manifest_paths: | 72 for path in manifest_paths: |
73 self._manifest.extend(parse_json_file(path)) | 73 self._manifest.extend(parse_json_file(path)) |
74 | 74 |
75 def _getDocLink(self, method, hashprefix): | 75 def _getDocLink(self, method, hashprefix): |
76 """ | 76 """ |
77 Given an API method, return a partial URL corresponding to the doc | 77 Given an API method, return a partial URL corresponding to the doc |
78 file for that method. | 78 file for that method. |
79 | 79 |
80 Args: | 80 Args: |
81 method: A string like 'chrome.foo.bar' or 'chrome.experimental.foo.onBar' | 81 method: A string like 'chrome.foo.bar' or 'chrome.experimental.foo.onBar' |
82 hashprefix: The prefix to put in front of hash links - 'method' for | 82 hashprefix: The prefix to put in front of hash links - 'method' for |
83 methods and 'event' for events. | 83 methods and 'event' for events. |
84 | 84 |
85 Returns: | 85 Returns: |
86 A string like 'foo.html#method-bar' or 'experimental.foo.html#event-onBar' | 86 A string like 'foo.html#method-bar' or 'experimental.foo.html#event-onBar' |
87 """ | 87 """ |
88 urlpattern = '%%s.html#%s-%%s' % hashprefix | 88 urlpattern = '%%s.html#%s-%%s' % hashprefix |
89 urlparts = tuple(method.replace('chrome.', '').rsplit('.', 1)) | 89 urlparts = tuple(method.replace('chrome.', '').rsplit('.', 1)) |
90 return urlpattern % urlparts | 90 return urlpattern % urlparts |
91 | 91 |
92 def _parseModuleDocLinksByKey(self, module, key): | 92 def _parseModuleDocLinksByKey(self, module, key): |
93 """ | 93 """ |
94 Given a specific API module, returns a dict of methods or events mapped to | 94 Given a specific API module, returns a dict of methods or events mapped to |
95 documentation URLs. | 95 documentation URLs. |
96 | 96 |
97 Args: | 97 Args: |
98 module: The data in extension_api.json corresponding to a single module. | 98 module: The data in the extension API JSON for a single module. |
99 key: A key belonging to _MODULE_DOC_KEYS to determine which set of | 99 key: A key belonging to _MODULE_DOC_KEYS to determine which set of |
100 methods to parse, and what kind of documentation URL to generate. | 100 methods to parse, and what kind of documentation URL to generate. |
101 | 101 |
102 Returns: | 102 Returns: |
103 A dict of extension methods mapped to file and hash URL parts for the | 103 A dict of extension methods mapped to file and hash URL parts for the |
104 corresponding documentation links, like: | 104 corresponding documentation links, like: |
105 { | 105 { |
106 "chrome.tabs.remove": "tabs.html#method-remove", | 106 "chrome.tabs.remove": "tabs.html#method-remove", |
107 "chrome.tabs.onDetached" : "tabs.html#event-onDetatched" | 107 "chrome.tabs.onDetached" : "tabs.html#event-onDetatched" |
108 } | 108 } |
(...skipping 23 matching lines...) Expand all Loading... |
132 def getModuleNames(self): | 132 def getModuleNames(self): |
133 """ Returns the names of individual modules in the API. | 133 """ Returns the names of individual modules in the API. |
134 | 134 |
135 Returns: | 135 Returns: |
136 The namespace """ | 136 The namespace """ |
137 # Exclude modules with a "nodoc" property. | 137 # Exclude modules with a "nodoc" property. |
138 return set(module['namespace'].encode() for module in self._manifest | 138 return set(module['namespace'].encode() for module in self._manifest |
139 if "nodoc" not in module) | 139 if "nodoc" not in module) |
140 | 140 |
141 def getDocumentationLinks(self): | 141 def getDocumentationLinks(self): |
142 """ Parses the extension_api.json manifest and returns a dict of all | 142 """ Parses the extension API JSON manifest and returns a dict of all |
143 events and methods for every module, mapped to relative documentation links. | 143 events and methods for every module, mapped to relative documentation links. |
144 | 144 |
145 Returns: | 145 Returns: |
146 A dict of methods/events => partial doc links for every module. | 146 A dict of methods/events => partial doc links for every module. |
147 """ | 147 """ |
148 api_dict = {} | 148 api_dict = {} |
149 for module in self._manifest: | 149 for module in self._manifest: |
150 api_dict.update(self._parseModuleDocLinksByKey(module, 'functions')) | 150 api_dict.update(self._parseModuleDocLinksByKey(module, 'functions')) |
151 api_dict.update(self._parseModuleDocLinksByKey(module, 'events')) | 151 api_dict.update(self._parseModuleDocLinksByKey(module, 'events')) |
152 return api_dict | 152 return api_dict |
(...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 zip_file.write(abspath, relpath) | 715 zip_file.write(abspath, relpath) |
716 if file == 'manifest.json': | 716 if file == 'manifest.json': |
717 info = zip_file.getinfo(zip_manifest_path) | 717 info = zip_file.getinfo(zip_manifest_path) |
718 info.comment = self['source_hash'] | 718 info.comment = self['source_hash'] |
719 except RuntimeError, msg: | 719 except RuntimeError, msg: |
720 raise Exception("Could not write zip at %s: %s" % (zip_path, msg)) | 720 raise Exception("Could not write zip at %s: %s" % (zip_path, msg)) |
721 finally: | 721 finally: |
722 zip_file.close() | 722 zip_file.close() |
723 | 723 |
724 return self._get_relative_zip_path() | 724 return self._get_relative_zip_path() |
OLD | NEW |