-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
60 lines (50 loc) · 1.67 KB
/
util.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
from pathlib import Path
import json
def stripleadingrelativefrom(inputf:Path):
result = Path("")
founddotdot = 0
if ("..") in inputf.parts[0]:
#get rid of leading ../../.. if relative path
for index, part in enumerate(inputf.parts):
#this will only work if they are contiguous .. found at the start
if (founddotdot == index) and (part == ".."):
founddotdot += 1
else:
result = result.joinpath(part)
else:
result = inputf
return result
#given c:/whatever/this/thing/is/not/foo.py
#and provided "thing" in argument frompos
#return "/is/not/foo.py" if include = False
#return "thing/is/not/foo.py" if include = True
def getpathfrom(inputf:Path, frompos:str, include=False)->Path:
result = Path("")
if (frompos != None):
try:
idx = inputf.parts.index(frompos)
if include == False:
idx += 1
for part in inputf.parts[idx:]:
result = result.joinpath(part)
except ValueError:
result = stripleadingrelativefrom(inputf)
return result
#taken from
#https://stackoverflow.com/questions/54491156/validate-json-data-using-python
def validate_json(filename):
with open(filename) as file:
try:
json.load(file) # put JSON-data to a variable
except json.decoder.JSONDecodeError:
return False
else:
return True
#cleanups for json
def cleanforjson(text:str):
#get rid of stray backslash
if "\\" in text:
text = text.replace("\\", "\\\\")
if r'"' in text:
text = text.replace(r'"', r"'")
return text