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
|
""" settings.settings - handles settings reading, updating and writing """
import os
from pathlib import Path
from .static import DEFAULT_SETTINGS
TEST_CONFIG_FILE = os.getenv("TEST_CONFIG_FILE")
class Settings:
""" Handles loading and saving settings from config file"""
def __init__(self):
self.settings=[]
if TEST_CONFIG_FILE:
self.config_filename = Path(TEST_CONFIG_FILE)
else:
if os.name == 'nt':
self.config_filename = (Path.home().resolve()
.joinpath('sliceitoff.cfg'))
else:
self.config_filename = (Path.home().resolve()
.joinpath('.config').joinpath('sliceitoffrc'))
if not self.config_filename.is_file():
self.settings=DEFAULT_SETTINGS[:]
return
with open(self.config_filename, "r", encoding="utf-8") as config_file:
for line in config_file:
entry = self.validate_line(line)
if not entry:
continue
self.settings.append(entry)
def validate_line(self, line):
""" Validates and splits config line """
if not line or line[0] == '#':
return None
data = line.split('=')
if len(data) != 2:
return None
return data[0], data[1]
def get_values(self, option):
""" Gets all values for certain option """
return [x[1] for x in self.settings if x[0]==option]
def put_values(self, option, values):
""" Puts multiple values with same option name """
for x in values:
self.put_value(option, x)
def remove_values(self, option):
""" Removes all values with given option name """
for x in self.settings[:]:
if x[0]==option:
self.settings.remove(x)
def replace_values(self, option, values):
""" After replacement only given values are attached to the option """
self.remove_values(option)
self.put_values(option, values)
def get_value(self, option):
""" Gets first value of the option """
v = self.get_values(option)
if not v:
return None
return v[0]
def get_value_or_default(self, option):
""" Gets first value if found otherwise default """
v = self.get_value(option)
if v:
return v
return [x[1] for x in DEFAULT_SETTINGS if x[0]==option][1]
def put_value(self, option, value):
""" Puts single value with option name """
self.settings.append((option, value))
def replace_value(self, option, value):
""" Replaces even multiple values from option just to add one """
self.remove_values(option)
self.put_value(option, value)
def save(self):
""" Saves options to config file """
with open(self.config_filename, 'w', encoding="utf-8") as config_file:
for option, value in self.settings:
config_file.write(f"{option}={value}\n")
# Initialize only one time
try:
# pylint: disable = used-before-assignment
# This is intented behaviour
settings
except NameError:
settings = Settings()
|