-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_system.py
135 lines (103 loc) · 3.02 KB
/
file_system.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import glob
import platform
from config import *
def createFolders():
for x in [SVG_DIR, SYMBOL_DIR, TREE_DIR]:
if not (os.path.exists(x) and os.path.isdir(x)):
os.mkdir(x)
def getAllFilesInDir(filetype, directory):
files = []
for r, d, f in os.walk(directory):
for file in f:
if filetype in file:
files.append(os.path.join(r, file))
return files
def getFileName(path, no_ext=False):
sep = file_seperator()
a = path.split(sep)[-1]
if no_ext:
a = ".".join(str(x) for x in a.split(".")[:-1])
return a
def removeBaseFolder(path):
sep = file_seperator()
s = path.split(sep)
if platform.system() == 'Windows':
split_at = 1
else:
split_at = 2
s = sep.join(str(x) for x in s[split_at:])
if len(s) > 0 and s[0] == " ":
s = s[1:]
return s
def saveWrite(toWrite, path):
d=False
if "/" in path or "\\" in path:
d=True
if not os.path.exists(os.path.dirname(path)) and d:
os.makedirs(os.path.dirname(path))
if toWrite is not None:
with open(path, 'w') as of:
of.write(toWrite)
def ensurePopulated(path):
if (len(os.listdir(path)) == 0):
print(path + " folder that should contain files is empty.")
def readColorSchemeFile_Colors(file=COLOR_SCHEME_FILE):
x = [
{
'color_replacements': s
}
for s in [
l[1:].strip().split(',')
for l in open(file).readlines()
if len(l.strip()) > 0 and l.strip()[0] == '>'
]
]
if len(x) == 0:
return TEMPLATE_COLORS
else:
print(TEMPLATE_COLORS)
print("VS")
print(x[0]['color_replacements'])
return x[0]['color_replacements']
def readColorSchemeFile_Themes(file=COLOR_SCHEME_FILE):
return [
{
'name': s[0],
'variant': s[1],
'colors': s[2:]
}
for s in [
l.strip().split(',')
for l in open(file).readlines()
if len(l.strip()) > 0 and l.strip()[0] not in ['#', '>']
]
]
def getCategory(folder, color_scheme_entry):
design_name = color_scheme_entry['name']
variant = color_scheme_entry['variant'].upper()
print(folder+"|"+design_name+"|"+variant)
return f'{folder}{design_name} {variant}'
def file_seperator():
if platform.system() == 'Windows':
return "\\"
else:
return "/"
def splitPath(path, no_cat=False):
x = getFileName(path)
sep = file_seperator()
if no_cat:
subdir = path.split(sep)[:-1]
subdir = sep.join(str(f) for f in subdir)
s = removeBaseFolder(subdir)
return x, None, s
else:
if platform.system() == 'Windows':
split_at = 1
else:
split_at = 2
category = path.split(sep)[split_at]
subdir = path.split(sep)[1:-1]
subdir = sep.join(str(f) for f in subdir)
s = removeBaseFolder(subdir)
return x, category, s