diff --git a/dyn2py/files.py b/dyn2py/files.py index 8e107a0..6c7cfd5 100644 --- a/dyn2py/files.py +++ b/dyn2py/files.py @@ -2,7 +2,6 @@ from __future__ import annotations import simplejson as json import hashlib import pathlib -import textwrap import logging import os from datetime import datetime @@ -399,10 +398,10 @@ class PythonFile(File): # Do not read from disk: super().__init__(filepath, read_from_disk=False) - header_notice = """\ - This file was generated with dyn2py from a Dynamo graph. - Do not edit this section, if you want to update the Dynamo graph!\ - """ + header_notice = os.linesep.join([ + "This file was generated with dyn2py from a Dynamo graph.", + "Do not edit this section, if you want to update the Dynamo graph!" + ]) # Double escape path: dyn_path_string = str(dynamo_file.realpath) @@ -420,14 +419,14 @@ class PythonFile(File): "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]) header_wrapper = '"""' - self.text = "\r\n".join([ + self.text = os.linesep.join([ header_wrapper, HEADER_SEPARATOR, - textwrap.dedent(header_notice), + header_notice, HEADER_SEPARATOR, header_string, HEADER_SEPARATOR, @@ -462,7 +461,8 @@ class PythonFile(File): logging.info(f"Reading file: {self.filepath}") 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.header_data = {} @@ -470,7 +470,6 @@ class PythonFile(File): code_start_line = 0 for i, line in enumerate(python_lines): - line = line.strip() logging.debug(f"Reading line: {line}") # Skip the first lines: @@ -490,7 +489,7 @@ class PythonFile(File): raise PythonFileException("Error reading header!") self.header_data[line[0:sl]] = line[sl+1:] - self.code = "".join(python_lines[code_start_line:]) + self.code = os.linesep.join(python_lines[code_start_line:]) self.open_files.add(self) logging.debug(f"Header data from python file: {self.header_data}") diff --git a/tests/test_PythonFile.py b/tests/test_PythonFile.py index e1ff28e..4cd131b 100644 --- a/tests/test_PythonFile.py +++ b/tests/test_PythonFile.py @@ -2,6 +2,7 @@ import unittest import dyn2py import shutil from time import sleep +import os from tests.support import * @@ -11,8 +12,7 @@ class TestPythonFile(unittest.TestCase): def test_init(self): extract_single_node_dyn() - py1 = dyn2py.PythonFile( - f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py") + py1 = dyn2py.PythonFile(f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py") dyn2py.DynamoFile.open_files.clear() dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn") @@ -22,8 +22,8 @@ class TestPythonFile(unittest.TestCase): for py in [py1, py2]: - self.assertEqual(len(py.code.split("\n")), 17) - self.assertEqual(len(py.text.split("\r\n")), 31) + self.assertEqual(len(py.code.split(os.linesep)), 17) + 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)