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

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

Issue 14125010: Docserver: Add support for viewing docs with a codereview patch applied (Closed) Base URL: https://src.chromium.org/svn/trunk/src/
Patch Set: Created 7 years, 8 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/patch_servlet.py
===================================================================
--- chrome/common/extensions/docs/server2/patch_servlet.py (revision 0)
+++ chrome/common/extensions/docs/server2/patch_servlet.py (revision 0)
@@ -0,0 +1,131 @@
+# Copyright 2013 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
+
+from appengine_url_fetcher import AppEngineUrlFetcher
+from appengine_wrappers import GetAppVersion
+from object_store_creator import ObjectStoreCreator
+#from servlet import Servlet
+import svn_constants
+import url_constants
+
+PATCH_BASEURL = '_patch/'
+PATCH_CHANNEL_NAME = 'trunk'
+CHROMIUM_REPO_BASEURLS = [
+ 'https://src.chromium.org/svn/trunk/src/',
+ 'http://src.chromium.org/svn/trunk/src/',
+ 'svn://svn.chromium.org/chrome/trunk/src',
+ 'https://chromium.googlesource.com/chromium/src.git@master',
+ 'http://git.chromium.org/chromium/src.git@master',
+]
+DOCS_PATHS = [
+ svn_constants.API_PATH,
+ svn_constants.INTRO_PATH,
+ svn_constants.ARTICLE_PATH,
+ svn_constants.PUBLIC_TEMPLATE_PATH,
+ svn_constants.PRIVATE_TEMPLATE_PATH,
+ svn_constants.JSON_PATH,
+ svn_constants.STATIC_PATH
+]
+RIETVELD_ISSUE_JSON = 'api/%s'
+RIETVELD_PATCHSET_JSON = 'api/%s/%s'
+
+class PatchData(object):
+ _object_store = None
+
+ def __init__(self, issue, patchset, files):
+ self.issue = issue
+ self.patchset = patchset
+ self.files = files
+
+ @staticmethod
+ def Create(issue):
+ fetcher = AppEngineUrlFetcher(url_constants.CODEREVIEW_SERVER)
+ object_store = PatchData._GetOrCreateObjectStore()
+ patchset = PatchData._GetPatchsetFromIssue(issue, fetcher, object_store)
+ if patchset is None:
+ return None
+ files = PatchData._GetPatchsetFiles(issue, patchset, fetcher, object_store)
+ if len(files) == 0:
+ return None
+ return PatchData(issue, patchset, files)
+
+ @staticmethod
+ def _GetPatchsetFromIssue(issue, fetcher, object_store):
+ patchset = object_store.Get(issue).Get()
+ if patchset is not None:
+ return patchset
+
+ try:
+ issue_json = json.loads(fetcher.Fetch(
+ RIETVELD_ISSUE_JSON % issue).content)
+ except Exception as e:
+ return None
+
+ if issue_json.get('closed'):
+ return None
+
+ patchsets = issue_json.get('patchsets')
+ if not isinstance(patchsets, list) or len(patchsets) == 0:
+ return None
+
+ if not issue_json.get('base_url') in CHROMIUM_REPO_BASEURLS:
+ return None
+
+ patchset = patchsets[-1]
+ object_store.Set(issue, patchset)
+ return patchset
+
+ @staticmethod
+ def _GetPatchsetFiles(issue, patchset, fetcher, object_store):
+ key = '%s.%s' % (issue, patchset)
+ docs_files = object_store.Get(key).Get()
+ if docs_files is not None:
+ return docs_files
+
+ try:
+ patchset_json = json.loads(fetcher.Fetch(
+ RIETVELD_PATCHSET_JSON % (issue, patchset)).content)
+ except Exception as e:
+ return []
+
+ files = patchset_json.get('files')
+ if files is None or not isinstance(files, dict):
+ return []
+
+ docs_files = []
+ for f in files:
+ f = f.split(svn_constants.EXTENSIONS_PATH + '/', 1)[1]
+ if (f.startswith(svn_constants.DOCS_PATH) or
+ f.startswith(svn_constants.API_PATH)):
+ docs_files.append(f)
+ if len(docs_files) == 0:
+ return []
+
+ object_store.Set(key, docs_files)
+ return docs_files
+
+ @staticmethod
+ def _GetOrCreateObjectStore():
+ if PatchData._object_store is None:
+ PatchData._object_store = (ObjectStoreCreator.SharedFactory(
+ GetAppVersion()).Create(PatchData).Create())
+ return PatchData._object_store
+
+class PatchServlet(object):
+ '''Servlet which renders patched docs.
+ '''
+ @staticmethod
+ def SplitPatchFromPath(path):
+ if path.startswith(PATCH_BASEURL):
+ remaining_path = path.split(PATCH_BASEURL, 1)[1]
+ if not '/' in remaining_path:
+ return (None, None, None)
+ issue, real_path = remaining_path.split('/', 1)
+ if not issue.isdigit() or len(real_path) == 0:
+ return (None, None, None)
+ return (PATCH_CHANNEL_NAME, issue, real_path)
+ else:
+ return (None, None, None)
Property changes on: chrome/common/extensions/docs/server2/patch_servlet.py
___________________________________________________________________
Added: svn:eol-style
+ LF

Powered by Google App Engine
This is Rietveld 408576698