Index: chrome/common/extensions/docs/server2/availability_data_source.py |
diff --git a/chrome/common/extensions/docs/server2/availability_data_source.py b/chrome/common/extensions/docs/server2/availability_data_source.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a40f0abb81b5072dd8fec9efbd1dd56f55b2c431 |
--- /dev/null |
+++ b/chrome/common/extensions/docs/server2/availability_data_source.py |
@@ -0,0 +1,83 @@ |
+# Copyright (c) 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. |
+ |
+from file_system import FileNotFoundError |
+import object_store |
+ |
+class AvailabilityDataSource: |
+ """Uses API data sources generated by a ChromeVersionDataSource in order to |
+ search the filesystem for the earliest existence of a specified API throughout |
+ the different versions of Chrome; this constitutes an API's availability. |
+ """ |
+ |
+ def __init__(self, version_data_source, version, object_store): |
+ self._version_data_source = version_data_source |
+ self._version = version |
+ self._object_store = object_store |
+ |
+ def _CheckExistence(self, api_name, version): |
+ """Tries to 'get' an api from the filesystem, and reports whether or not it |
+ was successful. |
+ """ |
+ api_data_source = self._version_data_source.GetDataSourceForVersion( |
+ str(version)) |
+ try: |
+ api_data_source.TryGet(api_name) |
+ except FileNotFoundError: |
+ try: |
+ api_data_source.TryGet(api_name, try_non_unix=True) |
+ except FileNotFoundError: |
+ return False |
+ return True |
+ |
+ def FindEarliestAvailability(self, api_name): |
+ """Searches in descending order through filesystem caches tied to specific |
+ chrome version numbers and looks for the existence of a specified api, |
+ |api_name|. When an api is not found, returns the previous version number |
+ (the last known version where the api was found). |
+ """ |
+ availability = self._object_store.Get( |
+ api_name, |
+ object_store.AVAILABILITY_DATA_SOURCE).Get() |
+ |
+ if availability is not None: |
+ return availability |
+ elif availability is False: |
+ return None |
+ |
+ if self._version == 'trunk': |
+ if not self._CheckExistence(api_name, 'trunk'): |
+ # If the API wasn't found in trunk, then it isn't currently available. |
+ self._object_store.Set(api_name, |
+ False, |
+ object_store.AVAILABILITY_DATA_SOURCE) |
+ return None |
+ |
+ if self._version == 'trunk': |
+ latest_version = self._version_data_source.GetLatestVersionNumber() |
+ version_int = latest_version |
+ else: |
+ version_int = int(self._version) |
+ |
+ # Extension APIs are not present in the filesystem before version 18. |
+ while version_int >= 18: |
+ if not self._CheckExistence(api_name, version_int): |
+ break |
+ version_int -= 1 |
+ |
+ version_int += 1 |
+ # If the version number is greater than the current version of Chrome, then |
+ # the API was not found in the current version. |
+ if self._version == 'trunk': |
+ if version_int > latest_version: |
+ availability = 'trunk' |
+ else: |
+ availability = str(version_int) |
+ elif version_int <= int(self._version): |
+ availability = str(version_int) |
+ |
+ self._object_store.Set(api_name, |
+ availability, |
+ object_store.AVAILABILITY_DATA_SOURCE) |
+ return availability |