-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhd_active_config.py
53 lines (43 loc) · 1.69 KB
/
hd_active_config.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
import configparser
from typing import List, Union
class HdActiveConfig:
"""
Config options for HD Active in a INI file format.
"""
SECTION_NAME = 'HD Active'
OPTION_RUN = 'run_on_start'
OPTION_WAIT = 'wait_between_access'
OPTION_DRIVE_PATHS = 'drives'
def __init__(self, file_name: str):
self.file_name = file_name
# Default values
self.run: bool = False
self.wait: Union[int, float] = 60
self.drive_paths: List[str] = []
self.config = configparser.ConfigParser(
converters={'list': lambda x: [i.strip(' "\'') for i in x.split(',')]}
)
self.read()
def __str__(self):
return f'drive paths: {", ".join(self.drive_paths)}' f'\nwait: {self.wait}s'
def read(self):
files_read = self.config.read(self.file_name)
if not files_read:
raise FileNotFoundError(self.file_name)
try:
section = self.config[self.SECTION_NAME]
self.run = section.getboolean(self.OPTION_RUN, self.run)
self.wait = section.getfloat(self.OPTION_WAIT, self.wait)
self.drive_paths = section.getlist(self.OPTION_DRIVE_PATHS, self.drive_paths)
except KeyError:
# Section didn't exist, vars will continue with previous values.
pass
def write(self):
section = self.config[self.SECTION_NAME]
section[self.OPTION_RUN] = str(self.run)
section[self.OPTION_WAIT] = str(self.wait)
section[self.OPTION_DRIVE_PATHS] = ', '.join(
str(drive_path) for drive_path in self.drive_paths
)
with open(self.file_name) as configfile:
self.config.write(configfile)