Repository: Open-EO/openeo-pg-parser-python

Issue:
Unclear error when JSON file not found
Currently, when the input JSON file (or related python dict) is not found, the following error is returned:
`UnboundLocalError: local variable 'pg_dict' referenced before assignment`

This is not clear and very unfriendly to debug, add check if input file exists or if input dict is not empty.

Discussion:

Patch:
diff --git a/src/openeo_pg_parser_python/translate_process_graph.py b/src/openeo_pg_parser_python/translate_process_graph.py
index bbbffde..3982dcd 100644
--- a/src/openeo_pg_parser_python/translate_process_graph.py
+++ b/src/openeo_pg_parser_python/translate_process_graph.py
@@ -216,10 +216,12 @@ def link_nodes(graph):
 
 def translate_graph(pg_filepath):
     if isinstance(pg_filepath, str):
-        if os.path.isfile(pg_filepath):
-            pg_dict = load(open(pg_filepath))
+        pg_dict = load(open(pg_filepath))
     elif isinstance(pg_filepath, dict):
         pg_dict = pg_filepath
+    else:
+        raise ValueError("'pg_filepath must either be file path to a JSON file or a dictionary.'")
+
     nodes = OrderedDict()
     nodes, _, _, _ = walk_pg_graph(nodes, pg_dict)
     # create graph object


Test patch:
diff --git a/tests/test_translate_pg.py b/tests/test_translate_pg.py
new file mode 100644
index 0000000..20140b8
--- /dev/null
+++ b/tests/test_translate_pg.py
@@ -0,0 +1,21 @@
+import unittest
+from openeo_pg_parser_python.translate_process_graph import translate_graph
+
+
+class PGTranslateTester(unittest.TestCase):
+    """ Responsible for testing the translation of an openEO process graph. """
+
+    def setUp(self):
+        """ Specifies paths to the test data. """
+
+        self.pg_test_1_filepath = r"process_graphs/test_1.json"
+
+    def test_pg_not_found(self):
+        pg_filepath = r"process_graphs/does_not_exist.json"
+        try:
+            translate_graph(pg_filepath)
+        except FileNotFoundError:
+            assert True
+
+if __name__ == '__main__':
+    unittest.main()
\ No newline at end of file