-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
73 lines (53 loc) · 1.8 KB
/
config.py
File metadata and controls
73 lines (53 loc) · 1.8 KB
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
from configparser import ConfigParser
from os import listdir, path
from os.path import join, isfile, join
CONFIG_NAME = "gans.config"
def config_write(outdir, indirs=None, **kwargs):
out = ConfigParser()
out["DEFAULT"] = {}
for key in kwargs:
if key == "PATHS":
raise Exception("Cannot use PATHS as variable name")
out["DEFAULT"][key] = str(kwargs[key])
if indirs is not None:
paths = {}
for i, value in enumerate(indirs):
paths[str(i)] = value
out["PATHS"] = paths
with open(join(outdir, CONFIG_NAME), "w") as config:
out.write(config)
def config_read(indir, indirs=[], **kwargs):
values = {}
read = ConfigParser()
read.read(join(indir, CONFIG_NAME))
cf = read["DEFAULT"]
for key in kwargs:
if cf.get(key) is None:
values[key] = kwargs[key]
for key, value in cf.items():
values[key] = value
if "PATHS" in read:
paths = []
for key in read["PATHS"]:
if key not in read["DEFAULT"]:
paths.append(read["PATHS"][key])
values["PATHS"] = paths
else:
values["PATHS"] = indirs
return values
def get_number(exp, folder, isdir=False):
if exp.index("*") != exp.rindex("*"):
raise Exception("String can only have one asterisk")
files = []
if isdir:
files = [x for x in listdir(folder) if path.isdir(join(folder, x))]
else:
files = [x for x in listdir(folder) if path.isfile(join(folder, x))]
FIRST = exp[:exp.index("*")]
SECOND = exp[exp.rindex("*") + 1:]
max_n = 0
for file in files:
if file.startswith(FIRST) and file.endswith(SECOND):
num = int(file[len(FIRST): len(file) - len(SECOND)])
max_n = max(num, max_n)
return max_n