Show More
This diff has been collapsed as it changes many lines, (774 lines changed) Show them Hide them | |||||
@@ -1,947 +1,1267 b'' | |||||
|
1 | # | |||
|
2 | # Copyright (c) 2010 Mikhail Gusarov | |||
|
3 | # | |||
|
4 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |||
|
5 | # of this software and associated documentation files (the "Software"), to deal | |||
|
6 | # in the Software without restriction, including without limitation the rights | |||
|
7 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |||
|
8 | # copies of the Software, and to permit persons to whom the Software is | |||
|
9 | # furnished to do so, subject to the following conditions: | |||
|
10 | # | |||
|
11 | # The above copyright notice and this permission notice shall be included in | |||
|
12 | # all copies or substantial portions of the Software. | |||
|
13 | # | |||
|
14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |||
|
15 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |||
|
16 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |||
|
17 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |||
|
18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |||
|
19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |||
|
20 | # SOFTWARE. | |||
|
21 | # | |||
|
22 | ||||
1 | """ path.py - An object representing a path to a file or directory. |
|
23 | """ path.py - An object representing a path to a file or directory. | |
2 |
|
24 | |||
3 | Example: |
|
25 | Original author: | |
|
26 | Jason Orendorff <jason.orendorff\x40gmail\x2ecom> | |||
|
27 | ||||
|
28 | Current maintainer: | |||
|
29 | Jason R. Coombs <jaraco@jaraco.com> | |||
4 |
|
30 | |||
5 | from IPython.external.path import path |
|
31 | Contributors: | |
6 | d = path('/home/guido/bin') |
|
32 | Mikhail Gusarov <dottedmag@dottedmag.net> | |
7 | for f in d.files('*.py'): |
|
33 | Marc Abramowitz <marc@marc-abramowitz.com> | |
8 | f.chmod(0755) |
|
34 | Jason R. Coombs <jaraco@jaraco.com> | |
|
35 | Jason Chu <jchu@xentac.net> | |||
|
36 | Vojislav Stojkovic <vstojkovic@syntertainment.com> | |||
9 |
|
37 | |||
10 | This module requires Python 2.5 or later. |
|
38 | Example:: | |
11 |
|
39 | |||
|
40 | from path import path | |||
|
41 | d = path('/home/guido/bin') | |||
|
42 | for f in d.files('*.py'): | |||
|
43 | f.chmod(0755) | |||
12 |
|
44 | |||
13 | URL: http://pypi.python.org/pypi/path.py |
|
45 | path.py requires Python 2.5 or later. | |
14 | Author: Jason Orendorff <jason.orendorff\x40gmail\x2ecom> (and others - see the url!) |
|
|||
15 | Date: 9 Mar 2007 |
|
|||
16 | """ |
|
46 | """ | |
17 |
|
47 | |||
|
48 | from __future__ import with_statement | |||
|
49 | ||||
|
50 | import sys | |||
|
51 | import warnings | |||
|
52 | import os | |||
|
53 | import fnmatch | |||
|
54 | import glob | |||
|
55 | import shutil | |||
|
56 | import codecs | |||
|
57 | import hashlib | |||
|
58 | import errno | |||
|
59 | import tempfile | |||
|
60 | import functools | |||
|
61 | import operator | |||
|
62 | import re | |||
|
63 | ||||
|
64 | try: | |||
|
65 | import win32security | |||
|
66 | except ImportError: | |||
|
67 | pass | |||
18 |
|
68 | |||
19 | # TODO |
|
69 | try: | |
20 | # - Tree-walking functions don't avoid symlink loops. Matt Harrison |
|
70 | import pwd | |
21 | # sent me a patch for this. |
|
71 | except ImportError: | |
22 | # - Bug in write_text(). It doesn't support Universal newline mode. |
|
72 | pass | |
23 | # - Better error message in listdir() when self isn't a |
|
|||
24 | # directory. (On Windows, the error message really sucks.) |
|
|||
25 | # - Make sure everything has a good docstring. |
|
|||
26 | # - Add methods for regex find and replace. |
|
|||
27 | # - guess_content_type() method? |
|
|||
28 | # - Perhaps support arguments to touch(). |
|
|||
29 |
|
73 | |||
30 | from __future__ import generators |
|
74 | ################################ | |
|
75 | # Monkey patchy python 3 support | |||
|
76 | try: | |||
|
77 | basestring | |||
|
78 | except NameError: | |||
|
79 | basestring = str | |||
|
80 | ||||
|
81 | try: | |||
|
82 | unicode | |||
|
83 | except NameError: | |||
|
84 | unicode = str | |||
|
85 | ||||
|
86 | try: | |||
|
87 | os.getcwdu | |||
|
88 | except AttributeError: | |||
|
89 | os.getcwdu = os.getcwd | |||
|
90 | ||||
|
91 | if sys.version < '3': | |||
|
92 | def u(x): | |||
|
93 | return codecs.unicode_escape_decode(x)[0] | |||
|
94 | else: | |||
|
95 | def u(x): | |||
|
96 | return x | |||
31 |
|
97 | |||
32 | import sys, warnings, os, fnmatch, glob, shutil, codecs |
|
98 | o777 = 511 | |
33 | from hashlib import md5 |
|
99 | o766 = 502 | |
|
100 | o666 = 438 | |||
|
101 | o554 = 364 | |||
|
102 | ################################ | |||
34 |
|
103 | |||
35 |
__version__ = ' |
|
104 | __version__ = '4.3' | |
36 | __all__ = ['path'] |
|
105 | __all__ = ['path'] | |
37 |
|
106 | |||
38 | # Platform-specific support for path.owner |
|
|||
39 | if os.name == 'nt': |
|
|||
40 | try: |
|
|||
41 | import win32security |
|
|||
42 | except ImportError: |
|
|||
43 | win32security = None |
|
|||
44 | else: |
|
|||
45 | try: |
|
|||
46 | import pwd |
|
|||
47 | except ImportError: |
|
|||
48 | pwd = None |
|
|||
49 |
|
||||
50 |
|
107 | |||
51 | class TreeWalkWarning(Warning): |
|
108 | class TreeWalkWarning(Warning): | |
52 | pass |
|
109 | pass | |
53 |
|
110 | |||
|
111 | ||||
|
112 | def simple_cache(func): | |||
|
113 | """ | |||
|
114 | Save results for the 'using_module' classmethod. | |||
|
115 | When Python 3.2 is available, use functools.lru_cache instead. | |||
|
116 | """ | |||
|
117 | saved_results = {} | |||
|
118 | ||||
|
119 | def wrapper(cls, module): | |||
|
120 | if module in saved_results: | |||
|
121 | return saved_results[module] | |||
|
122 | saved_results[module] = func(cls, module) | |||
|
123 | return saved_results[module] | |||
|
124 | return wrapper | |||
|
125 | ||||
|
126 | ||||
|
127 | class ClassProperty(property): | |||
|
128 | def __get__(self, cls, owner): | |||
|
129 | return self.fget.__get__(None, owner)() | |||
|
130 | ||||
|
131 | ||||
|
132 | class multimethod(object): | |||
|
133 | """ | |||
|
134 | Acts like a classmethod when invoked from the class and like an | |||
|
135 | instancemethod when invoked from the instance. | |||
|
136 | """ | |||
|
137 | def __init__(self, func): | |||
|
138 | self.func = func | |||
|
139 | ||||
|
140 | def __get__(self, instance, owner): | |||
|
141 | return ( | |||
|
142 | functools.partial(self.func, owner) if instance is None | |||
|
143 | else functools.partial(self.func, owner, instance) | |||
|
144 | ) | |||
|
145 | ||||
|
146 | ||||
54 | class path(unicode): |
|
147 | class path(unicode): | |
55 | """ Represents a filesystem path. |
|
148 | """ Represents a filesystem path. | |
56 |
|
149 | |||
57 | For documentation on individual methods, consult their |
|
150 | For documentation on individual methods, consult their | |
58 | counterparts in os.path. |
|
151 | counterparts in os.path. | |
59 | """ |
|
152 | """ | |
60 |
|
153 | |||
|
154 | module = os.path | |||
|
155 | "The path module to use for path operations." | |||
|
156 | ||||
|
157 | def __init__(self, other=''): | |||
|
158 | if other is None: | |||
|
159 | raise TypeError("Invalid initial value for path: None") | |||
|
160 | ||||
|
161 | @classmethod | |||
|
162 | @simple_cache | |||
|
163 | def using_module(cls, module): | |||
|
164 | subclass_name = cls.__name__ + '_' + module.__name__ | |||
|
165 | bases = (cls,) | |||
|
166 | ns = {'module': module} | |||
|
167 | return type(subclass_name, bases, ns) | |||
|
168 | ||||
|
169 | @ClassProperty | |||
|
170 | @classmethod | |||
|
171 | def _next_class(cls): | |||
|
172 | """ | |||
|
173 | What class should be used to construct new instances from this class | |||
|
174 | """ | |||
|
175 | return cls | |||
|
176 | ||||
61 | # --- Special Python methods. |
|
177 | # --- Special Python methods. | |
62 |
|
178 | |||
63 | def __repr__(self): |
|
179 | def __repr__(self): | |
64 | return 'path(%s)' % unicode.__repr__(self) |
|
180 | return '%s(%s)' % (type(self).__name__, super(path, self).__repr__()) | |
65 |
|
181 | |||
66 | # Adding a path and a string yields a path. |
|
182 | # Adding a path and a string yields a path. | |
67 | def __add__(self, more): |
|
183 | def __add__(self, more): | |
68 | try: |
|
184 | try: | |
69 | resultStr = unicode.__add__(self, more) |
|
185 | return self._next_class(super(path, self).__add__(more)) | |
70 | except TypeError: #Python bug |
|
186 | except TypeError: # Python bug | |
71 |
re |
|
187 | return NotImplemented | |
72 | if resultStr is NotImplemented: |
|
|||
73 | return resultStr |
|
|||
74 | return self.__class__(resultStr) |
|
|||
75 |
|
188 | |||
76 | def __radd__(self, other): |
|
189 | def __radd__(self, other): | |
77 | if isinstance(other, basestring): |
|
190 | if not isinstance(other, basestring): | |
78 | return self.__class__(other.__add__(self)) |
|
|||
79 | else: |
|
|||
80 | return NotImplemented |
|
191 | return NotImplemented | |
|
192 | return self._next_class(other.__add__(self)) | |||
81 |
|
193 | |||
82 | # The / operator joins paths. |
|
194 | # The / operator joins paths. | |
83 | def __div__(self, rel): |
|
195 | def __div__(self, rel): | |
84 | """ fp.__div__(rel) == fp / rel == fp.joinpath(rel) |
|
196 | """ fp.__div__(rel) == fp / rel == fp.joinpath(rel) | |
85 |
|
197 | |||
86 | Join two path components, adding a separator character if |
|
198 | Join two path components, adding a separator character if | |
87 | needed. |
|
199 | needed. | |
88 | """ |
|
200 | """ | |
89 |
return self.__class |
|
201 | return self._next_class(self.module.join(self, rel)) | |
90 |
|
202 | |||
91 | # Make the / operator work even when true division is enabled. |
|
203 | # Make the / operator work even when true division is enabled. | |
92 | __truediv__ = __div__ |
|
204 | __truediv__ = __div__ | |
93 |
|
205 | |||
|
206 | def __enter__(self): | |||
|
207 | self._old_dir = self.getcwd() | |||
|
208 | os.chdir(self) | |||
|
209 | return self | |||
|
210 | ||||
|
211 | def __exit__(self, *_): | |||
|
212 | os.chdir(self._old_dir) | |||
|
213 | ||||
|
214 | @classmethod | |||
94 | def getcwd(cls): |
|
215 | def getcwd(cls): | |
95 | """ Return the current working directory as a path object. """ |
|
216 | """ Return the current working directory as a path object. """ | |
96 | return cls(os.getcwdu()) |
|
217 | return cls(os.getcwdu()) | |
97 | getcwd = classmethod(getcwd) |
|
|||
98 |
|
||||
99 |
|
218 | |||
|
219 | # | |||
100 | # --- Operations on path strings. |
|
220 | # --- Operations on path strings. | |
101 |
|
221 | |||
102 | def isabs(s): return os.path.isabs(s) |
|
222 | def abspath(self): | |
103 |
|
|
223 | return self._next_class(self.module.abspath(self)) | |
104 | def normcase(self): return self.__class__(os.path.normcase(self)) |
|
224 | ||
105 | def normpath(self): return self.__class__(os.path.normpath(self)) |
|
225 | def normcase(self): | |
106 | def realpath(self): return self.__class__(os.path.realpath(self)) |
|
226 | return self._next_class(self.module.normcase(self)) | |
107 | def expanduser(self): return self.__class__(os.path.expanduser(self)) |
|
227 | ||
108 | def expandvars(self): return self.__class__(os.path.expandvars(self)) |
|
228 | def normpath(self): | |
109 |
|
|
229 | return self._next_class(self.module.normpath(self)) | |
110 | def basename(s): return os.path.basename(s) |
|
230 | ||
|
231 | def realpath(self): | |||
|
232 | return self._next_class(self.module.realpath(self)) | |||
|
233 | ||||
|
234 | def expanduser(self): | |||
|
235 | return self._next_class(self.module.expanduser(self)) | |||
|
236 | ||||
|
237 | def expandvars(self): | |||
|
238 | return self._next_class(self.module.expandvars(self)) | |||
|
239 | ||||
|
240 | def dirname(self): | |||
|
241 | return self._next_class(self.module.dirname(self)) | |||
|
242 | ||||
|
243 | def basename(self): | |||
|
244 | return self._next_class(self.module.basename(self)) | |||
111 |
|
245 | |||
112 | def expand(self): |
|
246 | def expand(self): | |
113 | """ Clean up a filename by calling expandvars(), |
|
247 | """ Clean up a filename by calling expandvars(), | |
114 | expanduser(), and normpath() on it. |
|
248 | expanduser(), and normpath() on it. | |
115 |
|
249 | |||
116 | This is commonly everything needed to clean up a filename |
|
250 | This is commonly everything needed to clean up a filename | |
117 | read from a configuration file, for example. |
|
251 | read from a configuration file, for example. | |
118 | """ |
|
252 | """ | |
119 | return self.expandvars().expanduser().normpath() |
|
253 | return self.expandvars().expanduser().normpath() | |
120 |
|
254 | |||
121 | def _get_namebase(self): |
|
255 | @property | |
122 | base, ext = os.path.splitext(self.name) |
|
256 | def namebase(self): | |
|
257 | """ The same as path.name, but with one file extension stripped off. | |||
|
258 | ||||
|
259 | For example, path('/home/guido/python.tar.gz').name == 'python.tar.gz', | |||
|
260 | but path('/home/guido/python.tar.gz').namebase == 'python.tar' | |||
|
261 | """ | |||
|
262 | base, ext = self.module.splitext(self.name) | |||
123 | return base |
|
263 | return base | |
124 |
|
264 | |||
125 | def _get_ext(self): |
|
265 | @property | |
126 | f, ext = os.path.splitext(unicode(self)) |
|
266 | def ext(self): | |
|
267 | """ The file extension, for example '.py'. """ | |||
|
268 | f, ext = self.module.splitext(self) | |||
127 | return ext |
|
269 | return ext | |
128 |
|
270 | |||
129 | def _get_drive(self): |
|
271 | @property | |
130 | drive, r = os.path.splitdrive(self) |
|
272 | def drive(self): | |
131 | return self.__class__(drive) |
|
273 | """ The drive specifier, for example 'C:'. | |
|
274 | This is always empty on systems that don't use drive specifiers. | |||
|
275 | """ | |||
|
276 | drive, r = self.module.splitdrive(self) | |||
|
277 | return self._next_class(drive) | |||
132 |
|
278 | |||
133 | parent = property( |
|
279 | parent = property( | |
134 | dirname, None, None, |
|
280 | dirname, None, None, | |
135 | """ This path's parent directory, as a new path object. |
|
281 | """ This path's parent directory, as a new path object. | |
136 |
|
282 | |||
137 | For example, path('/usr/local/lib/libpython.so').parent == path('/usr/local/lib') |
|
283 | For example, | |
|
284 | path('/usr/local/lib/libpython.so').parent == path('/usr/local/lib') | |||
138 | """) |
|
285 | """) | |
139 |
|
286 | |||
140 | name = property( |
|
287 | name = property( | |
141 | basename, None, None, |
|
288 | basename, None, None, | |
142 | """ The name of this file or directory without the full path. |
|
289 | """ The name of this file or directory without the full path. | |
143 |
|
290 | |||
144 | For example, path('/usr/local/lib/libpython.so').name == 'libpython.so' |
|
291 | For example, path('/usr/local/lib/libpython.so').name == 'libpython.so' | |
145 | """) |
|
292 | """) | |
146 |
|
293 | |||
147 | namebase = property( |
|
|||
148 | _get_namebase, None, None, |
|
|||
149 | """ The same as path.name, but with one file extension stripped off. |
|
|||
150 |
|
||||
151 | For example, path('/home/guido/python.tar.gz').name == 'python.tar.gz', |
|
|||
152 | but path('/home/guido/python.tar.gz').namebase == 'python.tar' |
|
|||
153 | """) |
|
|||
154 |
|
||||
155 | ext = property( |
|
|||
156 | _get_ext, None, None, |
|
|||
157 | """ The file extension, for example '.py'. """) |
|
|||
158 |
|
||||
159 | drive = property( |
|
|||
160 | _get_drive, None, None, |
|
|||
161 | """ The drive specifier, for example 'C:'. |
|
|||
162 | This is always empty on systems that don't use drive specifiers. |
|
|||
163 | """) |
|
|||
164 |
|
||||
165 | def splitpath(self): |
|
294 | def splitpath(self): | |
166 | """ p.splitpath() -> Return (p.parent, p.name). """ |
|
295 | """ p.splitpath() -> Return (p.parent, p.name). """ | |
167 |
parent, child = |
|
296 | parent, child = self.module.split(self) | |
168 |
return self.__class |
|
297 | return self._next_class(parent), child | |
169 |
|
298 | |||
170 | def splitdrive(self): |
|
299 | def splitdrive(self): | |
171 | """ p.splitdrive() -> Return (p.drive, <the rest of p>). |
|
300 | """ p.splitdrive() -> Return (p.drive, <the rest of p>). | |
172 |
|
301 | |||
173 | Split the drive specifier from this path. If there is |
|
302 | Split the drive specifier from this path. If there is | |
174 | no drive specifier, p.drive is empty, so the return value |
|
303 | no drive specifier, p.drive is empty, so the return value | |
175 | is simply (path(''), p). This is always the case on Unix. |
|
304 | is simply (path(''), p). This is always the case on Unix. | |
176 | """ |
|
305 | """ | |
177 |
drive, rel = |
|
306 | drive, rel = self.module.splitdrive(self) | |
178 |
return self.__class |
|
307 | return self._next_class(drive), rel | |
179 |
|
308 | |||
180 | def splitext(self): |
|
309 | def splitext(self): | |
181 | """ p.splitext() -> Return (p.stripext(), p.ext). |
|
310 | """ p.splitext() -> Return (p.stripext(), p.ext). | |
182 |
|
311 | |||
183 | Split the filename extension from this path and return |
|
312 | Split the filename extension from this path and return | |
184 | the two parts. Either part may be empty. |
|
313 | the two parts. Either part may be empty. | |
185 |
|
314 | |||
186 | The extension is everything from '.' to the end of the |
|
315 | The extension is everything from '.' to the end of the | |
187 | last path segment. This has the property that if |
|
316 | last path segment. This has the property that if | |
188 | (a, b) == p.splitext(), then a + b == p. |
|
317 | (a, b) == p.splitext(), then a + b == p. | |
189 | """ |
|
318 | """ | |
190 |
filename, ext = |
|
319 | filename, ext = self.module.splitext(self) | |
191 |
return self.__class |
|
320 | return self._next_class(filename), ext | |
192 |
|
321 | |||
193 | def stripext(self): |
|
322 | def stripext(self): | |
194 | """ p.stripext() -> Remove one file extension from the path. |
|
323 | """ p.stripext() -> Remove one file extension from the path. | |
195 |
|
324 | |||
196 | For example, path('/home/guido/python.tar.gz').stripext() |
|
325 | For example, path('/home/guido/python.tar.gz').stripext() | |
197 | returns path('/home/guido/python.tar'). |
|
326 | returns path('/home/guido/python.tar'). | |
198 | """ |
|
327 | """ | |
199 | return self.splitext()[0] |
|
328 | return self.splitext()[0] | |
200 |
|
329 | |||
201 | if hasattr(os.path, 'splitunc'): |
|
330 | def splitunc(self): | |
202 |
|
|
331 | unc, rest = self.module.splitunc(self) | |
203 | unc, rest = os.path.splitunc(self) |
|
332 | return self._next_class(unc), rest | |
204 | return self.__class__(unc), rest |
|
|||
205 |
|
||||
206 | def _get_uncshare(self): |
|
|||
207 | unc, r = os.path.splitunc(self) |
|
|||
208 | return self.__class__(unc) |
|
|||
209 |
|
333 | |||
210 | uncshare = property( |
|
334 | @property | |
211 | _get_uncshare, None, None, |
|
335 | def uncshare(self): | |
212 | """ The UNC mount point for this path. |
|
336 | """ | |
213 | This is empty for paths on local drives. """) |
|
337 | The UNC mount point for this path. | |
|
338 | This is empty for paths on local drives. | |||
|
339 | """ | |||
|
340 | unc, r = self.module.splitunc(self) | |||
|
341 | return self._next_class(unc) | |||
214 |
|
342 | |||
215 | def joinpath(self, *args): |
|
343 | @multimethod | |
216 | """ Join two or more path components, adding a separator |
|
344 | def joinpath(cls, first, *others): | |
217 | character (os.sep) if needed. Returns a new path |
|
345 | """ | |
218 | object. |
|
346 | Join first to zero or more path components, adding a separator | |
|
347 | character (first.module.sep) if needed. Returns a new instance of | |||
|
348 | first._next_class. | |||
219 | """ |
|
349 | """ | |
220 | return self.__class__(os.path.join(self, *args)) |
|
350 | if not isinstance(first, cls): | |
|
351 | first = cls(first) | |||
|
352 | return first._next_class(first.module.join(first, *others)) | |||
221 |
|
353 | |||
222 | def splitall(self): |
|
354 | def splitall(self): | |
223 | r""" Return a list of the path components in this path. |
|
355 | r""" Return a list of the path components in this path. | |
224 |
|
356 | |||
225 | The first item in the list will be a path. Its value will be |
|
357 | The first item in the list will be a path. Its value will be | |
226 | either os.curdir, os.pardir, empty, or the root directory of |
|
358 | either os.curdir, os.pardir, empty, or the root directory of | |
227 | this path (for example, '/' or 'C:\\'). The other items in |
|
359 | this path (for example, ``'/'`` or ``'C:\\'``). The other items in | |
228 | the list will be strings. |
|
360 | the list will be strings. | |
229 |
|
361 | |||
230 | path.path.joinpath(*result) will yield the original path. |
|
362 | ``path.path.joinpath(*result)`` will yield the original path. | |
231 | """ |
|
363 | """ | |
232 | parts = [] |
|
364 | parts = [] | |
233 | loc = self |
|
365 | loc = self | |
234 | while loc != os.curdir and loc != os.pardir: |
|
366 | while loc != os.curdir and loc != os.pardir: | |
235 | prev = loc |
|
367 | prev = loc | |
236 | loc, child = prev.splitpath() |
|
368 | loc, child = prev.splitpath() | |
237 | if loc == prev: |
|
369 | if loc == prev: | |
238 | break |
|
370 | break | |
239 | parts.append(child) |
|
371 | parts.append(child) | |
240 | parts.append(loc) |
|
372 | parts.append(loc) | |
241 | parts.reverse() |
|
373 | parts.reverse() | |
242 | return parts |
|
374 | return parts | |
243 |
|
375 | |||
244 | def relpath(self): |
|
376 | def relpath(self, start='.'): | |
245 | """ Return this path as a relative path, |
|
377 | """ Return this path as a relative path, | |
246 | based from the current working directory. |
|
378 | based from start, which defaults to the current working directory. | |
247 | """ |
|
379 | """ | |
248 |
cwd = self.__class |
|
380 | cwd = self._next_class(start) | |
249 | return cwd.relpathto(self) |
|
381 | return cwd.relpathto(self) | |
250 |
|
382 | |||
251 | def relpathto(self, dest): |
|
383 | def relpathto(self, dest): | |
252 | """ Return a relative path from self to dest. |
|
384 | """ Return a relative path from self to dest. | |
253 |
|
385 | |||
254 | If there is no relative path from self to dest, for example if |
|
386 | If there is no relative path from self to dest, for example if | |
255 | they reside on different drives in Windows, then this returns |
|
387 | they reside on different drives in Windows, then this returns | |
256 | dest.abspath(). |
|
388 | dest.abspath(). | |
257 | """ |
|
389 | """ | |
258 | origin = self.abspath() |
|
390 | origin = self.abspath() | |
259 |
dest = self.__class |
|
391 | dest = self._next_class(dest).abspath() | |
260 |
|
392 | |||
261 | orig_list = origin.normcase().splitall() |
|
393 | orig_list = origin.normcase().splitall() | |
262 | # Don't normcase dest! We want to preserve the case. |
|
394 | # Don't normcase dest! We want to preserve the case. | |
263 | dest_list = dest.splitall() |
|
395 | dest_list = dest.splitall() | |
264 |
|
396 | |||
265 |
if orig_list[0] != |
|
397 | if orig_list[0] != self.module.normcase(dest_list[0]): | |
266 | # Can't get here from there. |
|
398 | # Can't get here from there. | |
267 | return dest |
|
399 | return dest | |
268 |
|
400 | |||
269 | # Find the location where the two paths start to differ. |
|
401 | # Find the location where the two paths start to differ. | |
270 | i = 0 |
|
402 | i = 0 | |
271 | for start_seg, dest_seg in zip(orig_list, dest_list): |
|
403 | for start_seg, dest_seg in zip(orig_list, dest_list): | |
272 |
if start_seg != |
|
404 | if start_seg != self.module.normcase(dest_seg): | |
273 | break |
|
405 | break | |
274 | i += 1 |
|
406 | i += 1 | |
275 |
|
407 | |||
276 | # Now i is the point where the two paths diverge. |
|
408 | # Now i is the point where the two paths diverge. | |
277 | # Need a certain number of "os.pardir"s to work up |
|
409 | # Need a certain number of "os.pardir"s to work up | |
278 | # from the origin to the point of divergence. |
|
410 | # from the origin to the point of divergence. | |
279 | segments = [os.pardir] * (len(orig_list) - i) |
|
411 | segments = [os.pardir] * (len(orig_list) - i) | |
280 | # Need to add the diverging part of dest_list. |
|
412 | # Need to add the diverging part of dest_list. | |
281 | segments += dest_list[i:] |
|
413 | segments += dest_list[i:] | |
282 | if len(segments) == 0: |
|
414 | if len(segments) == 0: | |
283 | # If they happen to be identical, use os.curdir. |
|
415 | # If they happen to be identical, use os.curdir. | |
284 | relpath = os.curdir |
|
416 | relpath = os.curdir | |
285 | else: |
|
417 | else: | |
286 |
relpath = |
|
418 | relpath = self.module.join(*segments) | |
287 |
return self.__class |
|
419 | return self._next_class(relpath) | |
288 |
|
420 | |||
289 | # --- Listing, searching, walking, and matching |
|
421 | # --- Listing, searching, walking, and matching | |
290 |
|
422 | |||
291 | def listdir(self, pattern=None): |
|
423 | def listdir(self, pattern=None): | |
292 | """ D.listdir() -> List of items in this directory. |
|
424 | """ D.listdir() -> List of items in this directory. | |
293 |
|
425 | |||
294 | Use D.files() or D.dirs() instead if you want a listing |
|
426 | Use D.files() or D.dirs() instead if you want a listing | |
295 | of just files or just subdirectories. |
|
427 | of just files or just subdirectories. | |
296 |
|
428 | |||
297 | The elements of the list are path objects. |
|
429 | The elements of the list are path objects. | |
298 |
|
430 | |||
299 | With the optional 'pattern' argument, this only lists |
|
431 | With the optional 'pattern' argument, this only lists | |
300 | items whose names match the given pattern. |
|
432 | items whose names match the given pattern. | |
301 | """ |
|
433 | """ | |
302 | names = os.listdir(self) |
|
434 | names = os.listdir(self) | |
303 | if pattern is not None: |
|
435 | if pattern is not None: | |
304 | names = fnmatch.filter(names, pattern) |
|
436 | names = fnmatch.filter(names, pattern) | |
305 | return [self / child for child in names] |
|
437 | return [self / child for child in names] | |
306 |
|
438 | |||
307 | def dirs(self, pattern=None): |
|
439 | def dirs(self, pattern=None): | |
308 | """ D.dirs() -> List of this directory's subdirectories. |
|
440 | """ D.dirs() -> List of this directory's subdirectories. | |
309 |
|
441 | |||
310 | The elements of the list are path objects. |
|
442 | The elements of the list are path objects. | |
311 | This does not walk recursively into subdirectories |
|
443 | This does not walk recursively into subdirectories | |
312 | (but see path.walkdirs). |
|
444 | (but see path.walkdirs). | |
313 |
|
445 | |||
314 | With the optional 'pattern' argument, this only lists |
|
446 | With the optional 'pattern' argument, this only lists | |
315 | directories whose names match the given pattern. For |
|
447 | directories whose names match the given pattern. For | |
316 | example, d.dirs('build-*'). |
|
448 | example, ``d.dirs('build-*')``. | |
317 | """ |
|
449 | """ | |
318 | return [p for p in self.listdir(pattern) if p.isdir()] |
|
450 | return [p for p in self.listdir(pattern) if p.isdir()] | |
319 |
|
451 | |||
320 | def files(self, pattern=None): |
|
452 | def files(self, pattern=None): | |
321 | """ D.files() -> List of the files in this directory. |
|
453 | """ D.files() -> List of the files in this directory. | |
322 |
|
454 | |||
323 | The elements of the list are path objects. |
|
455 | The elements of the list are path objects. | |
324 | This does not walk into subdirectories (see path.walkfiles). |
|
456 | This does not walk into subdirectories (see path.walkfiles). | |
325 |
|
457 | |||
326 | With the optional 'pattern' argument, this only lists files |
|
458 | With the optional 'pattern' argument, this only lists files | |
327 | whose names match the given pattern. For example, |
|
459 | whose names match the given pattern. For example, | |
328 | d.files('*.pyc'). |
|
460 | ``d.files('*.pyc')``. | |
329 | """ |
|
461 | """ | |
330 |
|
462 | |||
331 | return [p for p in self.listdir(pattern) if p.isfile()] |
|
463 | return [p for p in self.listdir(pattern) if p.isfile()] | |
332 |
|
464 | |||
333 | def walk(self, pattern=None, errors='strict'): |
|
465 | def walk(self, pattern=None, errors='strict'): | |
334 | """ D.walk() -> iterator over files and subdirs, recursively. |
|
466 | """ D.walk() -> iterator over files and subdirs, recursively. | |
335 |
|
467 | |||
336 | The iterator yields path objects naming each child item of |
|
468 | The iterator yields path objects naming each child item of | |
337 | this directory and its descendants. This requires that |
|
469 | this directory and its descendants. This requires that | |
338 | D.isdir(). |
|
470 | D.isdir(). | |
339 |
|
471 | |||
340 | This performs a depth-first traversal of the directory tree. |
|
472 | This performs a depth-first traversal of the directory tree. | |
341 | Each directory is returned just before all its children. |
|
473 | Each directory is returned just before all its children. | |
342 |
|
474 | |||
343 | The errors= keyword argument controls behavior when an |
|
475 | The errors= keyword argument controls behavior when an | |
344 | error occurs. The default is 'strict', which causes an |
|
476 | error occurs. The default is 'strict', which causes an | |
345 | exception. The other allowed values are 'warn', which |
|
477 | exception. The other allowed values are 'warn', which | |
346 | reports the error via warnings.warn(), and 'ignore'. |
|
478 | reports the error via warnings.warn(), and 'ignore'. | |
347 | """ |
|
479 | """ | |
348 | if errors not in ('strict', 'warn', 'ignore'): |
|
480 | if errors not in ('strict', 'warn', 'ignore'): | |
349 | raise ValueError("invalid errors parameter") |
|
481 | raise ValueError("invalid errors parameter") | |
350 |
|
482 | |||
351 | try: |
|
483 | try: | |
352 | childList = self.listdir() |
|
484 | childList = self.listdir() | |
353 | except Exception: |
|
485 | except Exception: | |
354 | if errors == 'ignore': |
|
486 | if errors == 'ignore': | |
355 | return |
|
487 | return | |
356 | elif errors == 'warn': |
|
488 | elif errors == 'warn': | |
357 | warnings.warn( |
|
489 | warnings.warn( | |
358 | "Unable to list directory '%s': %s" |
|
490 | "Unable to list directory '%s': %s" | |
359 | % (self, sys.exc_info()[1]), |
|
491 | % (self, sys.exc_info()[1]), | |
360 | TreeWalkWarning) |
|
492 | TreeWalkWarning) | |
361 | return |
|
493 | return | |
362 | else: |
|
494 | else: | |
363 | raise |
|
495 | raise | |
364 |
|
496 | |||
365 | for child in childList: |
|
497 | for child in childList: | |
366 | if pattern is None or child.fnmatch(pattern): |
|
498 | if pattern is None or child.fnmatch(pattern): | |
367 | yield child |
|
499 | yield child | |
368 | try: |
|
500 | try: | |
369 | isdir = child.isdir() |
|
501 | isdir = child.isdir() | |
370 | except Exception: |
|
502 | except Exception: | |
371 | if errors == 'ignore': |
|
503 | if errors == 'ignore': | |
372 | isdir = False |
|
504 | isdir = False | |
373 | elif errors == 'warn': |
|
505 | elif errors == 'warn': | |
374 | warnings.warn( |
|
506 | warnings.warn( | |
375 | "Unable to access '%s': %s" |
|
507 | "Unable to access '%s': %s" | |
376 | % (child, sys.exc_info()[1]), |
|
508 | % (child, sys.exc_info()[1]), | |
377 | TreeWalkWarning) |
|
509 | TreeWalkWarning) | |
378 | isdir = False |
|
510 | isdir = False | |
379 | else: |
|
511 | else: | |
380 | raise |
|
512 | raise | |
381 |
|
513 | |||
382 | if isdir: |
|
514 | if isdir: | |
383 | for item in child.walk(pattern, errors): |
|
515 | for item in child.walk(pattern, errors): | |
384 | yield item |
|
516 | yield item | |
385 |
|
517 | |||
386 | def walkdirs(self, pattern=None, errors='strict'): |
|
518 | def walkdirs(self, pattern=None, errors='strict'): | |
387 | """ D.walkdirs() -> iterator over subdirs, recursively. |
|
519 | """ D.walkdirs() -> iterator over subdirs, recursively. | |
388 |
|
520 | |||
389 | With the optional 'pattern' argument, this yields only |
|
521 | With the optional 'pattern' argument, this yields only | |
390 | directories whose names match the given pattern. For |
|
522 | directories whose names match the given pattern. For | |
391 | example, mydir.walkdirs('*test') yields only directories |
|
523 | example, ``mydir.walkdirs('*test')`` yields only directories | |
392 | with names ending in 'test'. |
|
524 | with names ending in 'test'. | |
393 |
|
525 | |||
394 | The errors= keyword argument controls behavior when an |
|
526 | The errors= keyword argument controls behavior when an | |
395 | error occurs. The default is 'strict', which causes an |
|
527 | error occurs. The default is 'strict', which causes an | |
396 | exception. The other allowed values are 'warn', which |
|
528 | exception. The other allowed values are 'warn', which | |
397 | reports the error via warnings.warn(), and 'ignore'. |
|
529 | reports the error via warnings.warn(), and 'ignore'. | |
398 | """ |
|
530 | """ | |
399 | if errors not in ('strict', 'warn', 'ignore'): |
|
531 | if errors not in ('strict', 'warn', 'ignore'): | |
400 | raise ValueError("invalid errors parameter") |
|
532 | raise ValueError("invalid errors parameter") | |
401 |
|
533 | |||
402 | try: |
|
534 | try: | |
403 | dirs = self.dirs() |
|
535 | dirs = self.dirs() | |
404 | except Exception: |
|
536 | except Exception: | |
405 | if errors == 'ignore': |
|
537 | if errors == 'ignore': | |
406 | return |
|
538 | return | |
407 | elif errors == 'warn': |
|
539 | elif errors == 'warn': | |
408 | warnings.warn( |
|
540 | warnings.warn( | |
409 | "Unable to list directory '%s': %s" |
|
541 | "Unable to list directory '%s': %s" | |
410 | % (self, sys.exc_info()[1]), |
|
542 | % (self, sys.exc_info()[1]), | |
411 | TreeWalkWarning) |
|
543 | TreeWalkWarning) | |
412 | return |
|
544 | return | |
413 | else: |
|
545 | else: | |
414 | raise |
|
546 | raise | |
415 |
|
547 | |||
416 | for child in dirs: |
|
548 | for child in dirs: | |
417 | if pattern is None or child.fnmatch(pattern): |
|
549 | if pattern is None or child.fnmatch(pattern): | |
418 | yield child |
|
550 | yield child | |
419 | for subsubdir in child.walkdirs(pattern, errors): |
|
551 | for subsubdir in child.walkdirs(pattern, errors): | |
420 | yield subsubdir |
|
552 | yield subsubdir | |
421 |
|
553 | |||
422 | def walkfiles(self, pattern=None, errors='strict'): |
|
554 | def walkfiles(self, pattern=None, errors='strict'): | |
423 | """ D.walkfiles() -> iterator over files in D, recursively. |
|
555 | """ D.walkfiles() -> iterator over files in D, recursively. | |
424 |
|
556 | |||
425 | The optional argument, pattern, limits the results to files |
|
557 | The optional argument, pattern, limits the results to files | |
426 | with names that match the pattern. For example, |
|
558 | with names that match the pattern. For example, | |
427 | mydir.walkfiles('*.tmp') yields only files with the .tmp |
|
559 | ``mydir.walkfiles('*.tmp')`` yields only files with the .tmp | |
428 | extension. |
|
560 | extension. | |
429 | """ |
|
561 | """ | |
430 | if errors not in ('strict', 'warn', 'ignore'): |
|
562 | if errors not in ('strict', 'warn', 'ignore'): | |
431 | raise ValueError("invalid errors parameter") |
|
563 | raise ValueError("invalid errors parameter") | |
432 |
|
564 | |||
433 | try: |
|
565 | try: | |
434 | childList = self.listdir() |
|
566 | childList = self.listdir() | |
435 | except Exception: |
|
567 | except Exception: | |
436 | if errors == 'ignore': |
|
568 | if errors == 'ignore': | |
437 | return |
|
569 | return | |
438 | elif errors == 'warn': |
|
570 | elif errors == 'warn': | |
439 | warnings.warn( |
|
571 | warnings.warn( | |
440 | "Unable to list directory '%s': %s" |
|
572 | "Unable to list directory '%s': %s" | |
441 | % (self, sys.exc_info()[1]), |
|
573 | % (self, sys.exc_info()[1]), | |
442 | TreeWalkWarning) |
|
574 | TreeWalkWarning) | |
443 | return |
|
575 | return | |
444 | else: |
|
576 | else: | |
445 | raise |
|
577 | raise | |
446 |
|
578 | |||
447 | for child in childList: |
|
579 | for child in childList: | |
448 | try: |
|
580 | try: | |
449 | isfile = child.isfile() |
|
581 | isfile = child.isfile() | |
450 | isdir = not isfile and child.isdir() |
|
582 | isdir = not isfile and child.isdir() | |
451 | except: |
|
583 | except: | |
452 | if errors == 'ignore': |
|
584 | if errors == 'ignore': | |
453 | continue |
|
585 | continue | |
454 | elif errors == 'warn': |
|
586 | elif errors == 'warn': | |
455 | warnings.warn( |
|
587 | warnings.warn( | |
456 | "Unable to access '%s': %s" |
|
588 | "Unable to access '%s': %s" | |
457 | % (self, sys.exc_info()[1]), |
|
589 | % (self, sys.exc_info()[1]), | |
458 | TreeWalkWarning) |
|
590 | TreeWalkWarning) | |
459 | continue |
|
591 | continue | |
460 | else: |
|
592 | else: | |
461 | raise |
|
593 | raise | |
462 |
|
594 | |||
463 | if isfile: |
|
595 | if isfile: | |
464 | if pattern is None or child.fnmatch(pattern): |
|
596 | if pattern is None or child.fnmatch(pattern): | |
465 | yield child |
|
597 | yield child | |
466 | elif isdir: |
|
598 | elif isdir: | |
467 | for f in child.walkfiles(pattern, errors): |
|
599 | for f in child.walkfiles(pattern, errors): | |
468 | yield f |
|
600 | yield f | |
469 |
|
601 | |||
470 | def fnmatch(self, pattern): |
|
602 | def fnmatch(self, pattern): | |
471 | """ Return True if self.name matches the given pattern. |
|
603 | """ Return True if self.name matches the given pattern. | |
472 |
|
604 | |||
473 | pattern - A filename pattern with wildcards, |
|
605 | pattern - A filename pattern with wildcards, | |
474 | for example '*.py'. |
|
606 | for example ``'*.py'``. | |
475 | """ |
|
607 | """ | |
476 | return fnmatch.fnmatch(self.name, pattern) |
|
608 | return fnmatch.fnmatch(self.name, pattern) | |
477 |
|
609 | |||
478 | def glob(self, pattern): |
|
610 | def glob(self, pattern): | |
479 | """ Return a list of path objects that match the pattern. |
|
611 | """ Return a list of path objects that match the pattern. | |
480 |
|
612 | |||
481 | pattern - a path relative to this directory, with wildcards. |
|
613 | pattern - a path relative to this directory, with wildcards. | |
482 |
|
614 | |||
483 | For example, path('/users').glob('*/bin/*') returns a list |
|
615 | For example, path('/users').glob('*/bin/*') returns a list | |
484 | of all the files users have in their bin directories. |
|
616 | of all the files users have in their bin directories. | |
485 | """ |
|
617 | """ | |
486 |
cls = self.__class |
|
618 | cls = self._next_class | |
487 |
return [cls(s) for s in glob.glob( |
|
619 | return [cls(s) for s in glob.glob(self / pattern)] | |
488 |
|
||||
489 |
|
620 | |||
|
621 | # | |||
490 | # --- Reading or writing an entire file at once. |
|
622 | # --- Reading or writing an entire file at once. | |
491 |
|
623 | |||
492 |
def open(self, |
|
624 | def open(self, *args, **kwargs): | |
493 | """ Open this file. Return a file object. """ |
|
625 | """ Open this file. Return a file object. """ | |
494 |
return open(self, |
|
626 | return open(self, *args, **kwargs) | |
495 |
|
627 | |||
496 | def bytes(self): |
|
628 | def bytes(self): | |
497 | """ Open this file, read all bytes, return them as a string. """ |
|
629 | """ Open this file, read all bytes, return them as a string. """ | |
498 |
|
|
630 | with self.open('rb') as f: | |
499 | try: |
|
|||
500 | return f.read() |
|
631 | return f.read() | |
501 | finally: |
|
632 | ||
502 | f.close() |
|
633 | def chunks(self, size, *args, **kwargs): | |
|
634 | """ Returns a generator yielding chunks of the file, so it can | |||
|
635 | be read piece by piece with a simple for loop. | |||
|
636 | ||||
|
637 | Any argument you pass after `size` will be passed to `open()`. | |||
|
638 | ||||
|
639 | :example: | |||
|
640 | ||||
|
641 | >>> for chunk in path("file.txt").chunk(8192): | |||
|
642 | ... print(chunk) | |||
|
643 | ||||
|
644 | This will read the file by chunks of 8192 bytes. | |||
|
645 | """ | |||
|
646 | with open(self, *args, **kwargs) as f: | |||
|
647 | while True: | |||
|
648 | d = f.read(size) | |||
|
649 | if not d: | |||
|
650 | break | |||
|
651 | yield d | |||
503 |
|
652 | |||
504 | def write_bytes(self, bytes, append=False): |
|
653 | def write_bytes(self, bytes, append=False): | |
505 | """ Open this file and write the given bytes to it. |
|
654 | """ Open this file and write the given bytes to it. | |
506 |
|
655 | |||
507 | Default behavior is to overwrite any existing file. |
|
656 | Default behavior is to overwrite any existing file. | |
508 | Call p.write_bytes(bytes, append=True) to append instead. |
|
657 | Call p.write_bytes(bytes, append=True) to append instead. | |
509 | """ |
|
658 | """ | |
510 | if append: |
|
659 | if append: | |
511 | mode = 'ab' |
|
660 | mode = 'ab' | |
512 | else: |
|
661 | else: | |
513 | mode = 'wb' |
|
662 | mode = 'wb' | |
514 |
|
|
663 | with self.open(mode) as f: | |
515 | try: |
|
|||
516 | f.write(bytes) |
|
664 | f.write(bytes) | |
517 | finally: |
|
|||
518 | f.close() |
|
|||
519 |
|
665 | |||
520 | def text(self, encoding=None, errors='strict'): |
|
666 | def text(self, encoding=None, errors='strict'): | |
521 | r""" Open this file, read it in, return the content as a string. |
|
667 | r""" Open this file, read it in, return the content as a string. | |
522 |
|
668 | |||
523 |
This uses 'U' mode |
|
669 | This method uses 'U' mode, so '\r\n' and '\r' are automatically | |
524 |
|
|
670 | translated to '\n'. | |
525 |
|
671 | |||
526 | Optional arguments: |
|
672 | Optional arguments: | |
527 |
|
673 | |||
528 | encoding - The Unicode encoding (or character set) of |
|
674 | encoding - The Unicode encoding (or character set) of | |
529 | the file. If present, the content of the file is |
|
675 | the file. If present, the content of the file is | |
530 | decoded and returned as a unicode object; otherwise |
|
676 | decoded and returned as a unicode object; otherwise | |
531 | it is returned as an 8-bit str. |
|
677 | it is returned as an 8-bit str. | |
532 | errors - How to handle Unicode errors; see help(str.decode) |
|
678 | errors - How to handle Unicode errors; see help(str.decode) | |
533 | for the options. Default is 'strict'. |
|
679 | for the options. Default is 'strict'. | |
534 | """ |
|
680 | """ | |
535 | if encoding is None: |
|
681 | if encoding is None: | |
536 | # 8-bit |
|
682 | # 8-bit | |
537 |
|
|
683 | with self.open('U') as f: | |
538 | try: |
|
|||
539 | return f.read() |
|
684 | return f.read() | |
540 | finally: |
|
|||
541 | f.close() |
|
|||
542 | else: |
|
685 | else: | |
543 | # Unicode |
|
686 | # Unicode | |
544 |
|
|
687 | with codecs.open(self, 'r', encoding, errors) as f: | |
545 | # (Note - Can't use 'U' mode here, since codecs.open |
|
688 | # (Note - Can't use 'U' mode here, since codecs.open | |
546 |
# doesn't support 'U' mode |
|
689 | # doesn't support 'U' mode.) | |
547 | try: |
|
|||
548 | t = f.read() |
|
690 | t = f.read() | |
549 | finally: |
|
691 | return (t.replace(u('\r\n'), u('\n')) | |
550 | f.close() |
|
692 | .replace(u('\r\x85'), u('\n')) | |
551 |
|
|
693 | .replace(u('\r'), u('\n')) | |
552 |
.replace(u' |
|
694 | .replace(u('\x85'), u('\n')) | |
553 |
.replace(u'\ |
|
695 | .replace(u('\u2028'), u('\n'))) | |
554 | .replace(u'\x85', u'\n') |
|
696 | ||
555 | .replace(u'\u2028', u'\n')) |
|
697 | def write_text(self, text, encoding=None, errors='strict', | |
556 |
|
698 | linesep=os.linesep, append=False): | ||
557 | def write_text(self, text, encoding=None, errors='strict', linesep=os.linesep, append=False): |
|
|||
558 | r""" Write the given text to this file. |
|
699 | r""" Write the given text to this file. | |
559 |
|
700 | |||
560 | The default behavior is to overwrite any existing file; |
|
701 | The default behavior is to overwrite any existing file; | |
561 | to append instead, use the 'append=True' keyword argument. |
|
702 | to append instead, use the 'append=True' keyword argument. | |
562 |
|
703 | |||
563 | There are two differences between path.write_text() and |
|
704 | There are two differences between path.write_text() and | |
564 | path.write_bytes(): newline handling and Unicode handling. |
|
705 | path.write_bytes(): newline handling and Unicode handling. | |
565 | See below. |
|
706 | See below. | |
566 |
|
707 | |||
567 | Parameters: |
|
708 | Parameters: | |
568 |
|
709 | |||
569 | - text - str/unicode - The text to be written. |
|
710 | - text - str/unicode - The text to be written. | |
570 |
|
711 | |||
571 | - encoding - str - The Unicode encoding that will be used. |
|
712 | - encoding - str - The Unicode encoding that will be used. | |
572 | This is ignored if 'text' isn't a Unicode string. |
|
713 | This is ignored if 'text' isn't a Unicode string. | |
573 |
|
714 | |||
574 | - errors - str - How to handle Unicode encoding errors. |
|
715 | - errors - str - How to handle Unicode encoding errors. | |
575 | Default is 'strict'. See help(unicode.encode) for the |
|
716 | Default is 'strict'. See help(unicode.encode) for the | |
576 | options. This is ignored if 'text' isn't a Unicode |
|
717 | options. This is ignored if 'text' isn't a Unicode | |
577 | string. |
|
718 | string. | |
578 |
|
719 | |||
579 | - linesep - keyword argument - str/unicode - The sequence of |
|
720 | - linesep - keyword argument - str/unicode - The sequence of | |
580 | characters to be used to mark end-of-line. The default is |
|
721 | characters to be used to mark end-of-line. The default is | |
581 | os.linesep. You can also specify None; this means to |
|
722 | os.linesep. You can also specify None; this means to | |
582 | leave all newlines as they are in 'text'. |
|
723 | leave all newlines as they are in 'text'. | |
583 |
|
724 | |||
584 | - append - keyword argument - bool - Specifies what to do if |
|
725 | - append - keyword argument - bool - Specifies what to do if | |
585 | the file already exists (True: append to the end of it; |
|
726 | the file already exists (True: append to the end of it; | |
586 | False: overwrite it.) The default is False. |
|
727 | False: overwrite it.) The default is False. | |
587 |
|
728 | |||
588 |
|
729 | |||
589 | --- Newline handling. |
|
730 | --- Newline handling. | |
590 |
|
731 | |||
591 | write_text() converts all standard end-of-line sequences |
|
732 | write_text() converts all standard end-of-line sequences | |
592 | ('\n', '\r', and '\r\n') to your platform's default end-of-line |
|
733 | ('\n', '\r', and '\r\n') to your platform's default end-of-line | |
593 | sequence (see os.linesep; on Windows, for example, the |
|
734 | sequence (see os.linesep; on Windows, for example, the | |
594 | end-of-line marker is '\r\n'). |
|
735 | end-of-line marker is '\r\n'). | |
595 |
|
736 | |||
596 | If you don't like your platform's default, you can override it |
|
737 | If you don't like your platform's default, you can override it | |
597 | using the 'linesep=' keyword argument. If you specifically want |
|
738 | using the 'linesep=' keyword argument. If you specifically want | |
598 | write_text() to preserve the newlines as-is, use 'linesep=None'. |
|
739 | write_text() to preserve the newlines as-is, use 'linesep=None'. | |
599 |
|
740 | |||
600 | This applies to Unicode text the same as to 8-bit text, except |
|
741 | This applies to Unicode text the same as to 8-bit text, except | |
601 | there are three additional standard Unicode end-of-line sequences: |
|
742 | there are three additional standard Unicode end-of-line sequences: | |
602 | u'\x85', u'\r\x85', and u'\u2028'. |
|
743 | u'\x85', u'\r\x85', and u'\u2028'. | |
603 |
|
744 | |||
604 | (This is slightly different from when you open a file for |
|
745 | (This is slightly different from when you open a file for | |
605 | writing with fopen(filename, "w") in C or open(filename, 'w') |
|
746 | writing with fopen(filename, "w") in C or open(filename, 'w') | |
606 | in Python.) |
|
747 | in Python.) | |
607 |
|
748 | |||
608 |
|
749 | |||
609 | --- Unicode |
|
750 | --- Unicode | |
610 |
|
751 | |||
611 | If 'text' isn't Unicode, then apart from newline handling, the |
|
752 | If 'text' isn't Unicode, then apart from newline handling, the | |
612 | bytes are written verbatim to the file. The 'encoding' and |
|
753 | bytes are written verbatim to the file. The 'encoding' and | |
613 | 'errors' arguments are not used and must be omitted. |
|
754 | 'errors' arguments are not used and must be omitted. | |
614 |
|
755 | |||
615 | If 'text' is Unicode, it is first converted to bytes using the |
|
756 | If 'text' is Unicode, it is first converted to bytes using the | |
616 | specified 'encoding' (or the default encoding if 'encoding' |
|
757 | specified 'encoding' (or the default encoding if 'encoding' | |
617 | isn't specified). The 'errors' argument applies only to this |
|
758 | isn't specified). The 'errors' argument applies only to this | |
618 | conversion. |
|
759 | conversion. | |
619 |
|
760 | |||
620 | """ |
|
761 | """ | |
621 | if isinstance(text, unicode): |
|
762 | if isinstance(text, unicode): | |
622 | if linesep is not None: |
|
763 | if linesep is not None: | |
623 | # Convert all standard end-of-line sequences to |
|
764 | # Convert all standard end-of-line sequences to | |
624 | # ordinary newline characters. |
|
765 | # ordinary newline characters. | |
625 | text = (text.replace(u'\r\n', u'\n') |
|
766 | text = (text.replace(u('\r\n'), u('\n')) | |
626 | .replace(u'\r\x85', u'\n') |
|
767 | .replace(u('\r\x85'), u('\n')) | |
627 | .replace(u'\r', u'\n') |
|
768 | .replace(u('\r'), u('\n')) | |
628 | .replace(u'\x85', u'\n') |
|
769 | .replace(u('\x85'), u('\n')) | |
629 | .replace(u'\u2028', u'\n')) |
|
770 | .replace(u('\u2028'), u('\n'))) | |
630 | text = text.replace(u'\n', linesep) |
|
771 | text = text.replace(u('\n'), linesep) | |
631 | if encoding is None: |
|
772 | if encoding is None: | |
632 | encoding = sys.getdefaultencoding() |
|
773 | encoding = sys.getdefaultencoding() | |
633 | bytes = text.encode(encoding, errors) |
|
774 | bytes = text.encode(encoding, errors) | |
634 | else: |
|
775 | else: | |
635 | # It is an error to specify an encoding if 'text' is |
|
776 | # It is an error to specify an encoding if 'text' is | |
636 | # an 8-bit string. |
|
777 | # an 8-bit string. | |
637 | assert encoding is None |
|
778 | assert encoding is None | |
638 |
|
779 | |||
639 | if linesep is not None: |
|
780 | if linesep is not None: | |
640 | text = (text.replace('\r\n', '\n') |
|
781 | text = (text.replace('\r\n', '\n') | |
641 | .replace('\r', '\n')) |
|
782 | .replace('\r', '\n')) | |
642 | bytes = text.replace('\n', linesep) |
|
783 | bytes = text.replace('\n', linesep) | |
643 |
|
784 | |||
644 | self.write_bytes(bytes, append) |
|
785 | self.write_bytes(bytes, append) | |
645 |
|
786 | |||
646 | def lines(self, encoding=None, errors='strict', retain=True): |
|
787 | def lines(self, encoding=None, errors='strict', retain=True): | |
647 | r""" Open this file, read all lines, return them in a list. |
|
788 | r""" Open this file, read all lines, return them in a list. | |
648 |
|
789 | |||
649 | Optional arguments: |
|
790 | Optional arguments: | |
650 | encoding - The Unicode encoding (or character set) of |
|
791 | encoding - The Unicode encoding (or character set) of | |
651 | the file. The default is None, meaning the content |
|
792 | the file. The default is None, meaning the content | |
652 | of the file is read as 8-bit characters and returned |
|
793 | of the file is read as 8-bit characters and returned | |
653 | as a list of (non-Unicode) str objects. |
|
794 | as a list of (non-Unicode) str objects. | |
654 | errors - How to handle Unicode errors; see help(str.decode) |
|
795 | errors - How to handle Unicode errors; see help(str.decode) | |
655 | for the options. Default is 'strict' |
|
796 | for the options. Default is 'strict' | |
656 | retain - If true, retain newline characters; but all newline |
|
797 | retain - If true, retain newline characters; but all newline | |
657 | character combinations ('\r', '\n', '\r\n') are |
|
798 | character combinations ('\r', '\n', '\r\n') are | |
658 | translated to '\n'. If false, newline characters are |
|
799 | translated to '\n'. If false, newline characters are | |
659 | stripped off. Default is True. |
|
800 | stripped off. Default is True. | |
660 |
|
801 | |||
661 |
This uses 'U' mode |
|
802 | This uses 'U' mode. | |
662 | """ |
|
803 | """ | |
663 | if encoding is None and retain: |
|
804 | if encoding is None and retain: | |
664 |
|
|
805 | with self.open('U') as f: | |
665 | try: |
|
|||
666 | return f.readlines() |
|
806 | return f.readlines() | |
667 | finally: |
|
|||
668 | f.close() |
|
|||
669 | else: |
|
807 | else: | |
670 | return self.text(encoding, errors).splitlines(retain) |
|
808 | return self.text(encoding, errors).splitlines(retain) | |
671 |
|
809 | |||
672 | def write_lines(self, lines, encoding=None, errors='strict', |
|
810 | def write_lines(self, lines, encoding=None, errors='strict', | |
673 | linesep=os.linesep, append=False): |
|
811 | linesep=os.linesep, append=False): | |
674 | r""" Write the given lines of text to this file. |
|
812 | r""" Write the given lines of text to this file. | |
675 |
|
813 | |||
676 | By default this overwrites any existing file at this path. |
|
814 | By default this overwrites any existing file at this path. | |
677 |
|
815 | |||
678 | This puts a platform-specific newline sequence on every line. |
|
816 | This puts a platform-specific newline sequence on every line. | |
679 | See 'linesep' below. |
|
817 | See 'linesep' below. | |
680 |
|
818 | |||
681 | lines - A list of strings. |
|
819 | lines - A list of strings. | |
682 |
|
820 | |||
683 | encoding - A Unicode encoding to use. This applies only if |
|
821 | encoding - A Unicode encoding to use. This applies only if | |
684 | 'lines' contains any Unicode strings. |
|
822 | 'lines' contains any Unicode strings. | |
685 |
|
823 | |||
686 | errors - How to handle errors in Unicode encoding. This |
|
824 | errors - How to handle errors in Unicode encoding. This | |
687 | also applies only to Unicode strings. |
|
825 | also applies only to Unicode strings. | |
688 |
|
826 | |||
689 | linesep - The desired line-ending. This line-ending is |
|
827 | linesep - The desired line-ending. This line-ending is | |
690 | applied to every line. If a line already has any |
|
828 | applied to every line. If a line already has any | |
691 | standard line ending ('\r', '\n', '\r\n', u'\x85', |
|
829 | standard line ending ('\r', '\n', '\r\n', u'\x85', | |
692 | u'\r\x85', u'\u2028'), that will be stripped off and |
|
830 | u'\r\x85', u'\u2028'), that will be stripped off and | |
693 | this will be used instead. The default is os.linesep, |
|
831 | this will be used instead. The default is os.linesep, | |
694 | which is platform-dependent ('\r\n' on Windows, '\n' on |
|
832 | which is platform-dependent ('\r\n' on Windows, '\n' on | |
695 | Unix, etc.) Specify None to write the lines as-is, |
|
833 | Unix, etc.) Specify None to write the lines as-is, | |
696 | like file.writelines(). |
|
834 | like file.writelines(). | |
697 |
|
835 | |||
698 | Use the keyword argument append=True to append lines to the |
|
836 | Use the keyword argument append=True to append lines to the | |
699 | file. The default is to overwrite the file. Warning: |
|
837 | file. The default is to overwrite the file. Warning: | |
700 | When you use this with Unicode data, if the encoding of the |
|
838 | When you use this with Unicode data, if the encoding of the | |
701 | existing data in the file is different from the encoding |
|
839 | existing data in the file is different from the encoding | |
702 | you specify with the encoding= parameter, the result is |
|
840 | you specify with the encoding= parameter, the result is | |
703 | mixed-encoding data, which can really confuse someone trying |
|
841 | mixed-encoding data, which can really confuse someone trying | |
704 | to read the file later. |
|
842 | to read the file later. | |
705 | """ |
|
843 | """ | |
706 | if append: |
|
844 | if append: | |
707 | mode = 'ab' |
|
845 | mode = 'ab' | |
708 | else: |
|
846 | else: | |
709 | mode = 'wb' |
|
847 | mode = 'wb' | |
710 |
|
|
848 | with self.open(mode) as f: | |
711 | try: |
|
|||
712 | for line in lines: |
|
849 | for line in lines: | |
713 | isUnicode = isinstance(line, unicode) |
|
850 | isUnicode = isinstance(line, unicode) | |
714 | if linesep is not None: |
|
851 | if linesep is not None: | |
715 | # Strip off any existing line-end and add the |
|
852 | # Strip off any existing line-end and add the | |
716 | # specified linesep string. |
|
853 | # specified linesep string. | |
717 | if isUnicode: |
|
854 | if isUnicode: | |
718 | if line[-2:] in (u'\r\n', u'\x0d\x85'): |
|
855 | if line[-2:] in (u('\r\n'), u('\x0d\x85')): | |
719 | line = line[:-2] |
|
856 | line = line[:-2] | |
720 | elif line[-1:] in (u'\r', u'\n', |
|
857 | elif line[-1:] in (u('\r'), u('\n'), | |
721 | u'\x85', u'\u2028'): |
|
858 | u('\x85'), u('\u2028')): | |
722 | line = line[:-1] |
|
859 | line = line[:-1] | |
723 | else: |
|
860 | else: | |
724 | if line[-2:] == '\r\n': |
|
861 | if line[-2:] == '\r\n': | |
725 | line = line[:-2] |
|
862 | line = line[:-2] | |
726 | elif line[-1:] in ('\r', '\n'): |
|
863 | elif line[-1:] in ('\r', '\n'): | |
727 | line = line[:-1] |
|
864 | line = line[:-1] | |
728 | line += linesep |
|
865 | line += linesep | |
729 | if isUnicode: |
|
866 | if isUnicode: | |
730 | if encoding is None: |
|
867 | if encoding is None: | |
731 | encoding = sys.getdefaultencoding() |
|
868 | encoding = sys.getdefaultencoding() | |
732 | line = line.encode(encoding, errors) |
|
869 | line = line.encode(encoding, errors) | |
733 | f.write(line) |
|
870 | f.write(line) | |
734 | finally: |
|
|||
735 | f.close() |
|
|||
736 |
|
871 | |||
737 | def read_md5(self): |
|
872 | def read_md5(self): | |
738 | """ Calculate the md5 hash for this file. |
|
873 | """ Calculate the md5 hash for this file. | |
739 |
|
874 | |||
740 | This reads through the entire file. |
|
875 | This reads through the entire file. | |
741 | """ |
|
876 | """ | |
742 |
|
|
877 | return self.read_hash('md5') | |
743 | try: |
|
878 | ||
744 | m = md5() |
|
879 | def _hash(self, hash_name): | |
745 | while True: |
|
880 | """ Returns a hash object for the file at the current path. | |
746 | d = f.read(8192) |
|
881 | ||
747 | if not d: |
|
882 | `hash_name` should be a hash algo name such as 'md5' or 'sha1' | |
748 | break |
|
883 | that's available in the `hashlib` module. | |
749 | m.update(d) |
|
884 | """ | |
750 | finally: |
|
885 | m = hashlib.new(hash_name) | |
751 | f.close() |
|
886 | for chunk in self.chunks(8192): | |
752 | return m.digest() |
|
887 | m.update(chunk) | |
|
888 | return m | |||
|
889 | ||||
|
890 | def read_hash(self, hash_name): | |||
|
891 | """ Calculate given hash for this file. | |||
|
892 | ||||
|
893 | List of supported hashes can be obtained from hashlib package. This | |||
|
894 | reads the entire file. | |||
|
895 | """ | |||
|
896 | return self._hash(hash_name).digest() | |||
|
897 | ||||
|
898 | def read_hexhash(self, hash_name): | |||
|
899 | """ Calculate given hash for this file, returning hexdigest. | |||
|
900 | ||||
|
901 | List of supported hashes can be obtained from hashlib package. This | |||
|
902 | reads the entire file. | |||
|
903 | """ | |||
|
904 | return self._hash(hash_name).hexdigest() | |||
753 |
|
905 | |||
754 | # --- Methods for querying the filesystem. |
|
906 | # --- Methods for querying the filesystem. | |
755 | # N.B. We can't assign the functions directly, because they may on some |
|
907 | # N.B. On some platforms, the os.path functions may be implemented in C | |
756 |
# |
|
908 | # (e.g. isdir on Windows, Python 3.2.2), and compiled functions don't get | |
757 | # See gh-737 for discussion of this. |
|
909 | # bound. Playing it safe and wrapping them all in method calls. | |
758 |
|
910 | |||
759 | def exists(s): return os.path.exists(s) |
|
911 | def isabs(self): | |
760 | def isdir(s): return os.path.isdir(s) |
|
912 | return self.module.isabs(self) | |
761 | def isfile(s): return os.path.isfile(s) |
|
|||
762 | def islink(s): return os.path.islink(s) |
|
|||
763 | def ismount(s): return os.path.ismount(s) |
|
|||
764 |
|
913 | |||
765 | if hasattr(os.path, 'samefile'): |
|
914 | def exists(self): | |
766 | def samefile(s, o): return os.path.samefile(s, o) |
|
915 | return self.module.exists(self) | |
|
916 | ||||
|
917 | def isdir(self): | |||
|
918 | return self.module.isdir(self) | |||
|
919 | ||||
|
920 | def isfile(self): | |||
|
921 | return self.module.isfile(self) | |||
|
922 | ||||
|
923 | def islink(self): | |||
|
924 | return self.module.islink(self) | |||
|
925 | ||||
|
926 | def ismount(self): | |||
|
927 | return self.module.ismount(self) | |||
|
928 | ||||
|
929 | def samefile(self, other): | |||
|
930 | return self.module.samefile(self, other) | |||
|
931 | ||||
|
932 | def getatime(self): | |||
|
933 | return self.module.getatime(self) | |||
767 |
|
934 | |||
768 | def getatime(s): return os.path.getatime(s) |
|
|||
769 | atime = property( |
|
935 | atime = property( | |
770 | getatime, None, None, |
|
936 | getatime, None, None, | |
771 | """ Last access time of the file. """) |
|
937 | """ Last access time of the file. """) | |
772 |
|
938 | |||
773 | def getmtime(s): return os.path.getmtime(s) |
|
939 | def getmtime(self): | |
|
940 | return self.module.getmtime(self) | |||
|
941 | ||||
774 | mtime = property( |
|
942 | mtime = property( | |
775 | getmtime, None, None, |
|
943 | getmtime, None, None, | |
776 | """ Last-modified time of the file. """) |
|
944 | """ Last-modified time of the file. """) | |
777 |
|
945 | |||
778 | if hasattr(os.path, 'getctime'): |
|
946 | def getctime(self): | |
779 |
|
|
947 | return self.module.getctime(self) | |
780 | ctime = property( |
|
948 | ||
781 | getctime, None, None, |
|
949 | ctime = property( | |
782 | """ Creation time of the file. """) |
|
950 | getctime, None, None, | |
|
951 | """ Creation time of the file. """) | |||
|
952 | ||||
|
953 | def getsize(self): | |||
|
954 | return self.module.getsize(self) | |||
783 |
|
955 | |||
784 | def getsize(s): return os.path.getsize(s) |
|
|||
785 | size = property( |
|
956 | size = property( | |
786 | getsize, None, None, |
|
957 | getsize, None, None, | |
787 | """ Size of the file, in bytes. """) |
|
958 | """ Size of the file, in bytes. """) | |
788 |
|
959 | |||
789 | if hasattr(os, 'access'): |
|
960 | if hasattr(os, 'access'): | |
790 | def access(self, mode): |
|
961 | def access(self, mode): | |
791 | """ Return true if current user has access to this path. |
|
962 | """ Return true if current user has access to this path. | |
792 |
|
963 | |||
793 | mode - One of the constants os.F_OK, os.R_OK, os.W_OK, os.X_OK |
|
964 | mode - One of the constants os.F_OK, os.R_OK, os.W_OK, os.X_OK | |
794 | """ |
|
965 | """ | |
795 | return os.access(self, mode) |
|
966 | return os.access(self, mode) | |
796 |
|
967 | |||
797 | def stat(self): |
|
968 | def stat(self): | |
798 | """ Perform a stat() system call on this path. """ |
|
969 | """ Perform a stat() system call on this path. """ | |
799 | return os.stat(self) |
|
970 | return os.stat(self) | |
800 |
|
971 | |||
801 | def lstat(self): |
|
972 | def lstat(self): | |
802 | """ Like path.stat(), but do not follow symbolic links. """ |
|
973 | """ Like path.stat(), but do not follow symbolic links. """ | |
803 | return os.lstat(self) |
|
974 | return os.lstat(self) | |
804 |
|
975 | |||
805 | def get_owner(self): |
|
976 | def __get_owner_windows(self): | |
806 | r""" Return the name of the owner of this file or directory. |
|
977 | r""" | |
|
978 | Return the name of the owner of this file or directory. Follow | |||
|
979 | symbolic links. | |||
807 |
|
980 | |||
808 | This follows symbolic links. |
|
981 | Return a name of the form ur'DOMAIN\User Name'; may be a group. | |
|
982 | """ | |||
|
983 | desc = win32security.GetFileSecurity( | |||
|
984 | self, win32security.OWNER_SECURITY_INFORMATION) | |||
|
985 | sid = desc.GetSecurityDescriptorOwner() | |||
|
986 | account, domain, typecode = win32security.LookupAccountSid(None, sid) | |||
|
987 | return domain + u('\\') + account | |||
809 |
|
988 | |||
810 | On Windows, this returns a name of the form ur'DOMAIN\User Name'. |
|
989 | def __get_owner_unix(self): | |
811 | On Windows, a group can own a file or directory. |
|
|||
812 |
|
|
990 | """ | |
813 | if os.name == 'nt': |
|
991 | Return the name of the owner of this file or directory. Follow | |
814 | if win32security is None: |
|
992 | symbolic links. | |
815 | raise Exception("path.owner requires win32all to be installed") |
|
993 | """ | |
816 | desc = win32security.GetFileSecurity( |
|
994 | st = self.stat() | |
817 | self, win32security.OWNER_SECURITY_INFORMATION) |
|
995 | return pwd.getpwuid(st.st_uid).pw_name | |
818 | sid = desc.GetSecurityDescriptorOwner() |
|
996 | ||
819 | account, domain, typecode = win32security.LookupAccountSid(None, sid) |
|
997 | def __get_owner_not_implemented(self): | |
820 | return domain + u'\\' + account |
|
998 | raise NotImplementedError("Ownership not available on this platform.") | |
821 | else: |
|
999 | ||
822 | if pwd is None: |
|
1000 | if 'win32security' in globals(): | |
823 | raise NotImplementedError("path.owner is not implemented on this platform.") |
|
1001 | get_owner = __get_owner_windows | |
824 | st = self.stat() |
|
1002 | elif 'pwd' in globals(): | |
825 | return pwd.getpwuid(st.st_uid).pw_name |
|
1003 | get_owner = __get_owner_unix | |
|
1004 | else: | |||
|
1005 | get_owner = __get_owner_not_implemented | |||
826 |
|
1006 | |||
827 | owner = property( |
|
1007 | owner = property( | |
828 | get_owner, None, None, |
|
1008 | get_owner, None, None, | |
829 | """ Name of the owner of this file or directory. """) |
|
1009 | """ Name of the owner of this file or directory. """) | |
830 |
|
1010 | |||
831 | if hasattr(os, 'statvfs'): |
|
1011 | if hasattr(os, 'statvfs'): | |
832 | def statvfs(self): |
|
1012 | def statvfs(self): | |
833 | """ Perform a statvfs() system call on this path. """ |
|
1013 | """ Perform a statvfs() system call on this path. """ | |
834 | return os.statvfs(self) |
|
1014 | return os.statvfs(self) | |
835 |
|
1015 | |||
836 | if hasattr(os, 'pathconf'): |
|
1016 | if hasattr(os, 'pathconf'): | |
837 | def pathconf(self, name): |
|
1017 | def pathconf(self, name): | |
838 | return os.pathconf(self, name) |
|
1018 | return os.pathconf(self, name) | |
839 |
|
1019 | |||
840 |
|
1020 | # | ||
841 | # --- Modifying operations on files and directories |
|
1021 | # --- Modifying operations on files and directories | |
842 |
|
1022 | |||
843 | def utime(self, times): |
|
1023 | def utime(self, times): | |
844 | """ Set the access and modified times of this file. """ |
|
1024 | """ Set the access and modified times of this file. """ | |
845 | os.utime(self, times) |
|
1025 | os.utime(self, times) | |
|
1026 | return self | |||
846 |
|
1027 | |||
847 | def chmod(self, mode): |
|
1028 | def chmod(self, mode): | |
848 | os.chmod(self, mode) |
|
1029 | os.chmod(self, mode) | |
|
1030 | return self | |||
849 |
|
1031 | |||
850 | if hasattr(os, 'chown'): |
|
1032 | if hasattr(os, 'chown'): | |
851 | def chown(self, uid, gid): |
|
1033 | def chown(self, uid=-1, gid=-1): | |
852 | os.chown(self, uid, gid) |
|
1034 | os.chown(self, uid, gid) | |
|
1035 | return self | |||
853 |
|
1036 | |||
854 | def rename(self, new): |
|
1037 | def rename(self, new): | |
855 | os.rename(self, new) |
|
1038 | os.rename(self, new) | |
|
1039 | return self._next_class(new) | |||
856 |
|
1040 | |||
857 | def renames(self, new): |
|
1041 | def renames(self, new): | |
858 | os.renames(self, new) |
|
1042 | os.renames(self, new) | |
|
1043 | return self._next_class(new) | |||
859 |
|
1044 | |||
860 |
|
1045 | # | ||
861 | # --- Create/delete operations on directories |
|
1046 | # --- Create/delete operations on directories | |
862 |
|
1047 | |||
863 |
def mkdir(self, mode= |
|
1048 | def mkdir(self, mode=o777): | |
864 | os.mkdir(self, mode) |
|
1049 | os.mkdir(self, mode) | |
|
1050 | return self | |||
|
1051 | ||||
|
1052 | def mkdir_p(self, mode=o777): | |||
|
1053 | try: | |||
|
1054 | self.mkdir(mode) | |||
|
1055 | except OSError: | |||
|
1056 | _, e, _ = sys.exc_info() | |||
|
1057 | if e.errno != errno.EEXIST: | |||
|
1058 | raise | |||
|
1059 | return self | |||
865 |
|
1060 | |||
866 |
def makedirs(self, mode= |
|
1061 | def makedirs(self, mode=o777): | |
867 | os.makedirs(self, mode) |
|
1062 | os.makedirs(self, mode) | |
|
1063 | return self | |||
|
1064 | ||||
|
1065 | def makedirs_p(self, mode=o777): | |||
|
1066 | try: | |||
|
1067 | self.makedirs(mode) | |||
|
1068 | except OSError: | |||
|
1069 | _, e, _ = sys.exc_info() | |||
|
1070 | if e.errno != errno.EEXIST: | |||
|
1071 | raise | |||
|
1072 | return self | |||
868 |
|
1073 | |||
869 | def rmdir(self): |
|
1074 | def rmdir(self): | |
870 | os.rmdir(self) |
|
1075 | os.rmdir(self) | |
|
1076 | return self | |||
|
1077 | ||||
|
1078 | def rmdir_p(self): | |||
|
1079 | try: | |||
|
1080 | self.rmdir() | |||
|
1081 | except OSError: | |||
|
1082 | _, e, _ = sys.exc_info() | |||
|
1083 | if e.errno != errno.ENOTEMPTY and e.errno != errno.EEXIST: | |||
|
1084 | raise | |||
|
1085 | return self | |||
871 |
|
1086 | |||
872 | def removedirs(self): |
|
1087 | def removedirs(self): | |
873 | os.removedirs(self) |
|
1088 | os.removedirs(self) | |
|
1089 | return self | |||
874 |
|
1090 | |||
|
1091 | def removedirs_p(self): | |||
|
1092 | try: | |||
|
1093 | self.removedirs() | |||
|
1094 | except OSError: | |||
|
1095 | _, e, _ = sys.exc_info() | |||
|
1096 | if e.errno != errno.ENOTEMPTY and e.errno != errno.EEXIST: | |||
|
1097 | raise | |||
|
1098 | return self | |||
875 |
|
1099 | |||
876 | # --- Modifying operations on files |
|
1100 | # --- Modifying operations on files | |
877 |
|
1101 | |||
878 | def touch(self): |
|
1102 | def touch(self): | |
879 | """ Set the access/modified times of this file to the current time. |
|
1103 | """ Set the access/modified times of this file to the current time. | |
880 | Create the file if it does not exist. |
|
1104 | Create the file if it does not exist. | |
881 | """ |
|
1105 | """ | |
882 |
fd = os.open(self, os.O_WRONLY | os.O_CREAT, |
|
1106 | fd = os.open(self, os.O_WRONLY | os.O_CREAT, o666) | |
883 | os.close(fd) |
|
1107 | os.close(fd) | |
884 | os.utime(self, None) |
|
1108 | os.utime(self, None) | |
|
1109 | return self | |||
885 |
|
1110 | |||
886 | def remove(self): |
|
1111 | def remove(self): | |
887 | os.remove(self) |
|
1112 | os.remove(self) | |
|
1113 | return self | |||
|
1114 | ||||
|
1115 | def remove_p(self): | |||
|
1116 | try: | |||
|
1117 | self.unlink() | |||
|
1118 | except OSError: | |||
|
1119 | _, e, _ = sys.exc_info() | |||
|
1120 | if e.errno != errno.ENOENT: | |||
|
1121 | raise | |||
|
1122 | return self | |||
888 |
|
1123 | |||
889 | def unlink(self): |
|
1124 | def unlink(self): | |
890 | os.unlink(self) |
|
1125 | os.unlink(self) | |
|
1126 | return self | |||
891 |
|
1127 | |||
|
1128 | def unlink_p(self): | |||
|
1129 | self.remove_p() | |||
|
1130 | return self | |||
892 |
|
1131 | |||
893 | # --- Links |
|
1132 | # --- Links | |
894 |
|
1133 | |||
895 | if hasattr(os, 'link'): |
|
1134 | if hasattr(os, 'link'): | |
896 | def link(self, newpath): |
|
1135 | def link(self, newpath): | |
897 | """ Create a hard link at 'newpath', pointing to this file. """ |
|
1136 | """ Create a hard link at 'newpath', pointing to this file. """ | |
898 | os.link(self, newpath) |
|
1137 | os.link(self, newpath) | |
|
1138 | return self._next_class(newpath) | |||
899 |
|
1139 | |||
900 | if hasattr(os, 'symlink'): |
|
1140 | if hasattr(os, 'symlink'): | |
901 | def symlink(self, newlink): |
|
1141 | def symlink(self, newlink): | |
902 | """ Create a symbolic link at 'newlink', pointing here. """ |
|
1142 | """ Create a symbolic link at 'newlink', pointing here. """ | |
903 | os.symlink(self, newlink) |
|
1143 | os.symlink(self, newlink) | |
|
1144 | return self._next_class(newlink) | |||
904 |
|
1145 | |||
905 | if hasattr(os, 'readlink'): |
|
1146 | if hasattr(os, 'readlink'): | |
906 | def readlink(self): |
|
1147 | def readlink(self): | |
907 | """ Return the path to which this symbolic link points. |
|
1148 | """ Return the path to which this symbolic link points. | |
908 |
|
1149 | |||
909 | The result may be an absolute or a relative path. |
|
1150 | The result may be an absolute or a relative path. | |
910 | """ |
|
1151 | """ | |
911 |
return self.__class |
|
1152 | return self._next_class(os.readlink(self)) | |
912 |
|
1153 | |||
913 | def readlinkabs(self): |
|
1154 | def readlinkabs(self): | |
914 | """ Return the path to which this symbolic link points. |
|
1155 | """ Return the path to which this symbolic link points. | |
915 |
|
1156 | |||
916 | The result is always an absolute path. |
|
1157 | The result is always an absolute path. | |
917 | """ |
|
1158 | """ | |
918 | p = self.readlink() |
|
1159 | p = self.readlink() | |
919 | if p.isabs(): |
|
1160 | if p.isabs(): | |
920 | return p |
|
1161 | return p | |
921 | else: |
|
1162 | else: | |
922 | return (self.parent / p).abspath() |
|
1163 | return (self.parent / p).abspath() | |
923 |
|
1164 | |||
924 |
|
1165 | # | ||
925 | # --- High-level functions from shutil |
|
1166 | # --- High-level functions from shutil | |
926 |
|
1167 | |||
927 | copyfile = shutil.copyfile |
|
1168 | copyfile = shutil.copyfile | |
928 | copymode = shutil.copymode |
|
1169 | copymode = shutil.copymode | |
929 | copystat = shutil.copystat |
|
1170 | copystat = shutil.copystat | |
930 | copy = shutil.copy |
|
1171 | copy = shutil.copy | |
931 | copy2 = shutil.copy2 |
|
1172 | copy2 = shutil.copy2 | |
932 | copytree = shutil.copytree |
|
1173 | copytree = shutil.copytree | |
933 | if hasattr(shutil, 'move'): |
|
1174 | if hasattr(shutil, 'move'): | |
934 | move = shutil.move |
|
1175 | move = shutil.move | |
935 | rmtree = shutil.rmtree |
|
1176 | rmtree = shutil.rmtree | |
936 |
|
1177 | |||
|
1178 | def rmtree_p(self): | |||
|
1179 | try: | |||
|
1180 | self.rmtree() | |||
|
1181 | except OSError: | |||
|
1182 | _, e, _ = sys.exc_info() | |||
|
1183 | if e.errno != errno.ENOENT: | |||
|
1184 | raise | |||
|
1185 | return self | |||
|
1186 | ||||
|
1187 | def chdir(self): | |||
|
1188 | os.chdir(self) | |||
|
1189 | ||||
|
1190 | cd = chdir | |||
937 |
|
1191 | |||
|
1192 | # | |||
938 | # --- Special stuff from os |
|
1193 | # --- Special stuff from os | |
939 |
|
1194 | |||
940 | if hasattr(os, 'chroot'): |
|
1195 | if hasattr(os, 'chroot'): | |
941 | def chroot(self): |
|
1196 | def chroot(self): | |
942 | os.chroot(self) |
|
1197 | os.chroot(self) | |
943 |
|
1198 | |||
944 | if hasattr(os, 'startfile'): |
|
1199 | if hasattr(os, 'startfile'): | |
945 | def startfile(self): |
|
1200 | def startfile(self): | |
946 | os.startfile(self) |
|
1201 | os.startfile(self) | |
|
1202 | return self | |||
947 |
|
1203 | |||
|
1204 | ||||
|
1205 | class tempdir(path): | |||
|
1206 | """ | |||
|
1207 | A temporary directory via tempfile.mkdtemp, and constructed with the | |||
|
1208 | same parameters that you can use as a context manager. | |||
|
1209 | ||||
|
1210 | Example: | |||
|
1211 | ||||
|
1212 | with tempdir() as d: | |||
|
1213 | # do stuff with the path object "d" | |||
|
1214 | ||||
|
1215 | # here the directory is deleted automatically | |||
|
1216 | """ | |||
|
1217 | ||||
|
1218 | @ClassProperty | |||
|
1219 | @classmethod | |||
|
1220 | def _next_class(cls): | |||
|
1221 | return path | |||
|
1222 | ||||
|
1223 | def __new__(cls, *args, **kwargs): | |||
|
1224 | dirname = tempfile.mkdtemp(*args, **kwargs) | |||
|
1225 | return super(tempdir, cls).__new__(cls, dirname) | |||
|
1226 | ||||
|
1227 | def __init__(self, *args, **kwargs): | |||
|
1228 | pass | |||
|
1229 | ||||
|
1230 | def __enter__(self): | |||
|
1231 | return self | |||
|
1232 | ||||
|
1233 | def __exit__(self, exc_type, exc_value, traceback): | |||
|
1234 | if not exc_value: | |||
|
1235 | self.rmtree() | |||
|
1236 | ||||
|
1237 | ||||
|
1238 | def _permission_mask(mode): | |||
|
1239 | """ | |||
|
1240 | Convert a Unix chmod symbolic mode like 'ugo+rwx' to a function | |||
|
1241 | suitable for applying to a mask to affect that change. | |||
|
1242 | ||||
|
1243 | >>> mask = _permission_mask('ugo+rwx') | |||
|
1244 | >>> oct(mask(o554)) | |||
|
1245 | 'o777' | |||
|
1246 | ||||
|
1247 | >>> oct(_permission_mask('gw-x')(o777)) | |||
|
1248 | 'o766' | |||
|
1249 | """ | |||
|
1250 | parsed = re.match('(?P<who>[ugo]+)(?P<op>[-+])(?P<what>[rwx]+)$', mode) | |||
|
1251 | if not parsed: | |||
|
1252 | raise ValueError("Unrecognized symbolic mode", mode) | |||
|
1253 | spec_map = dict(r=4, w=2, x=1) | |||
|
1254 | spec = reduce(operator.or_, [spec_map[perm] | |||
|
1255 | for perm in parsed.group('what')]) | |||
|
1256 | # now apply spec to each in who | |||
|
1257 | shift_map = dict(u=6, g=3, o=0) | |||
|
1258 | mask = reduce(operator.or_, [spec << shift_map[subj] | |||
|
1259 | for subj in parsed.group('who')]) | |||
|
1260 | ||||
|
1261 | op = parsed.group('op') | |||
|
1262 | # if op is -, invert the mask | |||
|
1263 | if op == '-': | |||
|
1264 | mask ^= o777 | |||
|
1265 | ||||
|
1266 | op_map = {'+': operator.or_, '-': operator.and_} | |||
|
1267 | return functools.partial(op_map[op], mask) |
General Comments 0
You need to be logged in to leave comments.
Login now