-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpaths.py
More file actions
28 lines (24 loc) · 700 Bytes
/
paths.py
File metadata and controls
28 lines (24 loc) · 700 Bytes
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
import os
from typing import *
from contextlib import contextmanager
_projectDir: Optional[str] = None
@contextmanager
def projectDir(d: str):
"""Context manager to temporarily set the _relDir global variable."""
global _projectDir
old = _projectDir
_projectDir = _normPath(d)
sep = os.path.sep
if _projectDir and _projectDir[-1] != sep:
_projectDir = _projectDir + sep
try:
yield
finally:
_projectDir = old
def _normPath(s: str) -> str:
return os.path.normpath(os.path.abspath(s))
def canonicalizePath(s: str) -> str:
s = _normPath(s)
if _projectDir and s.startswith(_projectDir):
s = s[len(_projectDir):]
return s