forked from qdev-dk-archive/Majorana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigreader.py
85 lines (65 loc) · 2.44 KB
/
configreader.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
# Module containing the config file reader class
from configparser import ConfigParser
class Config:
"""
Object to be used for interacting with the config file.
The ConfigFile is constantly synced with the config file on disk
(provided that only this object was used to change the file).
Args:
filename (str): The path to the configuration file on disk
isdefault (Optional[bool]): Whether this is the default Config object.
Default: True.
Attributes:
default (Union[None, Config]): A reference to the default Config
object, if it exists. Else None.
"""
default = None
def __init__(self, filename, isdefault=True):
# If this is the default config object, we make it available as a
# class method.
if isdefault:
Config.default = self
self._filename = filename
self._cfg = ConfigParser()
self._load()
def _load(self):
self._cfg.read(self._filename)
def reload(self):
"""
Reload the file from disk.
"""
self._load()
def get(self, section, field=None):
"""
Gets the value of the specified section/field.
If no field is specified, the entire section is returned
as a dict.
Example: Config.get('QDac Channel Labels', '2')
Args:
section (str): Name of the section
field (Optional[str]): The field to return. If omitted, the entire
section is returned as a dict
"""
# Try to be really clever about the input
if not isinstance(field, str) and (field is not None):
field = '{}'.format(field)
if field is None:
output = dict(zip(self._cfg[section].keys(),
self._cfg[section].values()))
else:
output = self._cfg[section][field]
return output
def set(self, section, field, value):
"""
Set a value in the config file.
Immediately writes to disk.
Args:
section (str): The name of the section to write to
field (str): The name of the field to write
value (Union[str, float, int]): The value to write
"""
if not isinstance(value, str):
value = '{}'.format(value)
self._cfg[section][field] = value
with open(self._filename, 'w') as configfile:
self._cfg.write(configfile)