Merge branch 'dev'

This commit is contained in:
2023-03-24 17:17:41 +01:00
6 changed files with 31 additions and 28 deletions

View File

@@ -11,10 +11,11 @@ permissions:
jobs: jobs:
tests: tests:
runs-on: ubuntu-latest
strategy: strategy:
matrix: matrix:
os: [ubuntu-latest, windows-latest]
python-version: [3.8, 3.9, "3.10", 3.11] python-version: [3.8, 3.9, "3.10", 3.11]
runs-on: ${{ matrix.os }}
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3

View File

@@ -1,4 +1,4 @@
name: website name: Deploy website
# build the documentation whenever there are new commits on main # build the documentation whenever there are new commits on main
on: on:

View File

@@ -2,7 +2,6 @@ from __future__ import annotations
import simplejson as json import simplejson as json
import hashlib import hashlib
import pathlib import pathlib
import textwrap
import logging import logging
import os import os
from datetime import datetime from datetime import datetime
@@ -317,8 +316,8 @@ class DynamoFile(File):
self.python_nodes.remove(python_node_in_file) self.python_nodes.remove(python_node_in_file)
self.python_nodes.add(python_node) self.python_nodes.add(python_node)
# Update the dict: # Update the dict, Dyn files are always CRLF:
node_dict["Code"] = python_node.code node_dict["Code"] = "\r\n".join(python_node.code)
self.modified = True self.modified = True
@@ -371,8 +370,8 @@ class DynamoFile(File):
class PythonFile(File): class PythonFile(File):
"""A Python file, subclass of File()""" """A Python file, subclass of File()"""
code: str code: list[str]
"""The python code as a string.""" """The python code."""
header_data: dict header_data: dict
"""Parsed dict from the header of a python file.""" """Parsed dict from the header of a python file."""
text: str text: str
@@ -399,10 +398,10 @@ class PythonFile(File):
# Do not read from disk: # Do not read from disk:
super().__init__(filepath, read_from_disk=False) super().__init__(filepath, read_from_disk=False)
header_notice = """\ header_notice = os.linesep.join([
This file was generated with dyn2py from a Dynamo graph. "This file was generated with dyn2py from a Dynamo graph.",
Do not edit this section, if you want to update the Dynamo graph!\ "Do not edit this section, if you want to update the Dynamo graph!"
""" ])
# Double escape path: # Double escape path:
dyn_path_string = str(dynamo_file.realpath) dyn_path_string = str(dynamo_file.realpath)
@@ -420,19 +419,19 @@ class PythonFile(File):
"py_engine": python_node.engine "py_engine": python_node.engine
} }
header_string = "\r\n".join( header_string = os.linesep.join(
[f"{k}:{self.header_data[k]}" for k in self.header_data]) [f"{k}:{self.header_data[k]}" for k in self.header_data])
header_wrapper = '"""' header_wrapper = '"""'
self.text = "\r\n".join([ self.text = os.linesep.join([
header_wrapper, header_wrapper,
HEADER_SEPARATOR, HEADER_SEPARATOR,
textwrap.dedent(header_notice), header_notice,
HEADER_SEPARATOR, HEADER_SEPARATOR,
header_string, header_string,
HEADER_SEPARATOR, HEADER_SEPARATOR,
header_wrapper, header_wrapper,
python_node.code os.linesep.join(python_node.code)
]) ])
self.code = python_node.code self.code = python_node.code
@@ -462,7 +461,8 @@ class PythonFile(File):
logging.info(f"Reading file: {self.filepath}") logging.info(f"Reading file: {self.filepath}")
with open(self.filepath, mode="r", newline="", encoding="utf-8") as input_py: with open(self.filepath, mode="r", newline="", encoding="utf-8") as input_py:
python_lines = input_py.readlines() python_lines = [line.strip("\r\n")
for line in input_py.readlines()]
self.text = os.linesep.join(python_lines) self.text = os.linesep.join(python_lines)
self.header_data = {} self.header_data = {}
@@ -470,7 +470,6 @@ class PythonFile(File):
code_start_line = 0 code_start_line = 0
for i, line in enumerate(python_lines): for i, line in enumerate(python_lines):
line = line.strip()
logging.debug(f"Reading line: {line}") logging.debug(f"Reading line: {line}")
# Skip the first lines: # Skip the first lines:
@@ -490,7 +489,7 @@ class PythonFile(File):
raise PythonFileException("Error reading header!") raise PythonFileException("Error reading header!")
self.header_data[line[0:sl]] = line[sl+1:] self.header_data[line[0:sl]] = line[sl+1:]
self.code = "".join(python_lines[code_start_line:]) self.code = python_lines[code_start_line:]
self.open_files.add(self) self.open_files.add(self)
logging.debug(f"Header data from python file: {self.header_data}") logging.debug(f"Header data from python file: {self.header_data}")
@@ -564,7 +563,7 @@ class PythonNode():
"""The id of the node""" """The id of the node"""
engine: str engine: str
"""The engine of the node, IronPython2 or CPython3""" """The engine of the node, IronPython2 or CPython3"""
code: str code: list[str]
"""The full code""" """The full code"""
checksum: str checksum: str
"""The checksum of the code, for checking changes""" """The checksum of the code, for checking changes"""
@@ -600,7 +599,8 @@ class PythonNode():
else: else:
self.engine = "IronPython2" self.engine = "IronPython2"
self.code = node_dict_from_dyn["Code"] # It's read from a dynamo file, so separator is always CRLF
self.code = node_dict_from_dyn["Code"].split("\r\n")
# Get the name of the node: # Get the name of the node:
self.name = next( self.name = next(
@@ -619,6 +619,7 @@ class PythonNode():
"_".join(filename_parts) + ".py") "_".join(filename_parts) + ".py")
self.filepath = dynamo_file.dirpath.joinpath(self.filename) self.filepath = dynamo_file.dirpath.joinpath(self.filename)
# Initialize from a python file:
elif python_file and not node_dict_from_dyn and not dynamo_file: elif python_file and not node_dict_from_dyn and not dynamo_file:
self.id = python_file.header_data["py_id"] self.id = python_file.header_data["py_id"]
self.engine = python_file.header_data["py_engine"] self.engine = python_file.header_data["py_engine"]
@@ -630,4 +631,5 @@ class PythonNode():
raise PythonNodeException raise PythonNodeException
# Calculate checksum: # Calculate checksum:
self.checksum = hashlib.md5(self.code.encode()).hexdigest() checksums = [hashlib.md5(l.encode()).hexdigest() for l in self.code]
self.checksum = hashlib.md5("".join(checksums).encode()).hexdigest()

View File

@@ -37,7 +37,7 @@ class TestDynamoFile(unittest.TestCase):
self.assertEqual(len(dyn.python_nodes), 6) self.assertEqual(len(dyn.python_nodes), 6)
self.assertTrue(py_node) self.assertTrue(py_node)
self.assertIn(py_node, dyn.python_nodes) self.assertIn(py_node, dyn.python_nodes)
self.assertEqual(py_node.checksum, "1f3d9e6153804fe1ed37571a9cda8e26") self.assertEqual(py_node.checksum, "e830a6ae6b395bcfd4e5a40da48f3bfc")
with self.assertRaises(dyn2py.PythonNodeNotFoundException): with self.assertRaises(dyn2py.PythonNodeNotFoundException):
dyn.get_python_node_by_id("wrongid") dyn.get_python_node_by_id("wrongid")

View File

@@ -2,6 +2,7 @@ import unittest
import dyn2py import dyn2py
import shutil import shutil
from time import sleep from time import sleep
import os
from tests.support import * from tests.support import *
@@ -11,8 +12,7 @@ class TestPythonFile(unittest.TestCase):
def test_init(self): def test_init(self):
extract_single_node_dyn() extract_single_node_dyn()
py1 = dyn2py.PythonFile( py1 = dyn2py.PythonFile(f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py")
f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py")
dyn2py.DynamoFile.open_files.clear() dyn2py.DynamoFile.open_files.clear()
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn") dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
@@ -22,8 +22,8 @@ class TestPythonFile(unittest.TestCase):
for py in [py1, py2]: for py in [py1, py2]:
self.assertEqual(len(py.code.split("\n")), 17) self.assertEqual(len(py.code), 17)
self.assertEqual(len(py.text.split("\r\n")), 31) self.assertEqual(len(py.text.split(os.linesep)), 32, msg=py.filepath)
self.assertIs(type(py.header_data), dict) self.assertIs(type(py.header_data), dict)
self.assertTrue(py in dyn2py.PythonFile.open_files) self.assertTrue(py in dyn2py.PythonFile.open_files)

View File

@@ -21,7 +21,7 @@ class TestPythonNode(unittest.TestCase):
self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0") self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0")
self.assertEqual(node.engine, "CPython3") self.assertEqual(node.engine, "CPython3")
self.assertEqual(node.checksum, "ec2c85a11ddbf8375da03f11272d427a") self.assertEqual(node.checksum, "92d46019bf538072db6bd1287267c147")
self.assertEqual(node.name, "Python Script") self.assertEqual(node.name, "Python Script")
self.assertEqual( self.assertEqual(
node.filename, "single_node_1c5d99792882409e97e132b3e9f814b0.py") node.filename, "single_node_1c5d99792882409e97e132b3e9f814b0.py")
@@ -36,7 +36,7 @@ class TestPythonNode(unittest.TestCase):
self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0") self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0")
self.assertEqual(node.engine, "CPython3") self.assertEqual(node.engine, "CPython3")
self.assertEqual(node.checksum, "8d9091d24788a6fdfa5e1e109298b50e") self.assertEqual(node.checksum, "64a73c52bd213b6f1b593697cdde789b")
def test_init_exception(self): def test_init_exception(self):