made image generation exception safe
This commit is contained in:
13
draw/code_to_image_wrapper.py
Normal file
13
draw/code_to_image_wrapper.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import draw.code_to_image as cti
|
||||||
|
|
||||||
|
class NSD_writer(object):
|
||||||
|
def __init__(self, filepath: str, x_sz: int, y_sz: int) -> None:
|
||||||
|
self.filepath = filepath
|
||||||
|
self.x_sz = x_sz
|
||||||
|
self.y_sz = y_sz
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
cti.NSD_init(self.x_sz, self.y_sz)
|
||||||
|
|
||||||
|
def __exit__(self, _, __, ___):
|
||||||
|
cti.NSD_save(self.filepath)
|
||||||
@@ -3,7 +3,7 @@ import logging
|
|||||||
|
|
||||||
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 import code_to_image as cti
|
from draw.code_to_image_wrapper import NSD_writer
|
||||||
|
|
||||||
|
|
||||||
class NassiShneidermanDiagram:
|
class NassiShneidermanDiagram:
|
||||||
@@ -29,15 +29,14 @@ class NassiShneidermanDiagram:
|
|||||||
return int(h)
|
return int(h)
|
||||||
|
|
||||||
def convert_to_image(self, filename: str, x_size: int=200):
|
def convert_to_image(self, filename: str, x_size: int=200):
|
||||||
logging.info(f"Saving NSD to {filename}.png")
|
|
||||||
image_y_sz = self.get_image_height()
|
image_y_sz = self.get_image_height()
|
||||||
cti.NSD_init(x_size, image_y_sz)
|
logging.info(f"Saving NSD to {filename}.png...")
|
||||||
x, y = 0, 0
|
with NSD_writer(filename, x_size, image_y_sz):
|
||||||
|
x, y = 0, 0
|
||||||
|
|
||||||
for instruction in self.instructions:
|
for instruction in self.instructions:
|
||||||
x, y = instruction.to_image(x, y, x_size)
|
x, y = instruction.to_image(x, y, x_size)
|
||||||
|
logging.info("Done!")
|
||||||
cti.NSD_save(filename)
|
|
||||||
|
|
||||||
def load_from_file(self, filepath:str):
|
def load_from_file(self, filepath:str):
|
||||||
instructions = itp.load_instructions(filepath)
|
instructions = itp.load_instructions(filepath)
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ def handle_do_while(line: str, src: List[str], i: int) -> Tuple[Iinstruction, in
|
|||||||
bracket_index = end_line.rindex(')') #throws if contruct id ill-formed
|
bracket_index = end_line.rindex(')') #throws if contruct id ill-formed
|
||||||
instruction_txt = end_line[7:bracket_index]
|
instruction_txt = end_line[7:bracket_index]
|
||||||
elif check_line_start(src, i+1, "while("):
|
elif check_line_start(src, i+1, "while("):
|
||||||
i += 1 #make sure we return the correct value for i
|
i += 1
|
||||||
end_line = src[i]
|
end_line = src[i]
|
||||||
bracket_index = end_line.rindex(')')
|
bracket_index = end_line.rindex(')')
|
||||||
instruction_txt = end_line[6:bracket_index]
|
instruction_txt = end_line[6:bracket_index]
|
||||||
@@ -167,5 +167,5 @@ def load_instructions(filepath: str) -> List[Iinstruction]:
|
|||||||
src = load_src(filepath)
|
src = load_src(filepath)
|
||||||
instructions, i = get_instructions_in_scope(src)
|
instructions, i = get_instructions_in_scope(src)
|
||||||
if i != len(src):
|
if i != len(src):
|
||||||
raise InterpreterError("Unknown error during source interpretation! Unsupported language constructs or ill-formed?")
|
raise InterpreterError("Unknown error during source interpretation! Unsupported language constructs or ill-formed source?")
|
||||||
return instructions
|
return instructions
|
||||||
Reference in New Issue
Block a user