From a0687bc311aeedbb35842d33e70b658dde08ebff Mon Sep 17 00:00:00 2001 From: weckyy702 Date: Mon, 28 Dec 2020 16:47:52 +0100 Subject: [PATCH 1/2] found fatal bug while creating image @oleting --- .vscode/launch.json | 2 +- gui/utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index e80f071..1ded7e8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "name": "Python: Aktuelle Datei", "type": "python", "request": "launch", - "program": "debug.py", + "program": "run.py", "console": "integratedTerminal" } ] diff --git a/gui/utils.py b/gui/utils.py index 401e23b..48c7bc6 100644 --- a/gui/utils.py +++ b/gui/utils.py @@ -14,7 +14,7 @@ def nassi(input_path: str, output_path: str, outputname: str, gui, font_filepath NSD.set_font(font_filepath) NSD.load_from_file(input_path) - NSD.convert_to_image(output_path, outputname, 500) + NSD.convert_to_image(output_path, outputname, x_size=750) def output(values): From 99e7cbe7cc5be08b8519ff20a2e3d89da833ab0c Mon Sep 17 00:00:00 2001 From: weckyy702 Date: Mon, 28 Dec 2020 18:13:24 +0100 Subject: [PATCH 2/2] added customizable conflict behaviour in NSD saving --- .vscode/launch.json | 2 +- gui/utils.py | 8 +++--- interpreter/NassiShneidermann.py | 42 +++++++++++++++++++++++++------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 1ded7e8..e80f071 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "name": "Python: Aktuelle Datei", "type": "python", "request": "launch", - "program": "run.py", + "program": "debug.py", "console": "integratedTerminal" } ] diff --git a/gui/utils.py b/gui/utils.py index 53c1290..420e7da 100644 --- a/gui/utils.py +++ b/gui/utils.py @@ -1,20 +1,18 @@ -import logging import os from typing import Optional -from interpreter.NassiShneidermann import NassiShneidermanDiagram +from interpreter.NassiShneidermann import NassiShneidermanDiagram, Overwrite_behaviour, OB from draw.Iinstruction import * - -def nassi(input_path: str, output_path: str, outputname: str, gui, font_filepath: Optional[str]=None): +def nassi(input_path: str, output_path: str, outputname: str, gui, behaviour: Overwrite_behaviour, font_filepath: Optional[str]=None): NSD = NassiShneidermanDiagram(gui.debug_mode) if font_filepath != None: NSD.set_font(font_filepath) NSD.load_from_file(input_path) - NSD.convert_to_image(output_path, outputname, x_size=750) + NSD.convert_to_image(output_path, outputname, on_conflict=behaviour, x_size=750) def output(values): diff --git a/interpreter/NassiShneidermann.py b/interpreter/NassiShneidermann.py index 2a99b0d..7b39516 100644 --- a/interpreter/NassiShneidermann.py +++ b/interpreter/NassiShneidermann.py @@ -1,11 +1,20 @@ from typing import List import logging +from enum import IntEnum +import os.path +import secrets from draw.Iinstruction import Iinstruction from interpreter import interpret_source as itp from draw.code_to_image_wrapper import NSD_writer import draw.code_to_image as cti +class Overwrite_behaviour(IntEnum): + SKIP = 0 + OVERWWRITE = 1 + RANDOM_NAME = 2 + +OB = Overwrite_behaviour class NassiShneidermanDiagram: @@ -38,18 +47,33 @@ class NassiShneidermanDiagram: for instruction in scope_instructions: x, y = instruction.to_image(x, y, 1000) - def convert_to_image(self, output_path: str, filename: str, x_size: int=200): + def check_conflicts(self, filepath:str, behavoiur: Overwrite_behaviour): + if os.path.exists(filepath+".png"): + if behavoiur == OB.SKIP: + return None + elif behavoiur == OB.OVERWWRITE: + return filepath + else: + while os.path.exists(filepath+".png"): + filepath = filepath + str(secrets.token_hex(1)) + return filepath + return filepath + + def convert_to_image(self, output_path: str, filename: str, on_conflict: Overwrite_behaviour=OB.SKIP, x_size: int=200): for i in range(len(self.scopes)): filepath = f"{output_path}/{filename}#{i}" - logging.info(f"Saving NSD to {filepath}.png...") + filepath = self.check_conflicts(filepath, on_conflict) + if filepath is not None: + logging.info(f"Saving NSD to {filepath}...") + print(f"Saving... to {filepath}") - image_y_sz = self._get_image_height(i) - with NSD_writer(filepath, x_size, image_y_sz): - scope = self.scopes[i] - x, y = 0, 0 - for instruction in scope: - x, y = instruction.to_image(x, y, x_size) - logging.info("Done!") + image_y_sz = self._get_image_height(i) + with NSD_writer(filepath, x_size, image_y_sz): + scope = self.scopes[i] + x, y = 0, 0 + for instruction in scope: + x, y = instruction.to_image(x, y, x_size) + logging.info("Done!") def load_from_file(self, filepath:str): self.scopes = itp.load_instructions(filepath) \ No newline at end of file