From c5ebf86912ec38eead3fa6251d029324e9f89a57 Mon Sep 17 00:00:00 2001 From: Sebastian Muthwill Date: Fri, 5 Nov 2021 14:54:09 +0100 Subject: [PATCH 1/2] fix: normalize all path to a propper convention. --- .gitignore | 2 +- docstring_gui/main.py | 8 ++++---- powerapps_docstring/documentation.py | 13 +++++-------- powerapps_docstring/parser.py | 10 +++++----- powerapps_docstring/powerapp.py | 7 +------ 5 files changed, 16 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index bcb2099..b39ee80 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,5 @@ src/* __pycache* build/* .vscode/* -dist/d/* +dist/* *.code-workspace \ No newline at end of file diff --git a/docstring_gui/main.py b/docstring_gui/main.py index 28fda8e..137debf 100644 --- a/docstring_gui/main.py +++ b/docstring_gui/main.py @@ -12,6 +12,7 @@ # import powerapps classes from powerapps_docstring.documentation import Docstring +from powerapps_docstring.powerapp import PowerApp # import screen classes from docstring_gui.screens.result_screen.result_screen import ResultScreen @@ -28,11 +29,10 @@ def create_documentation(self): # Spot check on CanvasManifest.json file. If this exists, it should be # a correct power apps source path - if not os.path.isfile(self.source.text + "CanvasManifest.json"): + source_path = PowerApp(self.source.text).get_pa_src_path() + if not os.path.isfile(os.path.join(source_path, "CanvasManifest.json")): self.source.error = True self.source.helper_text = "Path is not an Power Apps source" - if not self.source.text.endswith("\\"): - self.source.text = self.source.text + "\\" return else: self.source.error = False @@ -59,7 +59,7 @@ def create_documentation(self): # create documentation # TODO: add try block and show error page if somethin went wrong - docstring = Docstring(self.source.text, self.output.text, config_instance) + docstring = Docstring(source_path, self.output.text, config_instance) output_file_path = docstring.create_documentation() # navigate to succeed page diff --git a/powerapps_docstring/documentation.py b/powerapps_docstring/documentation.py index c23eb0a..0025964 100644 --- a/powerapps_docstring/documentation.py +++ b/powerapps_docstring/documentation.py @@ -8,8 +8,8 @@ class Docstring(): def __init__(self, source, output, config) -> None: - self.source_path = source - self.output_path = output + self.source_path = os.path.normpath(source) + self.output_path = os.path.normpath(output) self.parser = Parser(self.source_path) self.config = config self.manifest_file = self.parser.get_canvas_manifest() @@ -18,7 +18,7 @@ def __init__(self, source, output, config) -> None: def _get_screen_files(self): screen_files = [] - screens_path = self.source_path + "/Src/" + screens_path = os.path.join(self.source_path, "Src") # read screen order from manifest and check if files exists screen_order = self.manifest_file["ScreenOrder"] @@ -128,7 +128,6 @@ def get_recursively(search_dict, field): screenflow_list = [":::mermaid", "graph LR"] screen_files = self._get_screen_files() - screens_path = self.source_path + "/Src/" for screen in screen_files: # check if screen has been excluded @@ -221,7 +220,6 @@ def _create_chapter_screens(self): self.md_file.new_line(scr_flow) # loop thru all screens and create markdown - screens_path = self.source_path + "/Src/" for file in self._get_screen_files(): screen_objects = self.parser.get_screen_objects(file) self._extract_screen_content_to_markdown(screen_objects) @@ -236,9 +234,8 @@ def create_documentation(self, format=None): # instantiate the md file # TODO: get title from docstring variable app_name = self.manifest_file["PublishInfo"]["AppName"] - output_file = self.output_path + f'/{app_name}-doc' - self.md_file = MdUtils(file_name=self.output_path + - f'/{app_name}-doc', title='Power App Documentation') + output_file = os.path.join(self.output_path, f'{app_name}-doc') + self.md_file = MdUtils(file_name=output_file, title='Power App Documentation') for chapter in self.config["DocumentStructure"]: if chapter == "App": diff --git a/powerapps_docstring/parser.py b/powerapps_docstring/parser.py index d096079..489797c 100644 --- a/powerapps_docstring/parser.py +++ b/powerapps_docstring/parser.py @@ -12,7 +12,7 @@ def get_connections(self) -> dict: """ connections = {} # create empty dict - connections_file = self.source_path + "Connections/Connections.json" + connections_file = os.path.join(self.source_path, "Connections", "Connections.json") if os.path.isfile(connections_file): with open(connections_file, "r") as file: connections = json.load(file) @@ -20,7 +20,7 @@ def get_connections(self) -> dict: return connections def _get_screen_content(self, screen_name): - screen_path = self.source_path + "src/" + screen_name + screen_path = os.path.join(self.source_path, "src", screen_name) screen_content = {} with open(screen_path, "r", encoding='utf8') as file: @@ -35,10 +35,10 @@ def get_screen_objects(self, screen_name) -> tuple: return screen_name, screen_content def get_canvas_manifest(self): - app_name = "PowerApp_Documentation" # get name from CanvasManifest.json - if os.path.isfile(self.source_path + "CanvasManifest.json"): - with open(self.source_path + "CanvasManifest.json", "r", encoding="utf-8") as file: + manifest_file = os.path.join(self.source_path, "CanvasManifest.json") + if os.path.isfile(manifest_file): + with open(manifest_file, "r", encoding="utf-8") as file: canvas_manifest = json.load(file) return canvas_manifest diff --git a/powerapps_docstring/powerapp.py b/powerapps_docstring/powerapp.py index 0592928..f07e7e7 100644 --- a/powerapps_docstring/powerapp.py +++ b/powerapps_docstring/powerapp.py @@ -5,17 +5,12 @@ class UnknownSourceException(Exception): class PowerApp(): def __init__(self, source) -> None: - self.source = source + self.source = os.path.normpath(source) self.source_type = self._check_source_type() def get_pa_src_path(self): source_path = None if self.source_type == "directory": - # TODO: check if directory contains powerapp content - - if self.source[-1] != "/": - self.source = self.source + "/" - source_path = self.source elif self.source_type == "zip": # TODO: unzip and unpack msssap to retrieve src folder From ea7fd66d869c88a1cd1c86e00dbc1773fac0d830 Mon Sep 17 00:00:00 2001 From: Sebastian Muthwill Date: Fri, 5 Nov 2021 14:57:03 +0100 Subject: [PATCH 2/2] fix: catch exception on GUI close --- main.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index ce02833..a132e37 100644 --- a/main.py +++ b/main.py @@ -21,7 +21,11 @@ def main(argv): # if programm started without arguments, we run the GUI if len(opts) == 0: from docstring_gui import main as gui_main - gui_main() + try: + gui_main() + except TypeError: + # when donwstream application is terminated, it will thro a TypeError exception. + pass sys.exit(1)