added customizable conflict behaviour in NSD saving

This commit is contained in:
weckyy702
2020-12-28 18:13:24 +01:00
parent b1215e8003
commit 99e7cbe7cc
3 changed files with 37 additions and 15 deletions

2
.vscode/launch.json vendored
View File

@@ -8,7 +8,7 @@
"name": "Python: Aktuelle Datei",
"type": "python",
"request": "launch",
"program": "run.py",
"program": "debug.py",
"console": "integratedTerminal"
}
]

View File

@@ -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):

View File

@@ -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)