Spaces:
Sleeping
Sleeping
Upload read_cfg.py
Browse files- read_cfg.py +50 -0
read_cfg.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Author: Aqeel Anwar(ICSRL)
|
2 |
+
# Created: 9/20/2019, 12:43 PM
|
3 |
+
# Email: [email protected]
|
4 |
+
|
5 |
+
from configparser import ConfigParser
|
6 |
+
from dotmap import DotMap
|
7 |
+
|
8 |
+
|
9 |
+
def ConvertIfStringIsInt(input_string):
|
10 |
+
try:
|
11 |
+
float(input_string)
|
12 |
+
|
13 |
+
try:
|
14 |
+
if int(input_string) == float(input_string):
|
15 |
+
return int(input_string)
|
16 |
+
else:
|
17 |
+
return float(input_string)
|
18 |
+
except ValueError:
|
19 |
+
return float(input_string)
|
20 |
+
|
21 |
+
except ValueError:
|
22 |
+
return input_string
|
23 |
+
|
24 |
+
|
25 |
+
def read_cfg(config_filename="masks.cfg", mask_type="surgical", verbose=False):
|
26 |
+
parser = ConfigParser()
|
27 |
+
parser.optionxform = str
|
28 |
+
parser.read(config_filename)
|
29 |
+
cfg = DotMap()
|
30 |
+
section_name = mask_type
|
31 |
+
|
32 |
+
if verbose:
|
33 |
+
hyphens = "-" * int((80 - len(config_filename)) / 2)
|
34 |
+
print(hyphens + " " + config_filename + " " + hyphens)
|
35 |
+
|
36 |
+
# for section_name in parser.sections():
|
37 |
+
|
38 |
+
if verbose:
|
39 |
+
print("[" + section_name + "]")
|
40 |
+
for name, value in parser.items(section_name):
|
41 |
+
value = ConvertIfStringIsInt(value)
|
42 |
+
if name != "template":
|
43 |
+
cfg[name] = tuple(int(s) for s in value.split(","))
|
44 |
+
else:
|
45 |
+
cfg[name] = value
|
46 |
+
spaces = " " * (30 - len(name))
|
47 |
+
if verbose:
|
48 |
+
print(name + ":" + spaces + str(cfg[name]))
|
49 |
+
|
50 |
+
return cfg
|