added customizable conflict behaviour in NSD saving
This commit is contained in:
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@@ -8,7 +8,7 @@
|
|||||||
"name": "Python: Aktuelle Datei",
|
"name": "Python: Aktuelle Datei",
|
||||||
"type": "python",
|
"type": "python",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "run.py",
|
"program": "debug.py",
|
||||||
"console": "integratedTerminal"
|
"console": "integratedTerminal"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,20 +1,18 @@
|
|||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
from interpreter.NassiShneidermann import NassiShneidermanDiagram
|
from interpreter.NassiShneidermann import NassiShneidermanDiagram, Overwrite_behaviour, OB
|
||||||
from draw.Iinstruction import *
|
from draw.Iinstruction import *
|
||||||
|
|
||||||
|
def nassi(input_path: str, output_path: str, outputname: str, gui, behaviour: Overwrite_behaviour, font_filepath: Optional[str]=None):
|
||||||
def nassi(input_path: str, output_path: str, outputname: str, gui, font_filepath: Optional[str]=None):
|
|
||||||
NSD = NassiShneidermanDiagram(gui.debug_mode)
|
NSD = NassiShneidermanDiagram(gui.debug_mode)
|
||||||
|
|
||||||
if font_filepath != None:
|
if font_filepath != None:
|
||||||
NSD.set_font(font_filepath)
|
NSD.set_font(font_filepath)
|
||||||
|
|
||||||
NSD.load_from_file(input_path)
|
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):
|
def output(values):
|
||||||
|
|||||||
@@ -1,11 +1,20 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
import logging
|
import logging
|
||||||
|
from enum import IntEnum
|
||||||
|
import os.path
|
||||||
|
import secrets
|
||||||
|
|
||||||
from draw.Iinstruction import Iinstruction
|
from draw.Iinstruction import Iinstruction
|
||||||
from interpreter import interpret_source as itp
|
from interpreter import interpret_source as itp
|
||||||
from draw.code_to_image_wrapper import NSD_writer
|
from draw.code_to_image_wrapper import NSD_writer
|
||||||
import draw.code_to_image as cti
|
import draw.code_to_image as cti
|
||||||
|
|
||||||
|
class Overwrite_behaviour(IntEnum):
|
||||||
|
SKIP = 0
|
||||||
|
OVERWWRITE = 1
|
||||||
|
RANDOM_NAME = 2
|
||||||
|
|
||||||
|
OB = Overwrite_behaviour
|
||||||
|
|
||||||
class NassiShneidermanDiagram:
|
class NassiShneidermanDiagram:
|
||||||
|
|
||||||
@@ -38,18 +47,33 @@ class NassiShneidermanDiagram:
|
|||||||
for instruction in scope_instructions:
|
for instruction in scope_instructions:
|
||||||
x, y = instruction.to_image(x, y, 1000)
|
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)):
|
for i in range(len(self.scopes)):
|
||||||
filepath = f"{output_path}/{filename}#{i}"
|
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)
|
image_y_sz = self._get_image_height(i)
|
||||||
with NSD_writer(filepath, x_size, image_y_sz):
|
with NSD_writer(filepath, x_size, image_y_sz):
|
||||||
scope = self.scopes[i]
|
scope = self.scopes[i]
|
||||||
x, y = 0, 0
|
x, y = 0, 0
|
||||||
for instruction in scope:
|
for instruction in scope:
|
||||||
x, y = instruction.to_image(x, y, x_size)
|
x, y = instruction.to_image(x, y, x_size)
|
||||||
logging.info("Done!")
|
logging.info("Done!")
|
||||||
|
|
||||||
def load_from_file(self, filepath:str):
|
def load_from_file(self, filepath:str):
|
||||||
self.scopes = itp.load_instructions(filepath)
|
self.scopes = itp.load_instructions(filepath)
|
||||||
Reference in New Issue
Block a user