mirror of
https://github.com/infeeeee/dyn2py
synced 2025-12-16 22:16:18 +01:00
Rework exceptions, class methods for open files
This commit is contained in:
@@ -31,8 +31,7 @@ def extract_single_node_dyn(modify_py: bool = False):
|
||||
pythonfiles = dyn.extract_python(options)
|
||||
pythonfiles[0].write()
|
||||
|
||||
dyn2py.PythonFile.open_files.clear()
|
||||
dyn2py.DynamoFile.open_files.clear()
|
||||
dyn2py.File.open_files.clear()
|
||||
|
||||
if modify_py:
|
||||
# Open the extracted file and replace a string:
|
||||
|
||||
@@ -19,7 +19,7 @@ class TestDynamoFile(unittest.TestCase):
|
||||
self.assertTrue(dyn in dyn2py.DynamoFile.open_files)
|
||||
|
||||
# Dynamo 1 file:
|
||||
with self.assertRaises(dyn2py.DynamoFileException):
|
||||
with self.assertRaises(dyn2py.DynamoFile.Error):
|
||||
dyn1 = dyn2py.DynamoFile(f"{INPUT_DIR}/dynamo1file.dyn")
|
||||
|
||||
# Not existing file:
|
||||
@@ -27,7 +27,7 @@ class TestDynamoFile(unittest.TestCase):
|
||||
dyn2 = dyn2py.DynamoFile(f"{INPUT_DIR}/not_existing.dyn")
|
||||
|
||||
# No python nodes:
|
||||
with self.assertRaises(dyn2py.PythonNodeNotFoundException):
|
||||
with self.assertRaises(dyn2py.DynamoFile.PythonNodeNotFound):
|
||||
dyn2 = dyn2py.DynamoFile(f"{INPUT_DIR}/no_python.dyn")
|
||||
|
||||
def test_get_python_nodes(self):
|
||||
@@ -39,7 +39,7 @@ class TestDynamoFile(unittest.TestCase):
|
||||
self.assertIn(py_node, dyn.python_nodes)
|
||||
self.assertEqual(py_node.checksum, "e830a6ae6b395bcfd4e5a40da48f3bfc")
|
||||
|
||||
with self.assertRaises(dyn2py.PythonNodeNotFoundException):
|
||||
with self.assertRaises(dyn2py.DynamoFile.PythonNodeNotFound):
|
||||
dyn.get_python_node_by_id("wrongid")
|
||||
|
||||
def test_extract_python(self):
|
||||
@@ -50,10 +50,9 @@ class TestDynamoFile(unittest.TestCase):
|
||||
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/python_nodes.dyn")
|
||||
dyn.extract_python(options=opt)
|
||||
|
||||
self.assertEqual(len(dyn2py.PythonFile.open_files), 6)
|
||||
self.assertEqual(len(dyn2py.PythonFile.get_open_files()), 6)
|
||||
|
||||
for f in dyn2py.PythonFile.open_files:
|
||||
f.write()
|
||||
dyn2py.PythonFile.write_open_files()
|
||||
|
||||
output_dir = pathlib.Path(OUTPUT_DIR)
|
||||
self.assertEqual(len(list(output_dir.iterdir())), 6)
|
||||
@@ -139,6 +138,6 @@ class TestDynamoFile(unittest.TestCase):
|
||||
self.assertTrue(node2)
|
||||
self.assertEqual(node1.checksum, node2.checksum)
|
||||
|
||||
with self.assertRaises(dyn2py.PythonNodeNotFoundException):
|
||||
with self.assertRaises(dyn2py.DynamoFile.PythonNodeNotFound):
|
||||
node2.id = "wrong_id"
|
||||
dyn2.update_python_node(node2)
|
||||
|
||||
@@ -12,9 +12,10 @@ class TestPythonFile(unittest.TestCase):
|
||||
def test_init(self):
|
||||
extract_single_node_dyn()
|
||||
|
||||
py1 = dyn2py.PythonFile(f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py")
|
||||
dyn2py.File.open_files.clear()
|
||||
|
||||
dyn2py.DynamoFile.open_files.clear()
|
||||
py1 = dyn2py.PythonFile(
|
||||
f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py")
|
||||
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
|
||||
node = list(dyn.python_nodes)[0]
|
||||
py2 = dyn2py.PythonFile(filepath=node.filepath,
|
||||
@@ -23,9 +24,10 @@ class TestPythonFile(unittest.TestCase):
|
||||
for py in [py1, py2]:
|
||||
|
||||
self.assertEqual(len(py.code), 17)
|
||||
self.assertEqual(len(py.text.split(os.linesep)), 32, msg=py.filepath)
|
||||
self.assertEqual(len(py.text.split(os.linesep)),
|
||||
32, msg=py.filepath)
|
||||
self.assertIs(type(py.header_data), dict)
|
||||
self.assertTrue(py in dyn2py.PythonFile.open_files)
|
||||
self.assertTrue(py in dyn2py.PythonFile.get_open_files())
|
||||
|
||||
def test_update_dynamo(self):
|
||||
extract_single_node_dyn(modify_py=True)
|
||||
@@ -68,14 +70,13 @@ class TestPythonFile(unittest.TestCase):
|
||||
|
||||
def test_get_source_dynamo_file(self):
|
||||
extract_single_node_dyn()
|
||||
dyn2py.DynamoFile.open_files.clear()
|
||||
dyn2py.PythonFile.open_files.clear()
|
||||
dyn2py.File.open_files.clear()
|
||||
|
||||
py1 = dyn2py.PythonFile(
|
||||
f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py")
|
||||
|
||||
dyn1 = py1.get_source_dynamo_file()
|
||||
self.assertEqual(len(dyn2py.DynamoFile.open_files), 1)
|
||||
self.assertEqual(len(dyn2py.DynamoFile.get_open_files()), 1)
|
||||
self.assertIn(dyn1, dyn2py.DynamoFile.open_files)
|
||||
|
||||
dyn2 = py1.get_source_dynamo_file()
|
||||
@@ -83,14 +84,13 @@ class TestPythonFile(unittest.TestCase):
|
||||
|
||||
dyn2py.DynamoFile.open_files.clear()
|
||||
|
||||
with self.assertRaises(dyn2py.DynamoFileException):
|
||||
with self.assertRaises(dyn2py.DynamoFile.Error):
|
||||
py1.header_data["dyn_uuid"] = "wrong-uuid"
|
||||
py1.get_source_dynamo_file()
|
||||
|
||||
def test_write(self):
|
||||
extract_single_node_dyn()
|
||||
dyn2py.DynamoFile.open_files.clear()
|
||||
dyn2py.PythonFile.open_files.clear()
|
||||
dyn2py.File.open_files.clear()
|
||||
|
||||
py1 = dyn2py.PythonFile(
|
||||
f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py")
|
||||
@@ -98,7 +98,7 @@ class TestPythonFile(unittest.TestCase):
|
||||
dyn1 = py1.get_source_dynamo_file()
|
||||
node = list(dyn1.python_nodes)[0]
|
||||
py2 = dyn2py.PythonFile(
|
||||
node.filepath, dynamo_file=dyn1, python_node=node)
|
||||
f"{OUTPUT_DIR}/{node.filename}", dynamo_file=dyn1, python_node=node)
|
||||
self.assertIsNot(py1, py2)
|
||||
self.assertEqual(py1.code, py2.code)
|
||||
for d in py1.header_data:
|
||||
|
||||
@@ -48,11 +48,12 @@ class TestPythonNode(unittest.TestCase):
|
||||
|
||||
py = dyn2py.PythonFile(f"{OUTPUT_DIR}/single_node_mod.py")
|
||||
|
||||
with self.assertRaises(dyn2py.PythonNodeException):
|
||||
with self.assertRaises(dyn2py.PythonNode.Error):
|
||||
node1 = dyn2py.PythonNode(
|
||||
node_dict_from_dyn=node_dict,
|
||||
dynamo_file=dyn,
|
||||
python_file=py
|
||||
)
|
||||
|
||||
|
||||
with self.assertRaises(dyn2py.PythonNode.Error):
|
||||
node2 = dyn2py.PythonNode()
|
||||
|
||||
Reference in New Issue
Block a user