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

Side by Side Diff: chrome/common/extensions/docs/server2/github_file_system.py

Issue 10825067: Extensions Docs Server: Apps samples page (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: no more zip file Created 8 years, 4 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import json
6
7 import file_system
8 from future import Future
9
10 class GithubFileSystem(file_system.FileSystem):
11 """Class to fetch resources from github.
12 """
13 def __init__(self, fetcher):
14 self._fetcher = fetcher
15
16 def Read(self, paths, binary=False):
17 return Future(delegate=_AsyncFetchFuture(paths, self._fetcher, binary))
18
19 def Stat(self, path):
20 if not path.strip('/'):
21 return self.StatInfo(0)
22 parts = path.strip('/').rsplit('/', 1)
23 if len(parts) == 1:
24 name = parts[0]
25 directory = '/'
26 else:
27 name = parts[-1]
28 directory = parts[-2]
29 json_ = json.loads(self._fetcher.Fetch(directory).content)
30 for item in json_:
31 if item['name'] == name:
32 return self.StatInfo(item['sha'])
33
34 def CheckStat(self, version1, version2):
35 return version1 != version2
36
37 class _AsyncFetchFuture(object):
38 def __init__(self, paths, fetcher, binary):
39 # A list of tuples of the form (path, Future).
40 self._fetches = []
41 self._value = {}
42 self._error = None
43 self._fetches = [(path, fetcher.FetchAsync(path.strip('/')))
44 for path in paths]
45 self._binary = binary
46
47 def _ListDir(self, directory):
48 dir_json = json.loads(directory)
49 files = []
50 for entry in dir_json:
51 if entry['type'] == 'dir':
52 files.append(entry['name'] + '/')
53 else:
54 files.append(entry['name'])
55 return files
56
57 def _GetFile(self, data, binary, path):
58 file_json = json.loads(data)
59 file_contents = file_json['content'].decode(file_json['encoding'])
60 if binary:
61 return file_contents
62 return file_system._ProcessFileData(file_contents, path)
63
64 def Get(self):
65 for path, future in self._fetches:
66 result = future.Get()
67 if result.status_code == 404:
68 self._value[path] = None
69 elif path.endswith('/'):
70 self._value[path] = self._ListDir(result.content)
71 else:
72 self._value[path] = self._GetFile(result.content, self._binary, path)
73 if self._error is not None:
74 raise self._error
75 return self._value
76
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698