Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3471)

Unified Diff: chrome/common/extensions/docs/server2/samples_data_source.py

Issue 10689144: Extensions Docs Server: Samples zip files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: clean up and tests Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/docs/server2/samples_data_source.py
diff --git a/chrome/common/extensions/docs/server2/samples_data_source.py b/chrome/common/extensions/docs/server2/samples_data_source.py
new file mode 100644
index 0000000000000000000000000000000000000000..76fbbac0963644ed633e0eb99e228b7fcf7ee855
--- /dev/null
+++ b/chrome/common/extensions/docs/server2/samples_data_source.py
@@ -0,0 +1,71 @@
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import json
+import re
+
+class SamplesDataSource(object):
+ """Constructs a list of samples and their respective files and api calls.
+ """
+ def __init__(self, fetcher, cache_builder, samples_path):
+ self._fetcher = fetcher
+ self._cache = cache_builder.build(self._MakeSamplesList)
+ self._samples_path = samples_path
+
+ def _GetApiItems(self, api_items, js_file):
+ return set(re.findall('(chrome\.[a-zA-Z0-9\.]+)',
+ self._fetcher.FetchResource(js_file).content))
+
+ def _MakeApiLink(self, prefix, item):
+ api, name = item.replace('chrome.', '').split('.', 1)
+ return api + '.html#' + prefix + '-' + name
+
+ def _GetDataFromManifest(self, path):
+ manifest = self._fetcher.FetchResource(path + '/manifest.json').content
+ manifest_json = json.loads(manifest)
+ return (manifest_json.get('name'), manifest_json.get('description'))
+
+ def _MakeSamplesList(self, files):
+ samples_list = []
+ for filename in files:
+ if filename.rsplit('/')[-1] != 'manifest.json':
+ continue
+ # This is a little hacky, but it makes a sample page.
+ sample_path = filename.rsplit('/', 1)[-2]
+ sample_files = filter(lambda x: x.startswith(sample_path + '/'), files)
+ api_items = set([])
+ for file_ in sample_files:
+ if file_.endswith('.js'):
+ api_items.update(self._GetApiItems(api_items, file_))
+
+ api_calls = []
+ for item in api_items:
+ if len(item.split('.')) < 3:
+ continue
+ if item.endswith('.addListener'):
+ item = item.replace('.addListener', '')
+ api_calls.append({
+ 'name': item,
+ 'link': self._MakeApiLink('event', item)
+ })
+ else:
+ api_calls.append({
+ 'name': item,
+ 'link': self._MakeApiLink('method', item)
+ })
+ name, description = self._GetDataFromManifest(sample_path)
+ samples_list.append({
+ 'name': name,
+ 'description': description,
+ 'path': sample_path.split('/', 1)[1],
+ 'files': [f.replace(sample_path + '/', '') for f in sample_files],
+ 'api_calls': api_calls
+ })
+ return samples_list
+
+ def __getitem__(self, key):
+ return self.get(key)
+
+ def get(self, key):
+ return self._cache.getFromFileListing('docs/' + self._samples_path, True)
« no previous file with comments | « chrome/common/extensions/docs/server2/local_fetcher_test.py ('k') | chrome/common/extensions/docs/server2/server_instance.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698