Skip to content

Commit

Permalink
#86 * Test cases dependencies to tlp runtime refactored
Browse files Browse the repository at this point in the history
* Moved state image to TlpConfig items
* Code refactorings
  • Loading branch information
d4nj1 committed Feb 5, 2021
1 parent 4ffa072 commit ee1d732
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 105 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
20 changes: 7 additions & 13 deletions test/test_tlp_configs.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion tlpui/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.3.1-6"
__version__ = "1.3.1-7"
58 changes: 9 additions & 49 deletions tlpui/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import Enum
from . import settings
from .uihelper import StateImage


class ConfType(Enum):
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
def refresh_state_image(self):
self.stateimage.refresh(self.value, self.valuestore, self.enabled, self.enabledstore)
3 changes: 1 addition & 2 deletions tlpui/configui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
59 changes: 27 additions & 32 deletions tlpui/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down
55 changes: 55 additions & 0 deletions tlpui/filehelper.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions tlpui/mainui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
1 change: 0 additions & 1 deletion tlpui/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@
tlpconfig = dict()
tlpconfig_original = dict()
tlpconfig_defaults = dict()
imagestate = dict()
2 changes: 1 addition & 1 deletion tlpui/ui_config_objects/gtkselection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tlpui/uihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit ee1d732

Please sign in to comment.