OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 from future import Gettable, Future | 5 from future import Gettable, Future |
6 | 6 |
7 | 7 |
8 class FileNotFoundError(Exception): | 8 class FileNotFoundError(Exception): |
9 '''Raised when a file isn't found for read or stat. | 9 '''Raised when a file isn't found for read or stat. |
10 ''' | 10 ''' |
11 def __init__(self, filename): | 11 def __init__(self, filename): |
12 Exception.__init__(self, filename) | 12 Exception.__init__(self, filename) |
13 | 13 |
| 14 |
14 class FileSystemError(Exception): | 15 class FileSystemError(Exception): |
15 '''Raised on when there are errors reading or statting files, such as a | 16 '''Raised on when there are errors reading or statting files, such as a |
16 network timeout. | 17 network timeout. |
17 ''' | 18 ''' |
18 def __init__(self, filename): | 19 def __init__(self, filename): |
19 Exception.__init__(self, filename) | 20 Exception.__init__(self, filename) |
20 | 21 |
| 22 |
21 class StatInfo(object): | 23 class StatInfo(object): |
22 '''The result of calling Stat on a FileSystem. | 24 '''The result of calling Stat on a FileSystem. |
23 ''' | 25 ''' |
24 def __init__(self, version, child_versions=None): | 26 def __init__(self, version, child_versions=None): |
25 self.version = version | 27 self.version = version |
26 self.child_versions = child_versions | 28 self.child_versions = child_versions |
27 | 29 |
28 def __eq__(self, other): | 30 def __eq__(self, other): |
29 return (isinstance(other, StatInfo) and | 31 return (isinstance(other, StatInfo) and |
30 self.version == other.version and | 32 self.version == other.version and |
31 self.child_versions == other.child_versions) | 33 self.child_versions == other.child_versions) |
32 | 34 |
33 def __ne__(self, other): | 35 def __ne__(self, other): |
34 return not (self == other) | 36 return not (self == other) |
35 | 37 |
36 def __str__(self): | 38 def __str__(self): |
37 return '{version: %s, child_versions: %s}' % (self.version, | 39 return '{version: %s, child_versions: %s}' % (self.version, |
38 self.child_versions) | 40 self.child_versions) |
39 | 41 |
40 def __repr__(self): | 42 def __repr__(self): |
41 return str(self) | 43 return str(self) |
42 | 44 |
| 45 |
43 def ToUnicode(data): | 46 def ToUnicode(data): |
44 '''Returns the str |data| as a unicode object. It's expected to be utf8, but | 47 '''Returns the str |data| as a unicode object. It's expected to be utf8, but |
45 there are also latin-1 encodings in there for some reason. Fall back to that. | 48 there are also latin-1 encodings in there for some reason. Fall back to that. |
46 ''' | 49 ''' |
47 try: | 50 try: |
48 return unicode(data, 'utf-8') | 51 return unicode(data, 'utf-8') |
49 except: | 52 except: |
50 return unicode(data, 'latin-1') | 53 return unicode(data, 'latin-1') |
51 | 54 |
| 55 |
52 class FileSystem(object): | 56 class FileSystem(object): |
53 '''A FileSystem interface that can read files and directories. | 57 '''A FileSystem interface that can read files and directories. |
54 ''' | 58 ''' |
55 def Read(self, paths, binary=False): | 59 def Read(self, paths, binary=False): |
56 '''Reads each file in paths and returns a dictionary mapping the path to the | 60 '''Reads each file in paths and returns a dictionary mapping the path to the |
57 contents. If a path in paths ends with a '/', it is assumed to be a | 61 contents. If a path in paths ends with a '/', it is assumed to be a |
58 directory, and a list of files in the directory is mapped to the path. | 62 directory, and a list of files in the directory is mapped to the path. |
59 | 63 |
60 If binary=False, the contents of each file will be unicode parsed as utf-8, | 64 If binary=False, the contents of each file will be unicode parsed as utf-8, |
61 and failing that as latin-1 (some extension docs use latin-1). If | 65 and failing that as latin-1 (some extension docs use latin-1). If |
62 binary=True then the contents will be a str. | 66 binary=True then the contents will be a str. |
63 | 67 |
64 If any path cannot be found, raises a FileNotFoundError. This is guaranteed | 68 If any path cannot be found, raises a FileNotFoundError. This is guaranteed |
65 to only happen once the Future has been resolved (Get() called). | 69 to only happen once the Future has been resolved (Get() called). |
66 | 70 |
67 For any other failure, raises a FileSystemError. | 71 For any other failure, raises a FileSystemError. |
68 ''' | 72 ''' |
69 raise NotImplementedError(self.__class__) | 73 raise NotImplementedError(self.__class__) |
70 | 74 |
71 def ReadSingle(self, path, binary=False): | 75 def ReadSingle(self, path, binary=False): |
72 '''Reads a single file from the FileSystem. Returns a Future with the same | 76 '''Reads a single file from the FileSystem. Returns a Future with the same |
73 rules as Read(). | 77 rules as Read(). |
74 ''' | 78 ''' |
75 read_single = self.Read([path], binary=binary) | 79 read_single = self.Read([path], binary=binary) |
76 return Future(delegate=Gettable(lambda: read_single.Get()[path])) | 80 return Future(delegate=Gettable(lambda: read_single.Get()[path])) |
77 | 81 |
78 def Refresh(self): | 82 def Refresh(self): |
79 raise NotImplementedError(self.__class__) | 83 '''Asynchronously refreshes the content of the FileSystem, returning a |
| 84 future to its completion. |
| 85 ''' |
| 86 return Future(value=()) |
80 | 87 |
81 # TODO(cduvall): Allow Stat to take a list of paths like Read. | 88 # TODO(cduvall): Allow Stat to take a list of paths like Read. |
82 def Stat(self, path): | 89 def Stat(self, path): |
83 '''Returns a |StatInfo| object containing the version of |path|. If |path| | 90 '''Returns a |StatInfo| object containing the version of |path|. If |path| |
84 is a directory, |StatInfo| will have the versions of all the children of | 91 is a directory, |StatInfo| will have the versions of all the children of |
85 the directory in |StatInfo.child_versions|. | 92 the directory in |StatInfo.child_versions|. |
86 | 93 |
87 If the path cannot be found, raises a FileNotFoundError. | 94 If the path cannot be found, raises a FileNotFoundError. |
88 For any other failure, raises a FileSystemError. | 95 For any other failure, raises a FileSystemError. |
89 ''' | 96 ''' |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 yield walkinfo | 133 yield walkinfo |
127 | 134 |
128 for walkinfo in walk(root): | 135 for walkinfo in walk(root): |
129 yield walkinfo | 136 yield walkinfo |
130 | 137 |
131 def __repr__(self): | 138 def __repr__(self): |
132 return '<%s>' % type(self).__name__ | 139 return '<%s>' % type(self).__name__ |
133 | 140 |
134 def __str__(self): | 141 def __str__(self): |
135 return repr(self) | 142 return repr(self) |
OLD | NEW |