From 8bf49e052a68d0e2abfcc9f205b5992ba8913ba9 Mon Sep 17 00:00:00 2001 From: infeeeee Date: Wed, 8 Mar 2023 19:07:39 +0100 Subject: [PATCH] Tests on Windows, better exception handling --- dyn2py/__init__.py | 6 ++- dyn2py/files.py | 71 +++++++++++++++++-------------- tests/input_files/dynamo1file.dyn | 30 +++++++++++++ tests/test_CommandLine.py | 33 +++++++++----- tests/test_DynamoFile.py | 9 ++++ 5 files changed, 105 insertions(+), 44 deletions(-) create mode 100644 tests/input_files/dynamo1file.dyn diff --git a/dyn2py/__init__.py b/dyn2py/__init__.py index a4a70e9..c1d4d65 100644 --- a/dyn2py/__init__.py +++ b/dyn2py/__init__.py @@ -146,7 +146,11 @@ def run(options: Options | None = None) -> None: if f.is_dynamo_file(): logging.debug("Source is a Dynamo file") dynamo_file = DynamoFile(f.filepath) - dynamo_file.extract_python(options) + + try: + dynamo_file.extract_python(options) + except DynamoFileException as e: + logging.error(f"{e} Skipping") elif f.is_python_file(): logging.debug("Source is a Python file") diff --git a/dyn2py/files.py b/dyn2py/files.py index b319264..3c82456 100644 --- a/dyn2py/files.py +++ b/dyn2py/files.py @@ -149,48 +149,55 @@ class DynamoFile(File): logging.info(f"Extracting from file: {self.filepath}") - try: - self.read() + self.read() - # Go through nodes in the file: - for python_node in self.get_python_nodes(): - if options.python_folder: - python_file_path = options.python_folder.joinpath( - python_node.filename) - else: - python_file_path = python_node.filepath + # Go through nodes in the file: + for python_node in self.get_python_nodes(): + if options.python_folder: + python_file_path = options.python_folder.joinpath( + python_node.filename) + else: + python_file_path = python_node.filepath - python_file = PythonFile(python_file_path) - python_file.generate_text( - dynamo_file=self, python_node=python_node) + python_file = PythonFile(python_file_path) + python_file.generate_text( + dynamo_file=self, python_node=python_node) - if python_file.is_newer(self) and not options.force: - logging.info( - f"Existing file is newer, skipping: {python_file.filepath}") - continue + if python_file.is_newer(self) and not options.force: + logging.info( + f"Existing file is newer, skipping: {python_file.filepath}") + continue - python_file.write(options) - - except DynamoFileException as e: - logging.warn(e) - return - except json.JSONDecodeError: - logging.error( - "File is not correctly formatted. Is it a Dynamo2 file?") - return + python_file.write(options) def read(self) -> None: - """Read Dynamo graph to parameters""" + """Read Dynamo graph to parameters + + Raises: + DynamoFileException: If the file is a Dynamo 1 file + json.JSONDecodeError: If there are any other problem with the file + """ + + if not self.exists: + raise FileNotFoundError + # Only read if it's not already open: if not self in self.open_files: logging.debug(f"Reading file: {self.filepath}") - with open(self.filepath, "r", encoding="utf-8") as input_json: - self.full_dict = json.load(input_json, - use_decimal=True) - self.uuid = self.full_dict["Uuid"] - self.name = self.full_dict["Name"] - self.open_files.add(self) + try: + with open(self.filepath, "r", encoding="utf-8") as input_json: + self.full_dict = json.load(input_json, + use_decimal=True) + self.uuid = self.full_dict["Uuid"] + self.name = self.full_dict["Name"] + self.open_files.add(self) + except json.JSONDecodeError as e: + with open(self.filepath, "r", encoding="utf-8") as input_json: + if input_json.readline().startswith(" list["PythonNode"]: """Get python nodes from the Dynamo graph diff --git a/tests/input_files/dynamo1file.dyn b/tests/input_files/dynamo1file.dyn new file mode 100644 index 0000000..5bc9387 --- /dev/null +++ b/tests/input_files/dynamo1file.dyn @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/test_CommandLine.py b/tests/test_CommandLine.py index 91c5f38..bcb68af 100644 --- a/tests/test_CommandLine.py +++ b/tests/test_CommandLine.py @@ -1,5 +1,6 @@ import unittest import subprocess +import platform class TestCommandLine(unittest.TestCase): @@ -12,20 +13,30 @@ class TestCommandLine(unittest.TestCase): is_help_line = False for line in readme.readlines(): - if line == "> dyn2py --help": + line_text = line.rstrip() + + if line_text == "> dyn2py --help": is_help_line = True - elif is_help_line and line == "```": + elif is_help_line and line_text == "```": # It's the end of the help break elif is_help_line: - readme_help_lines.append(line) + readme_help_lines.append(line_text) - for arg in args: - p = subprocess.run( - ["dyn2py", arg], capture_output=True, shell=True) - output_help = p.stdout.decode() - output_help_lines = output_help.split("\n") + # Check if readme was read at all: + self.assertTrue(readme_help_lines) - self.assertEqual(output_help.count("\n"), len(readme_help_lines)) - for i, l in enumerate(readme_help_lines): - self.assertEqual(l, output_help_lines[i]) + # Cannot set terminal columns on windows, so simply skip this: + if not platform.system() == "Windows": + for arg in args: + p = subprocess.run( + ["dyn2py", arg], capture_output=True, shell=True) + output_help = p.stdout.decode() + output_help_lines = output_help.split("\n") + + self.assertEqual( + output_help.count("\n"), + len(readme_help_lines)) + + for i, l in enumerate(readme_help_lines): + self.assertEqual(l, output_help_lines[i]) diff --git a/tests/test_DynamoFile.py b/tests/test_DynamoFile.py index ce66879..6c78b29 100644 --- a/tests/test_DynamoFile.py +++ b/tests/test_DynamoFile.py @@ -29,6 +29,15 @@ class TestDynamoFile(unittest.TestCase): self.assertEqual(dyn.name, "python_nodes") self.assertTrue(dyn in dyn.open_files) + with self.assertRaises(dyn2py.DynamoFileException): + dyn1 = dyn2py.DynamoFile(f"{INPUT_DIR}/dynamo1file.dyn") + dyn1.read() + + with self.assertRaises(FileNotFoundError): + dyn2 = dyn2py.DynamoFile(f"{INPUT_DIR}/not_existing.dyn") + dyn2.read() + + def test_get_python_node(self): dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/python_nodes.dyn") py_nodes = dyn.get_python_nodes()