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
|
""" settings.settings - handles settings reading, updating and writing """
import os
from pathlib import Path
from .static import DEFAULT_SETTINGS
class Settings:
def __init__(self, filename = None):
self.settings=[]
if filename:
self.config_filename = filename
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):
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):
return [x[1] for x in self.settings if x[0]==option]
def put_values(self, option, values):
for x in values:
self.put_value(option, x)
def remove_values(self, option):
for x in self.settings[:]:
if x[0]==option:
self.settings.remove(x)
def replace_values(self, option, values):
self.remove_values(option)
self.put_values(option, values)
def get_value(self, option):
v = self.get_values(option)
if not v:
return None
return v
def get_value_or_default(self, option):
v = self.get_values(option)
if v:
return v
return [x[1] for x in DEFAULT_SETTINGS if x[0]==option][1]
def put_value(self, option, value):
self.settings.append((option, value))
def replace_value(self, option, value):
self.remove_values(option)
self.put_value(option, value)
def save(self):
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()
|