Skip to content

Commit 061bd3b

Browse files
committed
fixes #2 and adds a test for line continuation behavior
1 parent 572685c commit 061bd3b

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

fleet/v1/objects/unit.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def _set_options_from_file(self, file_handle):
146146
"""
147147

148148
# TODO: Find a library to handle this unit file parsing
149-
# Can't use configparser, it doesn't handle multiple entries for the same key in the same sectoin
149+
# Can't use configparser, it doesn't handle multiple entries for the same key in the same section
150150
# This is terribly naive
151151

152152
# build our output here
@@ -161,6 +161,7 @@ def _set_options_from_file(self, file_handle):
161161
line_number += 1
162162

163163
# clear any extra white space
164+
orig_line = line
164165
line = line.strip()
165166

166167
# ignore comments, and blank lines
@@ -183,8 +184,25 @@ def _set_options_from_file(self, file_handle):
183184
))
184185

185186
# Attempt to parse a line inside a section
186-
# Lines should look like: name=value
187+
# Lines should look like: name=value \
188+
# continuation
189+
continuation = False
187190
try:
191+
# if the previous value ends with \ then we are a continuation
192+
# so remove the \, and set the flag so we'll append to this below
193+
if options[-1]['value'].endswith('\\'):
194+
options[-1]['value'] = options[-1]['value'][:-1]
195+
continuation = True
196+
except IndexError:
197+
pass
198+
199+
try:
200+
# if we are a continuation, then just append our value to the previous line
201+
if continuation:
202+
options[-1]['value'] += orig_line
203+
continue
204+
205+
# else we are a normal line, so spit and get our name / value
188206
name, value = line.split('=', 1)
189207
options.append({
190208
'section': section,
@@ -194,7 +212,7 @@ def _set_options_from_file(self, file_handle):
194212
except ValueError:
195213
raise ValueError(
196214
'Unable to parse unit file; '
197-
'Malformed line in section {0}: {1} (line: {2}'.format(
215+
'Malformed line in section {0}: {1} (line: {2})'.format(
198216
section,
199217
line,
200218
line_number

fleet/v1/tests/test_unit.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ def test2():
101101
self.assertRaises(ValueError, test)
102102
self.assertRaises(ValueError, test2)
103103

104+
def test_from_string_continuation_good(self):
105+
"""When parsing unit files, line continuation with a trailing backslash works"""
106+
unit = Unit(from_string="[Section]\nThisLine=The start of \\\nsomething very\\\n long and boring\n")
107+
108+
# test options (this should match the string above)
109+
test_options = [
110+
{
111+
'section': 'Section',
112+
'name': 'ThisLine',
113+
'value': 'The start of something very long and boring'
114+
}
115+
]
116+
117+
assert unit.options == test_options
118+
104119
def test_options_no_desired_state(self):
105120
"""Setting options explicitly works"""
106121
test_options = [{'section': 'Service', 'name': 'ExecStart', 'value': '/usr/bin/sleep 1d'}]

0 commit comments

Comments
 (0)