From ee1d732dc814d30b99418d322a8c34fe796d8728 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 5 Feb 2021 17:38:27 +0100 Subject: [PATCH] #86 * Test cases dependencies to tlp runtime refactored * Moved state image to TlpConfig items * Code refactorings --- README.md | 2 +- test/test_tlp_configs.py | 20 +++------ tlpui/__init__.py | 2 +- tlpui/config.py | 58 ++++-------------------- tlpui/configui.py | 3 +- tlpui/file.py | 59 +++++++++++-------------- tlpui/filehelper.py | 55 +++++++++++++++++++++++ tlpui/mainui.py | 3 +- tlpui/settings.py | 1 - tlpui/ui_config_objects/gtkselection.py | 2 +- tlpui/uihelper.py | 6 +-- 11 files changed, 106 insertions(+), 105 deletions(-) create mode 100644 tlpui/filehelper.py diff --git a/README.md b/README.md index db4716e..6bbde22 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -TLPUI - 2021.02.03 +TLPUI - 2021.02.05 The Python scripts in this project generate a GTK-UI to change TLP configuration files easily. It has the aim to protect users from setting bad configuration and to deliver a basic overview of all the valid configuration values. diff --git a/test/test_tlp_configs.py b/test/test_tlp_configs.py index 8762bb7..0171f83 100644 --- a/test/test_tlp_configs.py +++ b/test/test_tlp_configs.py @@ -1,5 +1,5 @@ import unittest -from tlpui.file import extract_default_tlp_file_config, get_json_schema_object_from_file +from tlpui.filehelper import extract_default_tlp_configs, get_json_schema_object_from_file def get_config_count(categories): @@ -19,8 +19,7 @@ class MyTestCase(unittest.TestCase): def test_tlp_version_0_8(self): version = "0_8" - tlpconfig_defaults = dict() - extract_default_tlp_file_config(tlpconfig_defaults, '../tlpui/defaults/tlp-{}.conf'.format(version)) + tlpconfig_defaults = extract_default_tlp_configs('../tlpui/defaults/tlp-{}.conf'.format(version)) jsoncategories = get_json_schema_object_from_file('categories', '../tlpui/configschema/{}.json'.format(version)) jsonconfigcount = get_config_count(jsoncategories) @@ -30,8 +29,7 @@ def test_tlp_version_0_8(self): def test_tlp_version_0_9(self): version = "0_9" - tlpconfig_defaults = dict() - extract_default_tlp_file_config(tlpconfig_defaults, '../tlpui/defaults/tlp-{}.conf'.format(version)) + tlpconfig_defaults = extract_default_tlp_configs('../tlpui/defaults/tlp-{}.conf'.format(version)) jsoncategories = get_json_schema_object_from_file('categories', '../tlpui/configschema/{}.json'.format(version)) jsonconfigcount = get_config_count(jsoncategories) @@ -41,8 +39,7 @@ def test_tlp_version_0_9(self): def test_tlp_version_1_0(self): version = "1_0" - tlpconfig_defaults = dict() - extract_default_tlp_file_config(tlpconfig_defaults, '../tlpui/defaults/tlp-{}.conf'.format(version)) + tlpconfig_defaults = extract_default_tlp_configs('../tlpui/defaults/tlp-{}.conf'.format(version)) jsoncategories = get_json_schema_object_from_file('categories', '../tlpui/configschema/{}.json'.format(version)) jsonconfigcount = get_config_count(jsoncategories) @@ -52,8 +49,7 @@ def test_tlp_version_1_0(self): def test_tlp_version_1_1(self): version = "1_1" - tlpconfig_defaults = dict() - extract_default_tlp_file_config(tlpconfig_defaults, '../tlpui/defaults/tlp-{}.conf'.format(version)) + tlpconfig_defaults = extract_default_tlp_configs('../tlpui/defaults/tlp-{}.conf'.format(version)) jsoncategories = get_json_schema_object_from_file('categories', '../tlpui/configschema/{}.json'.format(version)) jsonconfigcount = get_config_count(jsoncategories) @@ -63,8 +59,7 @@ def test_tlp_version_1_1(self): def test_tlp_version_1_2(self): version = "1_2" - tlpconfig_defaults = dict() - extract_default_tlp_file_config(tlpconfig_defaults, '../tlpui/defaults/tlp-{}.conf'.format(version)) + tlpconfig_defaults = extract_default_tlp_configs('../tlpui/defaults/tlp-{}.conf'.format(version)) jsoncategories = get_json_schema_object_from_file('categories', '../tlpui/configschema/{}.json'.format(version)) jsonconfigcount = get_config_count(jsoncategories) @@ -74,8 +69,7 @@ def test_tlp_version_1_2(self): def test_tlp_version_1_3(self): version = "1_3" - tlpconfig_defaults = dict() - extract_default_tlp_file_config(tlpconfig_defaults, '../tlpui/defaults/tlp-{}.conf'.format(version)) + tlpconfig_defaults = extract_default_tlp_configs('../tlpui/defaults/tlp-{}.conf'.format(version)) jsoncategories = get_json_schema_object_from_file('categories', '../tlpui/configschema/{}.json'.format(version)) jsonconfigcount = get_config_count(jsoncategories) diff --git a/tlpui/__init__.py b/tlpui/__init__.py index e39dfee..0cbd373 100644 --- a/tlpui/__init__.py +++ b/tlpui/__init__.py @@ -1 +1 @@ -__version__ = "1.3.1-6" +__version__ = "1.3.1-7" diff --git a/tlpui/config.py b/tlpui/config.py index a05f6c2..f16e292 100644 --- a/tlpui/config.py +++ b/tlpui/config.py @@ -1,5 +1,5 @@ from enum import Enum -from . import settings +from .uihelper import StateImage class ConfType(Enum): @@ -9,22 +9,6 @@ class ConfType(Enum): ERR = 4 -class TlpDefaults: - def __init__(self, name: str, value: str, enabled: bool): - self.name = name - self.value = value - self.enabled = enabled - - def get_name(self) -> str: - return self.name - - def get_value(self) -> str: - return self.value - - def is_enabled(self) -> bool: - return self.enabled - - class TlpConfig: def __init__(self, enabled: bool, name: str, value: str, conftype: ConfType, confpath=""): self.enabled = enabled @@ -34,6 +18,7 @@ def __init__(self, enabled: bool, name: str, value: str, conftype: ConfType, con self.valuestore = value self.conftype = conftype self.confpath = confpath + self.stateimage = None # type: StateImage def get_name(self) -> str: return self.name @@ -49,43 +34,18 @@ def get_value(self) -> str: def set_value(self, newvalue: str): self.value = newvalue - self.refresh_image_state() + self.refresh_state_image() def set_enabled(self, newstate: bool): self.enabled = newstate - self.refresh_image_state() + self.refresh_state_image() def is_enabled(self) -> bool: return self.enabled - def refresh_image_state(self): - settings.imagestate[self.name].refresh_image_state(self.value, self.valuestore, self.enabled, self.enabledstore) - - -def get_changed_properties() -> dict: - changedproperties = dict() - - changed = settings.tlpconfig - original = settings.tlpconfig_original - - for configid in changed: - config = changed[configid] # type: TlpConfig - config_original = original[configid] # type: TlpConfig - - statechange = config.is_enabled() != config_original.is_enabled() - configchange = config.get_value() != config_original.get_value() - - if statechange or configchange: - configname = config.get_name() - - if not config.is_enabled() and settings.tlpconfig_defaults[configname].is_enabled(): - enabled = "" - value = "* empty" - else: - enabled = "" if config.is_enabled() else "#" - value = config.get_value() - - value = '\"' + value + '\"' - changedproperties[configname] = "{}{}={}".format(enabled, configname, value) + def add_state_image(self, newstateimage: StateImage): + self.stateimage = newstateimage + self.refresh_state_image() - return changedproperties \ No newline at end of file + def refresh_state_image(self): + self.stateimage.refresh(self.value, self.valuestore, self.enabled, self.enabledstore) diff --git a/tlpui/configui.py b/tlpui/configui.py index 085f9fa..7853a38 100644 --- a/tlpui/configui.py +++ b/tlpui/configui.py @@ -89,8 +89,7 @@ def get_state_image(configname: str): image = Gtk.Image() defaultvalue = settings.tlpconfig_defaults[configname].get_value() defaultstate = settings.tlpconfig_defaults[configname].is_enabled() - settings.imagestate[configname] = StateImage(defaultvalue, defaultstate, image) - settings.tlpconfig[configname].refresh_image_state() + settings.tlpconfig[configname].add_state_image(StateImage(defaultvalue, defaultstate, image)) return image diff --git a/tlpui/file.py b/tlpui/file.py index 96fdafb..8d9236e 100644 --- a/tlpui/file.py +++ b/tlpui/file.py @@ -4,11 +4,11 @@ from sys import stdout from subprocess import check_output, STDOUT, CalledProcessError from io import open -from json import load from os import access, W_OK, close, path from tempfile import mkstemp -from .config import TlpDefaults, TlpConfig, ConfType +from .config import TlpConfig, ConfType from . import settings +from .filehelper import get_json_schema_object_from_file, extract_default_tlp_configs, TlpDefaults from .uihelper import get_graphical_sudo, SUDO_MISSING_TEXT @@ -18,24 +18,16 @@ def get_json_schema_object(objectname) -> dict: return get_json_schema_object_from_file(objectname, tlpprovidedschema) else: majorminor = settings.tlpbaseversion - return get_json_schema_object_from_file(objectname, settings.workdir + '/configschema/' + majorminor + '.json') - - -def get_json_schema_object_from_file(objectname: str, filename: str) -> dict: - jsonfile = open(filename) - jsonobject = load(jsonfile) - jsonfile.close() - return jsonobject[objectname] + return get_json_schema_object_from_file(objectname, f"{settings.workdir}/configschema/{majorminor}.json") def get_tlp_config_defaults(tlpversion: str): - tlpconfig_defaults = dict() - extract_default_tlp_file_config(tlpconfig_defaults, f"{settings.workdir}/defaults/tlp-{tlpversion}.conf") + tlpconfig_defaults = extract_default_tlp_configs(f"{settings.workdir}/defaults/tlp-{tlpversion}.conf") if tlpversion not in ["0_8", "0_9", "1_0", "1_1", "1_2"]: # update default values with intrinsic ones intrinsic_defaults_path = f"{settings.folder_prefix}/usr/share/tlp/defaults.conf" - extract_default_tlp_file_config(tlpconfig_defaults, intrinsic_defaults_path) + tlpconfig_defaults.update(extract_default_tlp_configs(intrinsic_defaults_path)) return tlpconfig_defaults @@ -112,30 +104,33 @@ def extract_tlp_settings(lines: list) -> None: settings.tlpconfig[propertyname] = TlpConfig(True, propertyname, propertyvalue, conftype, configfile) -def extract_default_tlp_file_config(tlpconfig_defaults: dict, filename: str) -> None: - propertypattern = re.compile(r'^#?[A-Z_\d]+=') - fileopener = open(filename) - lines = fileopener.readlines() - fileopener.close() +def get_changed_properties() -> dict: + changedproperties = dict() - for line in lines: - if propertypattern.match(line): - cleanline = line.lstrip().rstrip() + changed = settings.tlpconfig + original = settings.tlpconfig_original - if (cleanline.startswith('#')): - enabled = False - cleanline = cleanline.lstrip('#') - else: - enabled = True + for configid in changed: + config = changed[configid] # type: TlpConfig + config_original = original[configid] # type: TlpConfig - property = cleanline.split('=', maxsplit=1) - propertyname = property[0] - propertyvalue = property[1] + statechange = config.is_enabled() != config_original.is_enabled() + configchange = config.get_value() != config_original.get_value() - if propertyvalue.startswith('\"') and propertyvalue.endswith('\"'): - propertyvalue = propertyvalue.lstrip('\"').rstrip('\"') + if statechange or configchange: + configname = config.get_name() + + if not config.is_enabled() and settings.tlpconfig_defaults[configname].is_enabled(): + enabled = "" + value = "* empty" + else: + enabled = "" if config.is_enabled() else "#" + value = config.get_value() + + value = '\"' + value + '\"' + changedproperties[configname] = "{}{}={}".format(enabled, configname, value) - tlpconfig_defaults[propertyname] = TlpDefaults(propertyname, propertyvalue, enabled) + return changedproperties def create_tmp_tlp_config_file(changedproperties: dict) -> str: diff --git a/tlpui/filehelper.py b/tlpui/filehelper.py new file mode 100644 index 0000000..a249891 --- /dev/null +++ b/tlpui/filehelper.py @@ -0,0 +1,55 @@ + +import re +from io import open +from json import load + + +def get_json_schema_object_from_file(objectname: str, filename: str) -> dict: + jsonfile = open(filename) + jsonobject = load(jsonfile) + jsonfile.close() + return jsonobject[objectname] + + +class TlpDefaults: + def __init__(self, name: str, value: str, enabled: bool): + self.name = name + self.value = value + self.enabled = enabled + + def get_name(self) -> str: + return self.name + + def get_value(self) -> str: + return self.value + + def is_enabled(self) -> bool: + return self.enabled + + +def extract_default_tlp_configs(filename: str) -> dict: + propertypattern = re.compile(r'^#?[A-Z_\d]+=') + fileopener = open(filename) + lines = fileopener.readlines() + fileopener.close() + + tlpconfig_defaults = dict() + for line in lines: + if propertypattern.match(line): + cleanline = line.lstrip().rstrip() + + if (cleanline.startswith('#')): + enabled = False + cleanline = cleanline.lstrip('#') + else: + enabled = True + + property = cleanline.split('=', maxsplit=1) + propertyname = property[0] + propertyvalue = property[1] + + if propertyvalue.startswith('\"') and propertyvalue.endswith('\"'): + propertyvalue = propertyvalue.lstrip('\"').rstrip('\"') + + tlpconfig_defaults[propertyname] = TlpDefaults(propertyname, propertyvalue, enabled) + return tlpconfig_defaults diff --git a/tlpui/mainui.py b/tlpui/mainui.py index c093dda..7eac77b 100644 --- a/tlpui/mainui.py +++ b/tlpui/mainui.py @@ -7,9 +7,8 @@ from gi.repository import Gtk, Gdk, GdkPixbuf from . import settings from . import language -from .config import get_changed_properties from .configui import create_config_box -from .file import init_tlp_file_config, create_tmp_tlp_config_file, write_tlp_config +from .file import init_tlp_file_config, create_tmp_tlp_config_file, write_tlp_config, get_changed_properties from .statui import create_stat_box from .uihelper import get_flag_image, get_theme_image from . import __version__ diff --git a/tlpui/settings.py b/tlpui/settings.py index 4af470d..2542734 100644 --- a/tlpui/settings.py +++ b/tlpui/settings.py @@ -24,4 +24,3 @@ tlpconfig = dict() tlpconfig_original = dict() tlpconfig_defaults = dict() -imagestate = dict() diff --git a/tlpui/ui_config_objects/gtkselection.py b/tlpui/ui_config_objects/gtkselection.py index 5fce9a3..2832c96 100644 --- a/tlpui/ui_config_objects/gtkselection.py +++ b/tlpui/ui_config_objects/gtkselection.py @@ -8,7 +8,7 @@ def create_selection_box(configname: str, values: str) -> Gtk.ComboBox: configvalue = settings.tlpconfig[configname].get_value() if configvalue not in selectitems: - settings.imagestate[configname].warn_unknown_config_value(configvalue) + settings.tlpconfig[configname].stateimage.warn_unknown_config_value(configvalue) countid = 0 selectid = 0 diff --git a/tlpui/uihelper.py b/tlpui/uihelper.py index 4e457c2..1c15977 100644 --- a/tlpui/uihelper.py +++ b/tlpui/uihelper.py @@ -32,7 +32,7 @@ def get_graphical_sudo() -> str: def get_flag_image(locale: str) -> Gtk.Image: """Fetch flag image from icons folder""" - flagpixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(f"{settings.icondir }flags/{locale}.png", width=16, height=16) + flagpixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(f"{settings.icondir}flags/{locale}.png", width=16, height=16) return Gtk.Image().new_from_pixbuf(flagpixbuf) @@ -41,7 +41,7 @@ def get_theme_image(iconname: str, iconsize: Gtk.IconSize) -> Gtk.Image: if Gtk.IconTheme.get_default().has_icon(iconname): return Gtk.Image().new_from_icon_name(iconname, iconsize) else: - return Gtk.Image().new_from_file(settings.icondir + 'themeable/hicolor/scalable/actions/' + iconname + '.svg') + return Gtk.Image().new_from_file(f"{settings.icondir}themeable/hicolor/scalable/actions/{iconname}.svg") class StateImage: @@ -56,7 +56,7 @@ def warn_unknown_config_value(self, configvalue: str) -> None: self.stateimage.set_from_icon_name(Gtk.STOCK_DIALOG_WARNING, Gtk.IconSize.BUTTON) self.stateimage.set_tooltip_text('{}: {}'.format(UNKNOWN_CONFIG_VALUE_TEXT, configvalue)) - def refresh_image_state(self, value: str, store: str, enabled: bool, enabledstore: bool) -> None: + def refresh(self, value: str, store: str, enabled: bool, enabledstore: bool) -> None: """Refresh image and description by changed state""" changed = False if enabled != enabledstore or value != store: