Skip to content

Commit 18f8cd1

Browse files
committed
support different newline encodings
1 parent 3fc8e87 commit 18f8cd1

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

lib/macro_storage_format_parser.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ def __init__(
1414
self.forceUUIDsZeroed = forceUUIDsZeroed
1515
self.util = Util(forceUUIDsZeroed)
1616

17+
def _split_macro_body_on_content_block(self, macro_body: str) -> tuple[str, str]:
18+
if "[content]\r\n" in macro_body:
19+
splitted = macro_body.split("[content]\r\n", 1)
20+
return (splitted[0], splitted[1])
21+
if "[content]\r" in macro_body:
22+
splitted = macro_body.split("[content]\r", 1)
23+
return (splitted[0], splitted[1])
24+
if "[content]\n\r" in macro_body:
25+
splitted = macro_body.split("[content]\n\r", 1)
26+
return (splitted[0], splitted[1])
27+
if "[content]\n" in macro_body:
28+
splitted = macro_body.split("[content]\n", 1)
29+
return (splitted[0], splitted[1])
30+
return ("ERROR SPLITTING AT [CONTENT]", "ERROR: INVALID NEWLINE")
31+
1732
def transform(self):
1833
builder = MacroStorageFormatBuilder(self.extensionkey, self.forceUUIDsZeroed)
1934
print("START > reading storage format file from: " + self.inputfile)
@@ -70,14 +85,16 @@ def transform(self):
7085
macro_code = child.text
7186
macro_config = ""
7287
elif macro_name == "advanced-codeblock-macro":
73-
if "[content]\n" not in child.text:
88+
if "[content]" not in child.text:
7489
print(
7590
">> ERROR: no [content] element in macro body found!"
7691
)
7792
macro_code = "ERROR - COULD NOT PARSE"
7893
macro_config = "ERROR - COULD NOT PARSE"
7994
else:
80-
config_and_code = child.text.split("[content]\n", 1)
95+
config_and_code = (
96+
self._split_macro_body_on_content_block(child.text)
97+
)
8198
macro_code = config_and_code[1]
8299
macro_config = config_and_code[0]
83100
elif macro_name == "advanced-remote-codeblock-macro":

lib/test_macro_storage_format_parser.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,57 @@ def test_transform___valid_remote(self):
6767
parser.transform()
6868
# THEN
6969
self.assertListEqual(list(open(output_file)), list(open(expected_output_file)))
70+
71+
def test_transform___newlines(self):
72+
self.maxDiff = None
73+
# INIT
74+
input_file = "./lib/input/ac-remote-test1.input.storage"
75+
output_file = "./lib/output/ac-remote-test1.output.storage"
76+
extension_key = (
77+
"9a81c16f-31e6-4be7-be6e-dfc872fe4155/35d2082a-431b-46b7-90da-75a526882f2f"
78+
)
79+
parser = MacroStorageFormatParser(input_file, output_file, extension_key, True)
80+
# ###########################################################################
81+
# CASE1: \r\n
82+
#
83+
# GIVEN
84+
macro_body1 = "foo\n[content]\r\nbar"
85+
# WHEN
86+
result1 = parser._split_macro_body_on_content_block(macro_body1)
87+
# THEN
88+
self.assertTrue(isinstance(result1, tuple))
89+
self.assertEqual(result1[0], "foo\n")
90+
self.assertEqual(result1[1], "bar")
91+
# ###########################################################################
92+
# CASE2: \r
93+
#
94+
# GIVEN
95+
macro_body2 = "foo\r[content]\rbar"
96+
# WHEN
97+
result2 = parser._split_macro_body_on_content_block(macro_body2)
98+
# THEN
99+
self.assertTrue(isinstance(result2, tuple))
100+
self.assertEqual(result2[0], "foo\r")
101+
self.assertEqual(result2[1], "bar")
102+
# ###########################################################################
103+
# CASE3: \r\n
104+
#
105+
# GIVEN
106+
macro_body3 = "foo\r[content]\r\nbar"
107+
# WHEN
108+
result3 = parser._split_macro_body_on_content_block(macro_body3)
109+
# THEN
110+
self.assertTrue(isinstance(result3, tuple))
111+
self.assertEqual(result3[0], "foo\r")
112+
self.assertEqual(result3[1], "bar")
113+
# ###########################################################################
114+
# CASE4: \n
115+
#
116+
# GIVEN
117+
macro_body4 = "foo\n[content]\nbar"
118+
# WHEN
119+
result4 = parser._split_macro_body_on_content_block(macro_body4)
120+
# THEN
121+
self.assertTrue(isinstance(result4, tuple))
122+
self.assertEqual(result4[0], "foo\n")
123+
self.assertEqual(result4[1], "bar")

0 commit comments

Comments
 (0)