|
|
|
@ -13,7 +13,17 @@ KEYWORDS_LOWER_TRANSLATE = dict(zip(KEYWORDS_LOWER, KEYWORDS)) |
|
|
|
HOST_SPLITTER = re.compile( |
|
|
|
r"host(?:.(?!\bhost\b))+", flags=re.MULTILINE | re.DOTALL | re.IGNORECASE |
|
|
|
) |
|
|
|
KEY_VALUE = re.compile(r"(?P<key>\S+)(?:[ \t]+|[ \t]*=[ \t]*)(?P<values>.*)") |
|
|
|
KEY_VALUE = re.compile( |
|
|
|
r""" |
|
|
|
(?P<key>\S+) #anything that isn't a space is part of the key |
|
|
|
(?: |
|
|
|
[ \t]+ #seperated by whitespace |
|
|
|
| |
|
|
|
[ \t]*=[ \t]* #seperated by equals |
|
|
|
) |
|
|
|
(?P<values>.*)""", |
|
|
|
re.VERBOSE, |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
def CamelCase_to_snake(text): # noqa |
|
|
|
@ -21,13 +31,16 @@ def CamelCase_to_snake(text): # noqa |
|
|
|
|
|
|
|
|
|
|
|
def parse_config_text(text): |
|
|
|
# Step 0: strip leaing whitespaces and comments |
|
|
|
text_no_leading_whitespace = re.sub(r"^[ \t]+", "", text, flags=re.MULTILINE) |
|
|
|
text_no_comments = re.sub( |
|
|
|
r"^\#.*", "", text_no_leading_whitespace, flags=re.MULTILINE |
|
|
|
) |
|
|
|
# Step 1: split file by Host blocks |
|
|
|
hosts = HOST_SPLITTER.finditer(text_no_comments) |
|
|
|
for host in hosts: |
|
|
|
host_dict = {} |
|
|
|
# Step 2: go line by line looking for key=value |
|
|
|
for line in host.group(0).split("\n"): |
|
|
|
match = KEY_VALUE.match(line) |
|
|
|
if match: |
|
|
|
|