From de4f08682dec80c43549144a2679b38c9ccb71c1 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Tue, 20 Jul 2021 15:05:06 +0200 Subject: [PATCH] Fixing bugs with opening relative files, adding checks for file, and header for files by default. --- .drone/test1.lpf | 2 +- LogarithmPlotter/__init__.py | 30 ++++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/.drone/test1.lpf b/.drone/test1.lpf index 8b761d1..7785158 100644 --- a/.drone/test1.lpf +++ b/.drone/test1.lpf @@ -1 +1 @@ -{"xzoom":100,"yzoom":485,"xmin":-1,"ymax":1,"xaxisstep":"1","yaxisstep":"0.1","xaxislabel":"ω (rad/s)","yaxislabel":"G (dB)","logscalex":false,"linewidth":2,"showxgrad":true,"showygrad":true,"textsize":22,"history":[[["EditedProperty",["h","Function","labelX",3,2.5,false]],["EditedProperty",["h","Function","labelPosition","above-left","above",false]],["EditedProperty",["h","Function","labelPosition","above","left",false]],["EditedProperty",["h","Function","labelX",2.5,2,false]],["EditedProperty",["h","Function","labelX",2,2.2,false]],["EditedProperty",["h","Function","labelX",2.2,9,false]]],[]],"width":1908,"height":1011,"objects":{"Sequence":[["u",true,"#ff5500","name + value",true,true,{"1":"cos(u[n])"},{"0":"-1"},"above-left",8]],"Function":[["f",true,"#2b156d","name + value","integral((x + 1), x, \"cos (t)\", \"t\")","ℝ⁺*","ℝ","application","left",7,true,true],["g",true,"#ac539c","name + value","0.739083","ℝ","ℝ","application","below",-1,true,false],["h",true,"#00aa00","name + value","derivative(\"cos t\", \"t\", x)","ℤ","ℝ","application","left",9,true,true]],"Repartition":[["X",true,"#1E1EAF","name",true,true,{"0":"0.2","1":"0.1","2":"0.2"},"above",2.5]],"Point":[["A",true,"#060759","name + value","4","(-0.5)","top","●"]]},"type":"logplotv1"} \ No newline at end of file +LPFv1{"xzoom":100,"yzoom":485,"xmin":-1,"ymax":1,"xaxisstep":"1","yaxisstep":"0.1","xaxislabel":"ω (rad/s)","yaxislabel":"G (dB)","logscalex":false,"linewidth":2,"showxgrad":true,"showygrad":true,"textsize":22,"history":[[["EditedProperty",["h","Function","labelX",3,2.5,false]],["EditedProperty",["h","Function","labelPosition","above-left","above",false]],["EditedProperty",["h","Function","labelPosition","above","left",false]],["EditedProperty",["h","Function","labelX",2.5,2,false]],["EditedProperty",["h","Function","labelX",2,2.2,false]],["EditedProperty",["h","Function","labelX",2.2,9,false]]],[]],"width":1908,"height":1005,"objects":{"Sequence":[["u",true,"#ff5500","name + value",true,true,{"1":"cos(u[n])"},{"0":"-1"},"above-left",8]],"Function":[["f",true,"#2b156d","name + value","integral((x + 1), x, \"cos (t)\", \"t\")","ℝ⁺*","ℝ","application","left",7,true,true],["g",true,"#ac539c","name + value","0.739083","ℝ","ℝ","application","below",-1,true,false],["h",true,"#00aa00","name + value","derivative(\"cos t\", \"t\", x)","ℤ","ℝ","application","left",9,true,true]],"Repartition":[["X",true,"#1E1EAF","name",true,true,{"0":"0.2","1":"0.1","2":"0.2"},"above",2.5]],"Point":[["A",true,"#060759","name + value","4","(-0.5)","top","●"]]},"type":"logplotv1"} \ No newline at end of file diff --git a/LogarithmPlotter/__init__.py b/LogarithmPlotter/__init__.py index 86e9579..0853e1f 100644 --- a/LogarithmPlotter/__init__.py +++ b/LogarithmPlotter/__init__.py @@ -25,13 +25,14 @@ from PySide2 import __version__ as PySide2_version import os import tempfile from platform import release as os_release -from json import dumps +from json import dumps, loads from sys import platform, argv, version as sys_version import webbrowser __VERSION__ = "0.0.1.dev0" tempfile = tempfile.mkstemp(suffix='.png')[1] +pwd = os.getcwd() def get_linux_theme(): des = { @@ -50,19 +51,39 @@ class Helper(QObject): @Slot(str, str) def write(self, filename, filedata): + os.chdir(pwd) if os.path.exists(os.path.dirname(os.path.realpath(filename))): + if filename.split(".")[-1] == "lpf": + # Add header to file + filedata = "LPFv1" + filedata f = open(os.path.realpath(filename), 'w', -1, 'utf8') f.write(filedata) f.close() + os.chdir(os.path.dirname(os.path.realpath(__file__))) @Slot(str, result=str) def load(self, filename): + os.chdir(pwd) + data = '{}' if os.path.exists(os.path.realpath(filename)): f = open(os.path.realpath(filename), 'r', -1, 'utf8') data = f.read() f.close() - return data - return '{}' + 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 + else: + raise Exception("Invalid LogarithmPlotter file.") + except Exception as e: # If file can't be loaded + from PySide2.QtWidgets import QMessageBox + QMessageBox.warning(None, 'LogarithmPlotter', 'Could not open file "{}":\n{}'.format(filename, e), QMessageBox.Ok) # Cannot parse file + else: + QMessageBox.warning(None, 'LogarithmPlotter', 'Could not open file: "{}"\nFile does not exist.'.format(filename), QMessageBox.Ok) # Cannot parse file + os.chdir(os.path.dirname(os.path.realpath(__file__))) + return data @Slot(result=str) def gettmpfile(self): @@ -91,7 +112,6 @@ class Helper(QObject): webbrowser.open(url) def run(): - pwd = os.getcwd() os.chdir(os.path.dirname(os.path.realpath(__file__))) os.environ["QT_QUICK_CONTROLS_STYLE"] = { @@ -114,10 +134,8 @@ def run(): engine.addImportPath(os.path.realpath(os.path.join(os.getcwd(), "qml"))) engine.load(os.path.realpath(os.path.join(os.getcwd(), "qml", "eu", "ad5001", "LogarithmPlotter", "LogarithmPlotter.qml"))) - os.chdir(pwd) if len(argv) > 0 and os.path.exists(argv[-1]) and argv[-1].split('.')[-1] in ['json', 'lgg', 'lpf']: engine.rootObjects()[0].loadDiagram(argv[-1]) - os.chdir(os.path.dirname(os.path.realpath(__file__))) if not engine.rootObjects(): print("No root object")