Mercurial > ~astiob > upreckon > hgweb
comparison files.py @ 132:cdd0f970d112
Fixed several small bugs in the files module
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Thu, 19 May 2011 02:55:36 +0100 |
parents | 62a96d51bf94 |
children | a9d2aa6810c7 |
comparison
equal
deleted
inserted
replaced
131:7923faf392fe | 132:cdd0f970d112 |
---|---|
43 | 43 |
44 @abstractmethod | 44 @abstractmethod |
45 def extract(self, name, target): raise NotImplementedError | 45 def extract(self, name, target): raise NotImplementedError |
46 | 46 |
47 def __del__(self): | 47 def __del__(self): |
48 del self.file | 48 try: |
49 del self.file | |
50 except NameError: | |
51 pass | |
49 | 52 |
50 try: | 53 try: |
51 import tarfile | 54 import tarfile |
52 except ImportError: | 55 except ImportError: |
53 TarArchive = None | 56 TarArchive = None |
54 else: | 57 else: |
55 class TarArchive(Archive): | 58 class TarArchive(Archive): |
56 __slots__ = '__namelist' | 59 __slots__ = '_namelist' |
57 | 60 |
58 def __init__(self, path): | 61 def __init__(self, path): |
59 self.file = tarfile.open(path) | 62 self.file = tarfile.open(path) |
60 | 63 |
61 def extract(self, name, target): | 64 def extract(self, name, target): |
66 # TODO: somehow automagically emulate universal line break support | 69 # TODO: somehow automagically emulate universal line break support |
67 def open(self, name): | 70 def open(self, name): |
68 return self.file.extractfile(name) | 71 return self.file.extractfile(name) |
69 | 72 |
70 def exists(self, queried_name): | 73 def exists(self, queried_name): |
71 if not hasattr(self, '__namelist'): | 74 if not hasattr(self, '_namelist'): |
72 names = set() | 75 names = set() |
73 for name in self.file.getnames(): | 76 for name in self.file.getnames(): |
74 cutname = name | 77 cutname = name |
75 while cutname: | 78 while cutname: |
76 names.add(cutname) | 79 names.add(cutname) |
77 cutname = cutname.rpartition('/')[0] | 80 cutname = cutname.rpartition('/')[0] |
78 self.__namelist = frozenset(names) | 81 self._namelist = frozenset(names) |
79 return queried_name in self.__namelist | 82 return queried_name in self._namelist |
80 | 83 |
81 def __enter__(self): | 84 def __enter__(self): |
82 if hasattr(self.file, '__enter__'): | 85 if hasattr(self.file, '__enter__'): |
83 self.file.__enter__() | 86 self.file.__enter__() |
84 return self | 87 return self |
100 import zipfile | 103 import zipfile |
101 except ImportError: | 104 except ImportError: |
102 ZipArchive = None | 105 ZipArchive = None |
103 else: | 106 else: |
104 class ZipArchive(Archive): | 107 class ZipArchive(Archive): |
105 __slots__ = '__namelist' | 108 __slots__ = '_namelist' |
106 | 109 |
107 def __init__(self, path): | 110 def __init__(self, path): |
108 self.file = zipfile.ZipFile(path) | 111 self.file = zipfile.ZipFile(path) |
109 | 112 |
110 def extract(self, name, target): | 113 def extract(self, name, target): |
121 | 124 |
122 def open(self, name): | 125 def open(self, name): |
123 return self.file.open(name, 'rU') | 126 return self.file.open(name, 'rU') |
124 | 127 |
125 def exists(self, queried_name): | 128 def exists(self, queried_name): |
126 if not hasattr(self, '__namelist'): | 129 if not hasattr(self, '_namelist'): |
127 names = set() | 130 names = set() |
128 for name in self.file.namelist(): | 131 for name in self.file.namelist(): |
129 cutname = name | 132 cutname = name |
130 while cutname: | 133 while cutname: |
131 names.add(cutname) | 134 names.add(cutname) |
132 cutname = cutname.rpartition('/')[0] | 135 cutname = cutname.rpartition('/')[0] |
133 self.__namelist = frozenset(names) | 136 self._namelist = frozenset(names) |
134 return queried_name in self.__namelist | 137 return queried_name in self._namelist |
135 | 138 |
136 def __enter__(self): | 139 def __enter__(self): |
137 if hasattr(self.file, '__enter__'): | 140 if hasattr(self.file, '__enter__'): |
138 self.file.__enter__() | 141 self.file.__enter__() |
139 return self | 142 return self |