mirror of
https://github.com/infeeeee/dyn2py
synced 2025-12-16 22:16:18 +01:00
Tests on Windows, better exception handling
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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("<Workspace Version="):
|
||||
raise DynamoFileException("This is a Dynamo 1 file!")
|
||||
else:
|
||||
raise e
|
||||
|
||||
def get_python_nodes(self) -> list["PythonNode"]:
|
||||
"""Get python nodes from the Dynamo graph
|
||||
|
||||
30
tests/input_files/dynamo1file.dyn
Normal file
30
tests/input_files/dynamo1file.dyn
Normal file
@@ -0,0 +1,30 @@
|
||||
<Workspace Version="0.7.1.2932" X="-589.793616458388" Y="-28.0702613397556" zoom="0.571728258213614" Description="" Category="" Name="Home">
|
||||
<Elements>
|
||||
<Dynamo.Nodes.StringInput type="Dynamo.Nodes.StringInput" guid="b6383d0b-3463-4739-8f67-af511eeb5496" nickname="String" x="1463.88410839859" y="775.621975339227" isVisible="true" isUpstreamVisible="true" lacing="Disabled">
|
||||
<System.String value="C:\Users\Erick\Desktop\ADSK\ReferenceFiles\RoofPanels-subtle.xlsx" />
|
||||
</Dynamo.Nodes.StringInput>
|
||||
<Dynamo.Nodes.StringInput type="Dynamo.Nodes.StringInput" guid="7a8133a6-991d-45a5-816a-ee6429d4693d" nickname="String" x="1472.75997420503" y="895.385427726316" isVisible="true" isUpstreamVisible="true" lacing="Disabled">
|
||||
<System.String value="Sheet1" />
|
||||
</Dynamo.Nodes.StringInput>
|
||||
<Dynamo.Nodes.DSFunction type="Dynamo.Nodes.DSFunction" guid="ca479114-6c9e-4b3d-a385-81addfccf901" nickname="Excel.Read" x="1743.72609696551" y="816.73202623077" isVisible="true" isUpstreamVisible="true" lacing="Shortest" assembly="DSOffice.dll" function="DSOffice.Excel.Read@string,string" />
|
||||
<Dynamo.Nodes.DSFunction type="Dynamo.Nodes.DSFunction" guid="4a4f74f2-2e6a-4658-8523-3e26fb6e46df" nickname="Polygon.ByPoints" x="2986.00224327805" y="875.201652053384" isVisible="true" isUpstreamVisible="true" lacing="Shortest" assembly="ProtoGeometry.dll" function="Autodesk.DesignScript.Geometry.Polygon.ByPoints@Autodesk.DesignScript.Geometry.Point[]" />
|
||||
<Dynamo.Nodes.DSFunction type="Dynamo.Nodes.DSFunction" guid="e3f7f250-f697-45a1-b452-e539bcaa5729" nickname="Surface.ByPatch" x="3203.78149274746" y="874.510289356656" isVisible="true" isUpstreamVisible="true" lacing="Shortest" assembly="ProtoGeometry.dll" function="Autodesk.DesignScript.Geometry.Surface.ByPatch@Autodesk.DesignScript.Geometry.Curve" />
|
||||
<Dynamo.Nodes.DSFunction type="Dynamo.Nodes.DSFunction" guid="d6be51a0-0ec1-4462-891c-963ba121eae7" nickname="AdaptiveComponent.ByPoints" x="3087.8976476378" y="1165.99415025959" isVisible="true" isUpstreamVisible="true" lacing="Shortest" assembly="RevitNodes.dll" function="Revit.Elements.AdaptiveComponent.ByPoints@Autodesk.DesignScript.Geometry.Point[],Revit.Elements.FamilySymbol" />
|
||||
<DSRevitNodesUI.FamilyTypes type="DSRevitNodesUI.FamilyTypes" guid="743df6ca-5a50-43cc-8ff7-f7923e51db8e" nickname="Family Types" x="2658.54986209909" y="1366.64973129711" isVisible="true" isUpstreamVisible="true" lacing="Disabled" index="100" />
|
||||
<Dynamo.Nodes.CodeBlockNodeModel type="Dynamo.Nodes.CodeBlockNodeModel" guid="87602511-a932-4c98-8b6b-f46ed0dfc216" nickname="Code Block" x="1617.39906775223" y="303.481659876028" isVisible="true" isUpstreamVisible="true" lacing="Disabled" CodeText="def ExcelPoints(x,pointIndex)
{
pointValues=List.GetItemAtIndex(String.ToNumber(String.Split(List.Transpose(x),",")),pointIndex);
pointItems=List.Transpose(pointValues);
px=List.GetItemAtIndex(pointItems,0);
py=List.GetItemAtIndex(pointItems,1);
pz=List.GetItemAtIndex(pointItems,2);
return=Point.ByCoordinates(px,py,pz);
}" ShouldFocus="false" />
|
||||
<Dynamo.Nodes.DoubleInput type="Dynamo.Nodes.DoubleInput" guid="7feba4ef-5609-450b-b542-afecc65610f1" nickname="Number" x="2290.18494819169" y="908.180076994827" isVisible="true" isUpstreamVisible="true" lacing="Disabled">
|
||||
<System.Double value="0" />
|
||||
</Dynamo.Nodes.DoubleInput>
|
||||
<Dynamo.Nodes.CodeBlockNodeModel type="Dynamo.Nodes.CodeBlockNodeModel" guid="c073bc37-b556-4ca1-a123-50b4af301c72" nickname="Code Block" x="2497.57994977484" y="866.046123724641" isVisible="true" isUpstreamVisible="true" lacing="Disabled" CodeText="ExcelPoints(excelOutput,pointOfInterest);" ShouldFocus="false" />
|
||||
</Elements>
|
||||
<Connectors>
|
||||
<Dynamo.Models.ConnectorModel start="b6383d0b-3463-4739-8f67-af511eeb5496" start_index="0" end="ca479114-6c9e-4b3d-a385-81addfccf901" end_index="0" portType="0" />
|
||||
<Dynamo.Models.ConnectorModel start="7a8133a6-991d-45a5-816a-ee6429d4693d" start_index="0" end="ca479114-6c9e-4b3d-a385-81addfccf901" end_index="1" portType="0" />
|
||||
<Dynamo.Models.ConnectorModel start="ca479114-6c9e-4b3d-a385-81addfccf901" start_index="0" end="c073bc37-b556-4ca1-a123-50b4af301c72" end_index="0" portType="0" />
|
||||
<Dynamo.Models.ConnectorModel start="4a4f74f2-2e6a-4658-8523-3e26fb6e46df" start_index="0" end="e3f7f250-f697-45a1-b452-e539bcaa5729" end_index="0" portType="0" />
|
||||
<Dynamo.Models.ConnectorModel start="743df6ca-5a50-43cc-8ff7-f7923e51db8e" start_index="0" end="d6be51a0-0ec1-4462-891c-963ba121eae7" end_index="1" portType="0" />
|
||||
<Dynamo.Models.ConnectorModel start="7feba4ef-5609-450b-b542-afecc65610f1" start_index="0" end="c073bc37-b556-4ca1-a123-50b4af301c72" end_index="1" portType="0" />
|
||||
<Dynamo.Models.ConnectorModel start="c073bc37-b556-4ca1-a123-50b4af301c72" start_index="0" end="4a4f74f2-2e6a-4658-8523-3e26fb6e46df" end_index="0" portType="0" />
|
||||
</Connectors>
|
||||
<Notes />
|
||||
</Workspace>
|
||||
@@ -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])
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user