Bug fixes and slight file moving
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
- Gradients are no longer hidden when filtered out - Fixing #1 - Opening files don't work on compiled versions of LogarithmPlotter on MacOS - Moving python modules to "util" directory for more clarity - Moving flatpak metainfo to eu.ad5001.LogarithmPlotter repository.
This commit is contained in:
parent
20c910f884
commit
44e39e5265
7 changed files with 13 additions and 401 deletions
94
LogarithmPlotter/util/config.py
Normal file
94
LogarithmPlotter/util/config.py
Normal file
|
@ -0,0 +1,94 @@
|
|||
"""
|
||||
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and repartition functions.
|
||||
* Copyright (C) 2022 Ad5001
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
from os import path, environ, makedirs
|
||||
from platform import system
|
||||
from json import load, dumps
|
||||
from PySide2.QtCore import QLocale, QTranslator
|
||||
|
||||
|
||||
DEFAULT_SETTINGS = {
|
||||
"check_for_updates": True,
|
||||
"reset_redo_stack": True,
|
||||
"last_install_greet": "0",
|
||||
}
|
||||
|
||||
# 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"),
|
||||
"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
|
||||
|
||||
|
||||
def init():
|
||||
"""
|
||||
Initializes the config and loads all possible settings from the file if needs be.
|
||||
"""
|
||||
makedirs(CONFIG_PATH, exist_ok=True)
|
||||
|
||||
if path.exists(CONFIG_FILE):
|
||||
cfg_data = load(open(CONFIG_FILE, 'r', -1, 'utf8'))
|
||||
for setting_name in cfg_data:
|
||||
setSetting(setting_name, cfg_data[setting_name])
|
||||
|
||||
def save():
|
||||
"""
|
||||
Saves the config to the path.
|
||||
"""
|
||||
write_file = open(CONFIG_FILE, 'w', -1, 'utf8')
|
||||
write_file.write(dumps(current_config))
|
||||
write_file.close()
|
||||
|
||||
def getSetting(namespace):
|
||||
"""
|
||||
Returns a setting from a namespace.
|
||||
E.g: if the config is {"test": {"foo": 1}}, you can access the "foo" setting
|
||||
by using the "test.foo" namespace.
|
||||
"""
|
||||
names = namespace.split(".")
|
||||
setting = current_config
|
||||
for name in names:
|
||||
if name in setting:
|
||||
setting = setting[name]
|
||||
else:
|
||||
# return namespace # Return original name
|
||||
raise ValueError('Setting ' + namespace + ' doesn\'t exist. Debug: ', setting, name)
|
||||
return setting
|
||||
|
||||
def setSetting(namespace, data):
|
||||
"""
|
||||
Sets a setting at a namespace with data.
|
||||
E.g: if the config is {"test": {"foo": 1}}, you can access the "foo" setting
|
||||
by using the "test.foo" namespace.
|
||||
"""
|
||||
names = namespace.split(".")
|
||||
setting = current_config
|
||||
for name in names:
|
||||
if name != names[-1]:
|
||||
if name in setting:
|
||||
setting = setting[name]
|
||||
else:
|
||||
raise ValueError('Setting {} doesn\'t exist. Debug: {}, {}'.format(namespace, setting, name))
|
||||
else:
|
||||
setting[name] = data
|
161
LogarithmPlotter/util/helper.py
Normal file
161
LogarithmPlotter/util/helper.py
Normal file
|
@ -0,0 +1,161 @@
|
|||
"""
|
||||
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and repartition functions.
|
||||
* Copyright (C) 2022 Ad5001
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
from PySide2.QtWidgets import QMessageBox, QApplication
|
||||
from PySide2.QtCore import QRunnable, QThreadPool, QThread, QObject, Signal, Slot, QCoreApplication
|
||||
from PySide2.QtQml import QQmlApplicationEngine
|
||||
from PySide2.QtGui import QImage
|
||||
from PySide2 import __version__ as PySide2_version
|
||||
|
||||
from os import chdir, path
|
||||
from json import loads
|
||||
from sys import version as sys_version
|
||||
from urllib.request import urlopen
|
||||
from urllib.error import HTTPError, URLError
|
||||
|
||||
from LogarithmPlotter import __VERSION__
|
||||
from LogarithmPlotter.util import config
|
||||
|
||||
class ChangelogFetcher(QRunnable):
|
||||
def __init__(self, helper):
|
||||
QRunnable.__init__(self)
|
||||
self.helper = helper
|
||||
|
||||
def run(self):
|
||||
msg_text = "Unknown changelog error."
|
||||
try:
|
||||
# Fetching version
|
||||
r = urlopen("https://api.ad5001.eu/changelog/logarithmplotter/")
|
||||
lines = r.readlines()
|
||||
r.close()
|
||||
msg_text = "".join(map(lambda x: x.decode('utf-8'), lines)).strip()
|
||||
except HTTPError as e:
|
||||
msg_text = QCoreApplication.translate("changelog","Could not fetch changelog: Server error {}.").format(str(e.code))
|
||||
except URLError as e:
|
||||
msg_text = QCoreApplication.translate("changelog","Could not fetch update: {}.").format(str(e.reason))
|
||||
self.helper.gotChangelog.emit(msg_text)
|
||||
|
||||
class Helper(QObject):
|
||||
changelogFetched = Signal(str)
|
||||
gotChangelog = Signal(str)
|
||||
|
||||
def __init__(self, cwd: str, tmpfile: str):
|
||||
QObject.__init__(self)
|
||||
self.cwd = cwd
|
||||
self.tmpfile = tmpfile
|
||||
self.gotChangelog.connect(self.fetched)
|
||||
|
||||
def fetched(self, changelog: str):
|
||||
self.changelogFetched.emit(changelog)
|
||||
|
||||
@Slot(str, str)
|
||||
def write(self, filename, filedata):
|
||||
chdir(self.cwd)
|
||||
if path.exists(path.dirname(path.realpath(filename))):
|
||||
if filename.split(".")[-1] == "lpf":
|
||||
# Add header to file
|
||||
filedata = "LPFv1" + filedata
|
||||
f = open(path.realpath(filename), 'w', -1, 'utf8')
|
||||
f.write(filedata)
|
||||
f.close()
|
||||
chdir(path.dirname(path.realpath(__file__)))
|
||||
|
||||
@Slot(str, result=str)
|
||||
def load(self, filename):
|
||||
chdir(self.cwd)
|
||||
data = '{}'
|
||||
if path.exists(path.realpath(filename)):
|
||||
f = open(path.realpath(filename), 'r', -1, 'utf8')
|
||||
data = f.read()
|
||||
f.close()
|
||||
try:
|
||||
if data[:5] == "LPFv1":
|
||||
# V1 version of the file
|
||||
data = data[5:]
|
||||
elif data[0] == "{" and "type" in loads(data) and loads(data)["type"] == "logplotv1":
|
||||
pass
|
||||
elif data[:3] == "LPF":
|
||||
# More recent version of LogarithmPlotter file, but incompatible with the current format
|
||||
raise Exception(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__)))
|
||||
else:
|
||||
raise Exception("Invalid LogarithmPlotter file.")
|
||||
except Exception as e: # If file can't be loaded
|
||||
QMessageBox.warning(None, 'LogarithmPlotter', QCoreApplication.translate('main','Could not open file "{}":\n{}').format(filename, e), QMessageBox.Ok) # Cannot parse file
|
||||
else:
|
||||
QMessageBox.warning(None, 'LogarithmPlotter', QCoreApplication.translate('main','Could not open file: "{}"\nFile does not exist.').format(filename), QMessageBox.Ok) # Cannot parse file
|
||||
try:
|
||||
chdir(path.dirname(path.realpath(__file__)))
|
||||
except NotADirectoryError as e:
|
||||
# Triggered on bundled versions of MacOS when it shouldn't. Prevents opening files.
|
||||
# See more at https://git.ad5001.eu/Ad5001/LogarithmPlotter/issues/1
|
||||
pass
|
||||
return data
|
||||
|
||||
@Slot(result=str)
|
||||
def gettmpfile(self):
|
||||
return self.tmpfile
|
||||
|
||||
@Slot()
|
||||
def copyImageToClipboard(self):
|
||||
clipboard = QApplication.clipboard()
|
||||
clipboard.setImage(QImage(self.tmpfile))
|
||||
|
||||
@Slot(result=str)
|
||||
def getVersion(self):
|
||||
return __VERSION__
|
||||
|
||||
@Slot(str, result=str)
|
||||
def getSetting(self, namespace):
|
||||
return config.getSetting(namespace)
|
||||
|
||||
@Slot(str, result=bool)
|
||||
def getSettingBool(self, namespace):
|
||||
return config.getSetting(namespace)
|
||||
|
||||
@Slot(str, str)
|
||||
def setSetting(self, namespace, value):
|
||||
return config.setSetting(namespace, value)
|
||||
|
||||
@Slot(str, bool)
|
||||
def setSettingBool(self, namespace, value):
|
||||
return config.setSetting(namespace, value)
|
||||
|
||||
@Slot(str)
|
||||
def setLanguage(self, new_lang):
|
||||
config.setSetting("language", new_lang)
|
||||
|
||||
@Slot(result=str)
|
||||
def getDebugInfos(self):
|
||||
"""
|
||||
Returns the version info about Qt, PySide2 & Python
|
||||
"""
|
||||
return QCoreApplication.translate('main',"Built with PySide2 (Qt) v{} and python v{}").format(PySide2_version, sys_version.split("\n")[0])
|
||||
|
||||
@Slot()
|
||||
def fetchChangelog(self):
|
||||
changelog_cache_path = path.join(path.dirname(path.realpath(__file__)), "CHANGELOG.md")
|
||||
if path.exists(changelog_cache_path):
|
||||
# We have a cached version of the changelog, for env that don't have access to the internet.
|
||||
f = open(changelog_cache_path);
|
||||
self.changelogFetched.emit("".join(f.readlines()).strip())
|
||||
f.close()
|
||||
else:
|
||||
# Fetch it from the internet.
|
||||
runnable = ChangelogFetcher(self)
|
||||
QThreadPool.globalInstance().start(runnable)
|
||||
|
50
LogarithmPlotter/util/native.py
Normal file
50
LogarithmPlotter/util/native.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
"""
|
||||
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and repartition functions.
|
||||
* Copyright (C) 2022 Ad5001
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
# This file contains stuff for native interactions with each OS.
|
||||
|
||||
from PySide2.QtCore import QObject, QEvent
|
||||
|
||||
# On macOS, opening a file through finder can only be fetched through the
|
||||
# QFileOpenEvent and NOT throught command line parameters.
|
||||
class MacOSFileOpenHandler(QObject):
|
||||
def __init__(self):
|
||||
self.initilized = False
|
||||
self.mainwindow = None
|
||||
self.opened_file = ""
|
||||
QObject.__init__(self)
|
||||
|
||||
def init_graphics(self, mainwindow):
|
||||
self.mainwindow = mainwindow
|
||||
self.initilized = True
|
||||
if self.opened_file != "":
|
||||
self.open_file()
|
||||
|
||||
def open_file(self):
|
||||
self.mainwindow.loadDiagram(self.opened_file)
|
||||
|
||||
def eventFilter(self, obj, event):
|
||||
if event.type() == QEvent.FileOpen:
|
||||
print("Got file", event.file(), self.initilized)
|
||||
self.opened_file = event.file()
|
||||
if self.initilized:
|
||||
self.open_file()
|
||||
return True
|
||||
else:
|
||||
# standard event processing
|
||||
return QObject.eventFilter(self, obj, event)
|
83
LogarithmPlotter/util/update.py
Normal file
83
LogarithmPlotter/util/update.py
Normal file
|
@ -0,0 +1,83 @@
|
|||
"""
|
||||
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and repartition functions.
|
||||
* Copyright (C) 2022 Ad5001
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
from PySide2.QtCore import QRunnable, QThreadPool, QThread, QObject, Signal, QCoreApplication
|
||||
from urllib.request import urlopen
|
||||
from urllib.error import HTTPError, URLError
|
||||
from sys import argv
|
||||
|
||||
class UpdateInformation(QObject):
|
||||
got_update_info = Signal(bool, str, bool)
|
||||
|
||||
class UpdateCheckerRunnable(QRunnable):
|
||||
def __init__(self, current_version, callback):
|
||||
QRunnable.__init__(self)
|
||||
self.current_version = current_version
|
||||
self.callback = callback
|
||||
|
||||
def run(self):
|
||||
msg_text = "Unknown update error."
|
||||
show_alert = True
|
||||
update_available = False
|
||||
try:
|
||||
# Fetching version
|
||||
r = urlopen("https://api.ad5001.eu/update/v1/LogarithmPlotter")
|
||||
lines = r.readlines()
|
||||
r.close()
|
||||
# Parsing version
|
||||
version = "".join(map(chr, lines[0])).strip() # Converts byte to string.
|
||||
version_tuple = version.split(".")
|
||||
is_version_newer = False
|
||||
if "dev" in self.current_version:
|
||||
# We're on a dev version
|
||||
current_version_tuple = self.current_version.split(".")[:-1] # Removing the dev0+git bit.
|
||||
is_version_newer = version_tuple >= current_version_tuple # If equals, that means we got out of testing phase.
|
||||
else:
|
||||
current_version_tuple = self.current_version.split(".")
|
||||
is_version_newer = version_tuple > current_version_tuple
|
||||
if is_version_newer:
|
||||
msg_text = QCoreApplication.translate("update","An update for LogarithPlotter (v{}) is available.").format(version)
|
||||
update_available = True
|
||||
else:
|
||||
show_alert = False
|
||||
msg_text = QCoreApplication.translate("update","No update available.")
|
||||
|
||||
except HTTPError as e:
|
||||
msg_text = QCoreApplication.translate("update","Could not fetch update information: Server error {}.").format(str(e.code))
|
||||
except URLError as e:
|
||||
msg_text = QCoreApplication.translate("update","Could not fetch update information: {}.").format(str(e.reason))
|
||||
self.callback.got_update_info.emit(show_alert, msg_text,update_available)
|
||||
|
||||
def check_for_updates(current_version, window):
|
||||
"""
|
||||
Checks for updates in the background, and sends an alert with information.
|
||||
"""
|
||||
if "--no-check-for-updates" in argv:
|
||||
return #
|
||||
def cb(show_alert, msg_text, update_available):
|
||||
pass
|
||||
if show_alert:
|
||||
window.showAlert(msg_text)
|
||||
if update_available:
|
||||
window.showUpdateMenu()
|
||||
|
||||
update_info = UpdateInformation()
|
||||
update_info.got_update_info.connect(cb)
|
||||
|
||||
runnable = UpdateCheckerRunnable(current_version, update_info)
|
||||
QThreadPool.globalInstance().start(runnable)
|
Loading…
Add table
Add a link
Reference in a new issue