forked from sublimescala/sublime-ensime
-
Notifications
You must be signed in to change notification settings - Fork 2
/
paths.py
57 lines (50 loc) · 1.41 KB
/
paths.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
import os
def encode_path(path):
if not path:
return path
if os.name == "nt":
if os.path.isabs(path):
drive, rest = os.path.splitdrive(path)
return "/" + drive[:-1].upper() + rest.replace("\\", "/")
else:
return path.replace("\\", "/")
else:
return path
def decode_path(path):
if not path:
return path
if os.name == "nt":
if path.startswith("/"):
path = path[1:]
iof = path.find("/")
if iof == -1:
drive = path
rest = ""
else:
drive = path[:iof]
rest = path[iof:]
return (drive + ":" + rest).replace("/", "\\")
else:
return path.replace("/", "\\")
else:
return path
def same_paths(path1, path2):
if not path1 or not path2:
return False
path1_normalized = os.path.normcase(os.path.realpath(path1))
path2_normalized = os.path.normcase(os.path.realpath(path2))
return path1_normalized == path2_normalized
def is_subpath(root, wannabe):
if not root or not wannabe:
return False
root = os.path.normcase(os.path.realpath(root))
wannabe = os.path.normcase(os.path.realpath(wannabe))
return wannabe.startswith(root)
def relative_path(root, wannabe):
if not root or not wannabe:
return None
if not is_subpath(root, wannabe):
return None
root = os.path.normcase(os.path.realpath(root))
wannabe = os.path.normcase(os.path.realpath(wannabe))
return wannabe[len(root) + 1:]