| Index: chrome/common/extensions/docs/server2/github_file_system.py
|
| diff --git a/chrome/common/extensions/docs/server2/github_file_system.py b/chrome/common/extensions/docs/server2/github_file_system.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..aeff81b0883f656e7d76437cf1bca86bdaa169c7
|
| --- /dev/null
|
| +++ b/chrome/common/extensions/docs/server2/github_file_system.py
|
| @@ -0,0 +1,76 @@
|
| +# 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 file_system
|
| +from future import Future
|
| +
|
| +class GithubFileSystem(file_system.FileSystem):
|
| + """Class to fetch resources from github.
|
| + """
|
| + def __init__(self, fetcher):
|
| + self._fetcher = fetcher
|
| +
|
| + def Read(self, paths, binary=False):
|
| + return Future(delegate=_AsyncFetchFuture(paths, self._fetcher, binary))
|
| +
|
| + def Stat(self, path):
|
| + if not path.strip('/'):
|
| + return self.StatInfo(0)
|
| + parts = path.strip('/').rsplit('/', 1)
|
| + if len(parts) == 1:
|
| + name = parts[0]
|
| + directory = '/'
|
| + else:
|
| + name = parts[-1]
|
| + directory = parts[-2]
|
| + json_ = json.loads(self._fetcher.Fetch(directory).content)
|
| + for item in json_:
|
| + if item['name'] == name:
|
| + return self.StatInfo(item['sha'])
|
| +
|
| + def CheckStat(self, version1, version2):
|
| + return version1 != version2
|
| +
|
| +class _AsyncFetchFuture(object):
|
| + def __init__(self, paths, fetcher, binary):
|
| + # A list of tuples of the form (path, Future).
|
| + self._fetches = []
|
| + self._value = {}
|
| + self._error = None
|
| + self._fetches = [(path, fetcher.FetchAsync(path.strip('/')))
|
| + for path in paths]
|
| + self._binary = binary
|
| +
|
| + def _ListDir(self, directory):
|
| + dir_json = json.loads(directory)
|
| + files = []
|
| + for entry in dir_json:
|
| + if entry['type'] == 'dir':
|
| + files.append(entry['name'] + '/')
|
| + else:
|
| + files.append(entry['name'])
|
| + return files
|
| +
|
| + def _GetFile(self, data, binary, path):
|
| + file_json = json.loads(data)
|
| + file_contents = file_json['content'].decode(file_json['encoding'])
|
| + if binary:
|
| + return file_contents
|
| + return file_system._ProcessFileData(file_contents, path)
|
| +
|
| + def Get(self):
|
| + for path, future in self._fetches:
|
| + result = future.Get()
|
| + if result.status_code == 404:
|
| + self._value[path] = None
|
| + elif path.endswith('/'):
|
| + self._value[path] = self._ListDir(result.content)
|
| + else:
|
| + self._value[path] = self._GetFile(result.content, self._binary, path)
|
| + if self._error is not None:
|
| + raise self._error
|
| + return self._value
|
| +
|
|
|