LEFT | RIGHT |
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 """The Virtual File System (VFS) file entry interface. | 2 """The Virtual File System (VFS) file entry interface. |
3 | 3 |
4 The file entry can be various file system elements like a regular file, | 4 The file entry can be various file system elements like a regular file, |
5 a directory or file system metadata. | 5 a directory or file system metadata. |
6 """ | 6 """ |
| 7 |
| 8 from __future__ import unicode_literals |
7 | 9 |
8 import abc | 10 import abc |
9 | 11 |
10 from dfvfs.lib import definitions | 12 from dfvfs.lib import definitions |
11 from dfvfs.lib import py2to3 | 13 from dfvfs.lib import py2to3 |
12 from dfvfs.resolver import resolver | 14 from dfvfs.resolver import resolver |
13 from dfvfs.vfs import vfs_stat | 15 from dfvfs.vfs import vfs_stat |
14 | 16 |
15 | 17 |
16 class Attribute(object): | 18 class Attribute(object): |
17 """VFS attribute interface.""" | 19 """VFS attribute interface.""" |
18 | 20 |
19 @property | 21 @property |
20 def type_indicator(self): | 22 def type_indicator(self): |
21 """str: type indicator.""" | 23 """str: type indicator.""" |
22 type_indicator = getattr(self, u'TYPE_INDICATOR', None) | 24 type_indicator = getattr(self, 'TYPE_INDICATOR', None) |
23 if type_indicator is None: | 25 if type_indicator is None: |
24 raise NotImplementedError( | 26 raise NotImplementedError( |
25 u'Invalid attribute missing type indicator.') | 27 'Invalid attribute missing type indicator.') |
26 return type_indicator | 28 return type_indicator |
27 | 29 |
28 | 30 |
29 class DataStream(object): | 31 class DataStream(object): |
30 """VFS data stream interface.""" | 32 """VFS data stream interface.""" |
31 | 33 |
32 # The data stream object should not have a reference to its | 34 # The data stream object should not have a reference to its |
33 # file entry since that will create a cyclic reference. | 35 # file entry since that will create a cyclic reference. |
34 | 36 |
35 @property | 37 @property |
36 def name(self): | 38 def name(self): |
37 """str: name.""" | 39 """str: name.""" |
38 return u'' | 40 return '' |
39 | 41 |
40 | 42 |
41 class Directory(object): | 43 class Directory(object): |
42 """VFS directory interface. | 44 """VFS directory interface. |
43 | 45 |
44 Attributes: | 46 Attributes: |
45 path_spec (PathSpec): path specification of the directory. | 47 path_spec (PathSpec): path specification of the directory. |
46 """ | 48 """ |
47 | 49 |
48 def __init__(self, file_system, path_spec): | 50 def __init__(self, file_system, path_spec): |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 self._resolver_context = resolver_context | 104 self._resolver_context = resolver_context |
103 self._stat_object = None | 105 self._stat_object = None |
104 self._type = None | 106 self._type = None |
105 self.path_spec = path_spec | 107 self.path_spec = path_spec |
106 | 108 |
107 self._file_system.Open(path_spec) | 109 self._file_system.Open(path_spec) |
108 | 110 |
109 def __del__(self): | 111 def __del__(self): |
110 """Cleans up the file entry.""" | 112 """Cleans up the file entry.""" |
111 # __del__ can be invoked before __init__ has completed. | 113 # __del__ can be invoked before __init__ has completed. |
112 if hasattr(self, u'_file_system'): | 114 if hasattr(self, '_file_system'): |
113 self._file_system.Close() | 115 self._file_system.Close() |
114 self._file_system = None | 116 self._file_system = None |
115 | 117 |
116 def _GetAttributes(self): | 118 def _GetAttributes(self): |
117 """Retrieves the attributes. | 119 """Retrieves the attributes. |
118 | 120 |
119 Returns: | 121 Returns: |
120 list[Attribute]: attributes. | 122 list[Attribute]: attributes. |
121 """ | 123 """ |
122 if self._attributes is None: | 124 if self._attributes is None: |
(...skipping 28 matching lines...) Expand all Loading... |
151 Directory: a directory or None. | 153 Directory: a directory or None. |
152 """ | 154 """ |
153 | 155 |
154 def _GetLink(self): | 156 def _GetLink(self): |
155 """Retrieves the link. | 157 """Retrieves the link. |
156 | 158 |
157 Returns: | 159 Returns: |
158 str: full path of the linked file entry. | 160 str: full path of the linked file entry. |
159 """ | 161 """ |
160 if self._link is None: | 162 if self._link is None: |
161 self._link = u'' | 163 self._link = '' |
162 return self._link | 164 return self._link |
163 | 165 |
164 def _GetStat(self): | 166 def _GetStat(self): |
165 """Retrieves information about the file entry. | 167 """Retrieves information about the file entry. |
166 | 168 |
167 Returns: | 169 Returns: |
168 VFSStat: a stat object. | 170 VFSStat: a stat object. |
169 """ | 171 """ |
170 stat_object = vfs_stat.VFSStat() | 172 stat_object = vfs_stat.VFSStat() |
171 | 173 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 # We cannot use len(self._directory.entries) since entries is a generator. | 273 # We cannot use len(self._directory.entries) since entries is a generator. |
272 return sum(1 for path_spec in self._directory.entries) | 274 return sum(1 for path_spec in self._directory.entries) |
273 | 275 |
274 @abc.abstractproperty | 276 @abc.abstractproperty |
275 def sub_file_entries(self): | 277 def sub_file_entries(self): |
276 """generator[FileEntry]: sub file entries.""" | 278 """generator[FileEntry]: sub file entries.""" |
277 | 279 |
278 @property | 280 @property |
279 def type_indicator(self): | 281 def type_indicator(self): |
280 """str: type indicator.""" | 282 """str: type indicator.""" |
281 type_indicator = getattr(self, u'TYPE_INDICATOR', None) | 283 type_indicator = getattr(self, 'TYPE_INDICATOR', None) |
282 if type_indicator is None: | 284 if type_indicator is None: |
283 raise NotImplementedError( | 285 raise NotImplementedError( |
284 u'Invalid file entry missing type indicator.') | 286 'Invalid file entry missing type indicator.') |
285 return type_indicator | 287 return type_indicator |
286 | 288 |
287 def GetDataStream(self, name, case_sensitive=True): | 289 def GetDataStream(self, name, case_sensitive=True): |
288 """Retrieves a data stream by name. | 290 """Retrieves a data stream by name. |
289 | 291 |
290 Args: | 292 Args: |
291 name (str): name of the data stream. | 293 name (str): name of the data stream. |
292 case_sentitive (Optional[bool]): True if the name is case sensitive. | 294 case_sentitive (Optional[bool]): True if the name is case sensitive. |
293 | 295 |
294 Returns: | 296 Returns: |
295 DataStream: a data stream or None if not available. | 297 DataStream: a data stream or None if not available. |
296 | 298 |
297 Raises: | 299 Raises: |
298 ValueError: if the name is not string. | 300 ValueError: if the name is not string. |
299 """ | 301 """ |
300 if not isinstance(name, py2to3.STRING_TYPES): | 302 if not isinstance(name, py2to3.STRING_TYPES): |
301 raise ValueError(u'Name is not a string.') | 303 raise ValueError('Name is not a string.') |
302 | 304 |
303 name_lower = name.lower() | 305 name_lower = name.lower() |
304 matching_data_stream = None | 306 matching_data_stream = None |
305 | 307 |
306 for data_stream in self._GetDataStreams(): | 308 for data_stream in self._GetDataStreams(): |
307 if data_stream.name == name: | 309 if data_stream.name == name: |
308 return data_stream | 310 return data_stream |
309 | 311 |
310 if not case_sensitive and data_stream.name.lower() == name_lower: | 312 if not case_sensitive and data_stream.name.lower() == name_lower: |
311 if not matching_data_stream: | 313 if not matching_data_stream: |
312 matching_data_stream = data_stream | 314 matching_data_stream = data_stream |
313 | 315 |
314 return matching_data_stream | 316 return matching_data_stream |
315 | 317 |
316 def GetFileObject(self, data_stream_name=u''): | 318 def GetFileObject(self, data_stream_name=''): |
317 """Retrieves the file-like object. | 319 """Retrieves the file-like object. |
318 | 320 |
319 Args: | 321 Args: |
320 data_stream_name (Optional[str]): name of the data stream, where an empty | 322 data_stream_name (Optional[str]): name of the data stream, where an empty |
321 string represents the default data stream. | 323 string represents the default data stream. |
322 | 324 |
323 Returns: | 325 Returns: |
324 FileIO: a file-like object or None if not available. | 326 FileIO: a file-like object or None if not available. |
325 """ | 327 """ |
326 if not data_stream_name: | 328 if not data_stream_name: |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 name (str): name of the data stream. | 393 name (str): name of the data stream. |
392 case_sentitive (Optional[bool]): True if the name is case sensitive. | 394 case_sentitive (Optional[bool]): True if the name is case sensitive. |
393 | 395 |
394 Returns: | 396 Returns: |
395 bool: True if the file entry has the data stream. | 397 bool: True if the file entry has the data stream. |
396 | 398 |
397 Raises: | 399 Raises: |
398 ValueError: if the name is not string. | 400 ValueError: if the name is not string. |
399 """ | 401 """ |
400 if not isinstance(name, py2to3.STRING_TYPES): | 402 if not isinstance(name, py2to3.STRING_TYPES): |
401 raise ValueError(u'Name is not a string.') | 403 raise ValueError('Name is not a string.') |
402 | 404 |
403 name_lower = name.lower() | 405 name_lower = name.lower() |
404 | 406 |
405 for data_stream in self._GetDataStreams(): | 407 for data_stream in self._GetDataStreams(): |
406 if data_stream.name == name: | 408 if data_stream.name == name: |
407 return True | 409 return True |
408 | 410 |
409 if not case_sensitive and data_stream.name.lower() == name_lower: | 411 if not case_sensitive and data_stream.name.lower() == name_lower: |
410 return True | 412 return True |
411 | 413 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 self._type = self._stat_object.type | 511 self._type = self._stat_object.type |
510 return self._type == definitions.FILE_ENTRY_TYPE_SOCKET | 512 return self._type == definitions.FILE_ENTRY_TYPE_SOCKET |
511 | 513 |
512 def IsVirtual(self): | 514 def IsVirtual(self): |
513 """Determines if the file entry is virtual (emulated by dfVFS). | 515 """Determines if the file entry is virtual (emulated by dfVFS). |
514 | 516 |
515 Returns: | 517 Returns: |
516 bool: True if the file entry is virtual. | 518 bool: True if the file entry is virtual. |
517 """ | 519 """ |
518 return self._is_virtual | 520 return self._is_virtual |
LEFT | RIGHT |