Starting python tests

This commit is contained in:
Adsooi 2024-09-18 00:30:58 +02:00
parent b50d56d511
commit 9072e94d14
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
8 changed files with 66 additions and 28 deletions
LogarithmPlotter/util

View file

@ -21,7 +21,6 @@ from platform import system
from json import load, dumps
from PySide6.QtCore import QLocale, QTranslator
DEFAULT_SETTINGS = {
"check_for_updates": True,
"reset_redo_stack": True,
@ -38,7 +37,7 @@ DEFAULT_SETTINGS = {
"default_graph": {
"xzoom": 100,
"yzoom": 10,
"xmin": 5/10,
"xmin": 5 / 10,
"ymax": 25,
"xaxisstep": "4",
"yaxisstep": "4",
@ -55,41 +54,47 @@ DEFAULT_SETTINGS = {
# Create config directory
CONFIG_PATH = {
"Linux": path.join(environ["XDG_CONFIG_HOME"], "LogarithmPlotter")
if "XDG_CONFIG_HOME" in environ else
path.join(path.expanduser("~"), ".config", "LogarithmPlotter"),
if "XDG_CONFIG_HOME" in environ else
path.join(path.expanduser("~"), ".config", "LogarithmPlotter"),
"Windows": path.join(path.expandvars('%APPDATA%'), "LogarithmPlotter", "config"),
"Darwin": path.join(path.expanduser("~"), "Library", "Application Support", "LogarithmPlotter"),
}[system()]
CONFIG_FILE = path.join(CONFIG_PATH, "config.json")
initialized = False
current_config = DEFAULT_SETTINGS
class UnknownNamespaceError(Exception): pass
def init():
"""
Initializes the config and loads all possible settings from the file if needs be.
"""
global current_config
current_config = DEFAULT_SETTINGS
makedirs(CONFIG_PATH, exist_ok=True)
if path.exists(CONFIG_FILE):
cfg_data = load(open(CONFIG_FILE, 'r', -1, 'utf8'))
cfg_data = load(open(CONFIG_FILE, 'r', -1, 'utf8'))
for setting_name in cfg_data:
if type(cfg_data[setting_name]) == dict:
for setting_name2 in cfg_data[setting_name]:
setSetting(setting_name+"."+setting_name2, cfg_data[setting_name][setting_name2])
setSetting(setting_name + "." + setting_name2, cfg_data[setting_name][setting_name2])
else:
setSetting(setting_name, cfg_data[setting_name])
def save():
def save(file=CONFIG_FILE):
"""
Saves the config to the path.
"""
write_file = open(CONFIG_FILE, 'w', -1, 'utf8')
write_file = open(file, 'w', -1, 'utf8')
write_file.write(dumps(current_config))
write_file.close()
def getSetting(namespace):
"""
Returns a setting from a namespace.
@ -103,9 +108,10 @@ def getSetting(namespace):
setting = setting[name]
else:
# return namespace # Return original name
raise ValueError('Setting ' + namespace + ' doesn\'t exist. Debug: ', setting, name)
raise UnknownNamespaceError(f"Setting {namespace} doesn't exist. Debug: {setting}, {name}")
return setting
def setSetting(namespace, data):
"""
Sets a setting at a namespace with data.
@ -119,6 +125,6 @@ def setSetting(namespace, data):
if name in setting:
setting = setting[name]
else:
raise ValueError('Setting {} doesn\'t exist. Debug: {}, {}'.format(namespace, setting, name))
raise UnknownNamespaceError(f"Setting {namespace} doesn't exist. Debug: {setting}, {name}")
else:
setting[name] = data

View file

@ -32,6 +32,9 @@ from LogarithmPlotter import __VERSION__
from LogarithmPlotter.util import config
class InvalidFileException(Exception): pass
class ChangelogFetcher(QRunnable):
def __init__(self, helper):
QRunnable.__init__(self)
@ -94,8 +97,8 @@ class Helper(QObject):
pass
elif data[:3] == "LPF":
# More recent version of LogarithmPlotter file, but incompatible with the current format
msg = QCoreApplication.translate("This file was created by a more recent version of LogarithmPlotter and cannot be backloaded in LogarithmPlotter v{}.\nPlease update LogarithmPlotter to open this file.".format(__VERSION__))
raise Exception(msg)
msg = QCoreApplication.translate("This file was created by a more recent version of LogarithmPlotter and cannot be backloaded in LogarithmPlotter v{}.\nPlease update LogarithmPlotter to open this file.")
raise InvalidFileException(msg.format(__VERSION__))
else:
raise Exception("Invalid LogarithmPlotter file.")
except Exception as e: # If file can't be loaded
@ -131,7 +134,6 @@ class Helper(QObject):
@Slot(str, result=float)
def getSettingInt(self, namespace):
print('Getting', namespace, config.getSetting(namespace))
return config.getSetting(namespace)
@Slot(str, result=bool)

View file

@ -18,6 +18,7 @@
from PySide6.QtQml import QJSValue
class InvalidAttributeValueException(Exception): pass
class PyJSValue:
"""
@ -38,15 +39,32 @@ class PyJSValue:
elif isinstance(value, PyJSValue):
# Set property
self.qjs_value.setProperty(key, value.qjs_value)
else:
print('Setting', key, value)
elif isinstance(value, QJSValue):
self.qjs_value.setProperty(key, value)
elif type(value) in (int, float, str, bool):
self.qjs_value.setProperty(key, QJSValue(value))
else:
raise InvalidAttributeValueException(f"Invalid value {value} of type {type(value)} being set to {key}.")
def __eq__(self, other):
if isinstance(other, PyJSValue):
return self.qjs_value.equals(other.qjs_value)
elif isinstance(other, QJSValue):
return self.qjs_value.equals(other)
elif type(other) in (int, float, str, bool):
return self.qjs_value.equals(QJSValue(other))
else:
return False
def __call__(self, *args, **kwargs):
value = None
if self.qjs_value.isCallable():
if self._parent is None:
return self.qjs_value.call(args)
value = self.qjs_value.call(args)
else:
return self.qjs_value.callWithInstance(self._parent, args)
value = self.qjs_value.callWithInstance(self._parent, args)
else:
raise ValueError('Cannot call non-function JS value.')
raise InvalidAttributeValueException('Cannot call non-function JS value.')
if isinstance(value, QJSValue):
value = PyJSValue(value)
return value