-
Notifications
You must be signed in to change notification settings - Fork 3
/
pathlib2.py
72 lines (55 loc) · 2.03 KB
/
pathlib2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import pathlib
import shutil
class SharedPathMethods:
@property
def path(self):
# Path2().path is new in 3.4.5 and 3.5.2
return str(self)
def makedirs(self, *args, **kwargs):
os.makedirs(self.extended_path, *args, **kwargs)
def link(self, other):
os.link(self.extended_path, other.extended_path)
def utime(self, *args, **kwargs):
os.utime(self.extended_path, *args, **kwargs)
def copyfile(self, other, *args, **kwargs):
shutil.copyfile(self.extended_path, other.extended_path, *args, **kwargs)
def expanduser(self):
return Path2(os.path.expanduser(self.extended_path))
class WindowsPath2(SharedPathMethods, pathlib.WindowsPath):
@property
def extended_path(self):
"""
Add prefix \\?\ to every absolute path, so that it's a "extended-length"
path, that should be longer than 259 characters (called: "MAX_PATH")
see:
https://msdn.microsoft.com/en-us/library/aa365247.aspx#maxpath
"""
if self.is_absolute():
return "\\\\?\\%s" % self
return self.path
class PosixPath2(SharedPathMethods, pathlib.PosixPath):
@property
def extended_path(self):
return self.path
class Path2(pathlib.Path):
"""
https://github.com/python/cpython/blob/master/Lib/pathlib.py
"""
def __new__(cls, *args, **kwargs):
if cls is Path2 or cls is pathlib.Path:
cls = WindowsPath2 if os.name == 'nt' else PosixPath2
self = cls._from_parts(args, init=False)
if not self._flavour.is_supported:
raise NotImplementedError("cannot instantiate %r on your system"
% (cls.__name__,))
self._init()
return self
@classmethod
def home(cls):
"""
Return a new path pointing to the user's home directory (as
returned by os.path.expanduser('~'))
"""
# Note: pathlib.Path.home() exist since in Python 3.5
return cls(os.path.expanduser("~"))