from code_to_image import NSD_save from Iinstruction import Iinstruction import logging import re class NassiShneidermanDiagram: def __init__(self, debug: bool=False) -> None: self.instructions: dict[str, Iinstruction] = {} self.init_logging(debug) def init_logging(self, debug: bool): logLevel = logging.INFO if debug: logLevel = logging.DEBUG logging.basicConfig(level=logLevel) def add_instruction(self, instruction: Iinstruction): instruction_key = "instruction#" + str(len(self.instructions)) self.instructions[instruction_key] = instruction logging.debug("added instruction %s : %s", instruction_key, instruction.instruction_text) def convert_to_image(self, filename: str, x_size=200): logging.info(f"Saving NSD to {filename}.png") cti.NSD_init(x_size, 1000) x, y, x_sz = 0, 0, x_size for _k, instruction in self.instructions.items(): x, y = instruction.to_image(x, y, x_sz, 200) cti.NSD_save(filename) @staticmethod def load_code_lines(filepath): lines = [] try: with open(filepath) as file: for _line in file: line:str = _line.strip() if line and not re.match(r"""^//|^#|^COMMENT|^--""", line): lines.append(line) except: logging.error(f"Failed to open input file {filepath}!") return lines def load_from_file(self, filepath: str): filtered_lines = self.load_code_lines(filepath) scopes = [] current_scope = scopes for line in filtered_lines: logging.debug(line) if line.__contains__('}'): current_scope.append("scope exit") current_scope = scopes[-1] # does not get correct parent scope #TODO: get correct parent scope if line.__contains__('{'): current_scope.append("scope enter") current_scope.append([]) current_scope = current_scope[-1] elif not line.__contains__('}'): current_scope.append("generic instruction") print(scopes) """if __name__ == "__main__": #for debugging from Iinstruction import * NSD = NassiShneidermanDiagram(True) NSD.load_from_file("res/input/input.java") NSD.convert_to_image("Nina", 500)"""