Skip to content

Commit 7dc06e2

Browse files
committed
Dev: corosync_config_format: Enable to parse comments
1 parent cad0f31 commit 7dc06e2

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

crmsh/corosync.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
COROSYNC_TOKEN_DEFAULT = 1000 # in ms units
2525
COROSYNC_CONF_TEMPLATE = """
26+
# Generated by crmsh
27+
# For more details please see corosync.conf.5 man page
2628
totem {
2729
version: 2
2830
}

crmsh/corosync_config_format.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99

1010
logger = logging.getLogger(__name__)
11+
COMMENT_PREFIX = '__comment'
1112

1213

1314
class Parser:
1415
"""SAX-style parser for the configuration of corosync"""
1516

16-
_SPLIT_RE = re.compile('\\s*({|:|})\\s*')
17+
_SPLIT_RE = re.compile('\\s*(#|{|:|})\\s*')
1718

1819
def __init__(self, ifile):
1920
"""Parse data form a file-like object."""
@@ -34,14 +35,15 @@ def _parse(self, ifile):
3435
line = line.strip()
3536
if not line:
3637
continue
37-
if line[0] == '#':
38-
continue
3938
try:
4039
tokens = self._tokenize(line)
4140
except ValueError as e:
4241
raise MalformedLineException(lineno, line)
4342
logger.debug('tokens: %s', tokens)
44-
if tokens[1] == '{':
43+
if tokens[1] == '#':
44+
fake_secion_name = f'{COMMENT_PREFIX}_{lineno}'
45+
self.on_key_value(fake_secion_name, tokens[2])
46+
elif tokens[1] == '{':
4547
if not tokens[0] or tokens[2]:
4648
raise MalformedLineException(lineno, line)
4749
self.__section_stack.append(tokens[0])
@@ -237,11 +239,14 @@ def on_dict(self, node):
237239
self.on_list(value)
238240
del self._path_stack[-1]
239241
case _:
240-
self.__write_indent(len(self._path_stack))
241-
self._ofile.write(key)
242-
self._ofile.write(': ')
243-
self.on_value(value)
244-
self._ofile.write('\n')
242+
if key.startswith(COMMENT_PREFIX):
243+
self._ofile.write(f'# {value}\n')
244+
else:
245+
self.__write_indent(len(self._path_stack))
246+
self._ofile.write(key)
247+
self._ofile.write(': ')
248+
self.on_value(value)
249+
self._ofile.write('\n')
245250

246251
def on_list(self, node):
247252
key = self._path_stack[-1]

0 commit comments

Comments
 (0)