Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/chrome_version_data_source.py |
| diff --git a/chrome/common/extensions/docs/server2/chrome_version_data_source.py b/chrome/common/extensions/docs/server2/chrome_version_data_source.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5af6254438b6e43e064f69e296f8081069df083f |
| --- /dev/null |
| +++ b/chrome/common/extensions/docs/server2/chrome_version_data_source.py |
| @@ -0,0 +1,79 @@ |
| +# 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 logging |
|
cduvall
2013/03/27 23:01:10
nit: newline between logging and object_store
epeterson
2013/03/28 00:53:51
Done.
|
| +import object_store |
| + |
| +class ChromeVersionDataSource: |
| + """Generates and stores API data sources corresponding to the latest branch |
| + number for a given version of Chrome. |
| + |
| + Accesses data found on omahaproxy in order to link branch numbers to version |
| + numbers. |
| + """ |
| + |
| + def __init__(self, |
| + base_path, |
| + fetcher, |
| + object_store, |
| + create_objects_for_branch): |
| + self._base_path = base_path |
| + self._fetcher = fetcher |
| + self._object_store = object_store |
| + self._create_objects_for_branch = create_objects_for_branch |
| + |
| + def _GetBranchNumberForVersion(self, version_number): |
| + """Returns the most recent branch number for a given chrome version number |
| + using data stored on omahaproxy (see url_constants) |
| + """ |
| + if version_number == 'trunk': |
| + return 'trunk' |
| + |
| + branch = self._object_store.Get( |
| + version_number, |
| + object_store.CHROME_VERSION_DATA_SOURCE).Get() |
| + |
| + if branch is not None: |
| + return branch |
| + |
| + try: |
| + version_json = json.loads(self._fetcher.Fetch(self._base_path).content) |
| + except Exception as e: |
| + # If omahaproxy is having problems, report it. |
| + logging.error('Could not fetch data at Omaha Proxy: %s' % (e)) |
| + return None |
| + |
| + # Here, entry['title'] looks like: 'title - version#.#.branch#.#' |
| + for entry in version_json['events']: |
| + version_title = entry['title'].split(' - ')[1].split('.') |
| + if version_title[0] == version_number: |
| + self._object_store.Set(version_number, |
| + version_title[2], |
| + object_store.CHROME_VERSION_DATA_SOURCE) |
| + return version_title[2] |
| + return None |
| + |
| + def GetLatestVersionNumber(self): |
| + try: |
| + version_json = json.loads(self._fetcher.Fetch(self._base_path).content) |
| + except Exception as e: |
| + logging.error('Could not fetch data at Omaha Proxy: %s' % (e)) |
| + return None |
| + |
| + latest_version = 0 |
| + for entry in version_json['events']: |
|
cduvall
2013/03/27 23:01:10
why 'events' here?
epeterson
2013/03/28 00:53:51
events is the JSON key for a list of all the separ
|
| + version_title = entry['title'].split(' - ')[1].split('.') |
| + version = int(version_title[0]) |
| + if version > latest_version: |
| + latest_version = version |
| + return latest_version |
| + |
| + def GetDataSourceForVersion(self, version_number): |
| + """Returns an api data source for the most recent branch number |
| + corresponding to a given version number |
| + """ |
| + branch_number = self._GetBranchNumberForVersion(version_number) |
| + branch_api_data_source = self._create_objects_for_branch(branch_number) |
| + return branch_api_data_source |