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 cee5b3c..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, 500) + 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