Commit 1f0981e2 authored by Roman Alifanov's avatar Roman Alifanov

some improvements

parent c0ee9f94
......@@ -67,11 +67,11 @@ class FileBackend(Backend):
ext = ext.lower()
if ext == '.json':
return 'json'
elif ext == '.yaml' or ext == '.yml':
elif ext in ['.yaml', '.yml']:
return 'yaml'
elif ext == '.ini':
return 'ini'
elif ext == '.sh' or ext == '.conf':
elif ext in ['.sh', '.conf']:
return 'text'
else:
return 'text'
......@@ -97,12 +97,15 @@ class FileBackend(Backend):
def _write_file(self, data):
try:
with open(self.file_path, 'w', encoding=self.encoding) as file:
with open(self.file_path, 'r+', encoding=self.encoding) as file:
if self.file_type == 'json':
file.seek(0)
json.dump(data, file, indent=4)
elif self.file_type == 'yaml':
file.seek(0)
yaml.dump(data, file, default_flow_style=False)
elif self.file_type == 'ini':
file.seek(0)
config = ConfigParser()
for section, values in data.items():
config[section] = values
......@@ -126,39 +129,47 @@ class FileBackend(Backend):
return config
def _write_text_config(self, file, data):
existing_lines = []
with open(self.file_path, 'r', encoding=self.encoding) as file_read:
existing_lines = file_read.readlines()
existing_style = self._detect_text_style(existing_lines)
existing_lines = file.readlines()
style = self._detect_text_style(existing_lines)
file.seek(0)
file.truncate()
for line in existing_lines:
if not line.strip() or line.startswith('#'):
file.write(line)
elif '=' in line:
key, _ = line.split('=', 1)
if key.strip() in data:
if style == 'space_around':
file.write(f"{key.strip()} = {data[key.strip()]}\n")
elif style == 'no_space':
file.write(f"{key.strip()}={data[key.strip()]}\n")
else:
file.write(f"{key.strip()} = {data[key.strip()]}\n")
else:
file.write(line)
for key, value in data.items():
if existing_style == 'space_around':
file.write(f"{key} = {value}\n")
elif existing_style == 'no_space':
file.write(f"{key}={value}\n")
else:
if not any(key.strip() == line.split('=', 1)[0].strip() for line in existing_lines):
file.write(f"{key} = {value}\n")
def _detect_text_style(self, lines):
style = None
for line in lines:
line = line.strip()
if '=' in line:
if line.startswith(' ') and line.endswith(' '):
style = 'space_around'
break
return 'space_around'
elif line.find('=') == len(line.split('=')[0]):
style = 'no_space'
break
return style or 'space_around'
return 'no_space'
return 'space_around'
def get_value(self, key, gtype):
data = self._read_file()
if data is None:
return None
if self.file_type == 'json' or self.file_type == 'yaml':
if self.file_type in ['json', 'yaml']:
return data.get(key, None)
elif self.file_type == 'ini':
section, key_name = key.split('.', 1)
......@@ -173,7 +184,7 @@ class FileBackend(Backend):
if data is None:
return None
if self.file_type == 'json' or self.file_type == 'yaml':
if self.file_type in ['json', 'yaml']:
if isinstance(data.get(key), list):
return (min(data[key]), max(data[key]))
elif self.file_type == 'ini':
......@@ -185,7 +196,7 @@ class FileBackend(Backend):
if data is None:
return
if self.file_type == 'json' or self.file_type == 'yaml':
if self.file_type in ['json', 'yaml']:
data[key] = value
elif self.file_type == 'ini':
section, key_name = key.split('.', 1)
......
......@@ -4,6 +4,7 @@ from .backends import backend_factory
from .daemon_client import dclient
from .tools.yml_tools import load_modules, merge_categories_by_name
from .tools.gvariant import convert_by_gvariant
from .widgets import WidgetFactory
......@@ -18,7 +19,9 @@ class Setting:
self.key = setting_data.get('key')
self.default = setting_data.get('default')
self.gtype = setting_data.get('gtype', [])
self.map = setting_data.get('map', self._default_map())
self.map = setting_data.get('map')
if self.map is None:
self.map = self._default_map()
self.data = setting_data.get('data', {})
if len(self.gtype) > 2:
......@@ -68,10 +71,15 @@ class Setting:
return list(self.map.values()).index(current_value) if current_value in self.map.values() else 0
def _get_backend_value(self):
value = None
backend = self._get_backend()
if backend:
return backend.get_value(self.key, self.gtype)
return self.default
value = backend.get_value(self.key, self.gtype)
if value is None:
value = self.default
return value
def _get_backend_range(self):
backend = self._get_backend()
......@@ -81,7 +89,7 @@ class Setting:
def _set_backend_value(self, value):
backend = self._get_backend()
if backend:
backend.set_value(self.key, value, self.gtype)
backend.set_value(self.key, convert_by_gvariant(value, self.gtype), self.gtype)
def _get_backend(self):
if self.root is True:
......
def convert_by_gvariant(value, gtype):
"""
Приводит значение к нужному типу в зависимости от GVariant gtype.
:param value: Исходное значение
:param gtype: Тип GVariant ('b', 'y', 'n', 'q', 'i', 'u', 'x', 't', 'd', 's')
:return: Значение, приведенное к указанному типу
"""
try:
if gtype == 'b': # Boolean
return bool(value)
elif gtype == 'y': # Byte
return max(0, min(255, int(value))) # Ограничение диапазона
elif gtype == 'n': # Int16
return max(-32768, min(32767, int(value))) # Ограничение диапазона
elif gtype == 'q': # Uint16
return max(0, min(65535, int(value))) # Ограничение диапазона
elif gtype == 'i': # Int32
return max(-2147483648, min(2147483647, int(value)))
elif gtype == 'u': # Uint32
return max(0, min(4294967295, int(value)))
elif gtype == 'x': # Int64
return max(-9223372036854775808, min(9223372036854775807, int(value)))
elif gtype == 't': # Uint64
return max(0, min(18446744073709551615, int(value)))
elif gtype == 'd': # Double
return float(value)
elif gtype == 's': # String
return str(value)
else:
raise ValueError(f"Неизвестный GVariant тип: {gtype}")
except (ValueError, TypeError) as e:
print(f"Ошибка приведения типа: {e}")
return None
......@@ -19,7 +19,7 @@
from gi.repository import GObject, Adw, Gtk
from .settings import init_settings_stack
from .settings.main import init_settings_stack
@Gtk.Template(resource_path='/ru.ximperlinux.TuneIt/window.ui')
class TuneitWindow(Adw.ApplicationWindow):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment