Parse every configuration entry without crossing assignment lines
This commit is contained in:
parent
034108e636
commit
eba404bc96
2 changed files with 52 additions and 22 deletions
|
|
@ -4,7 +4,7 @@ from typing import Dict, ItemsView, Iterator, KeysView, Mapping, Union, ValuesVi
|
|||
|
||||
from ..logger.get_logger import get_logger
|
||||
from .parse_error import ParseError
|
||||
from .pattern import pattern
|
||||
from .pattern import ignored_line, pattern
|
||||
|
||||
logger = get_logger("ConfigFile")
|
||||
|
||||
|
|
@ -89,14 +89,25 @@ class ConfigFile(Mapping[str, str]):
|
|||
with open(self._path, encoding="utf-8") as f:
|
||||
lines: str = f.read()
|
||||
|
||||
matches = pattern.findall(lines)
|
||||
for key, *values in matches:
|
||||
try:
|
||||
value = next(v for v in values if v)
|
||||
except StopIteration:
|
||||
raise ParseError(f"""Cannot parse config file ({
|
||||
self._path.absolute()
|
||||
}), error at key `{key}`""")
|
||||
position = 0
|
||||
while position < len(lines):
|
||||
ignored = ignored_line.match(lines, position)
|
||||
if ignored is not None and ignored.end() > position:
|
||||
position = ignored.end()
|
||||
continue
|
||||
match = pattern.match(lines, position)
|
||||
if match is None:
|
||||
line = lines.count("\n", 0, position) + 1
|
||||
raise ParseError(
|
||||
f"Cannot parse config file ({self._path.absolute()}) at line {line}"
|
||||
)
|
||||
position = match.end()
|
||||
key = match.group("key")
|
||||
value = next(
|
||||
match.group(name)
|
||||
for name in ("double", "single", "backtick", "bare")
|
||||
if match.group(name) is not None
|
||||
)
|
||||
|
||||
already_exists = key in self._key_values
|
||||
if already_exists and not value.startswith(
|
||||
|
|
|
|||
|
|
@ -2,19 +2,16 @@ import re
|
|||
|
||||
pattern = re.compile(
|
||||
r"""
|
||||
(?:^|\n) # new key-value pairs must start on a new line
|
||||
\s* # leading whitespace is allowed
|
||||
(?!\#) # the key cannot start with a `#` symbol
|
||||
(\w+?) # then comes the key
|
||||
\s*=\s* # the key and value are separated by an equal sign
|
||||
(?: # then comes the value
|
||||
"([^"]*)" # the value can be surrounded by quotes: "value"
|
||||
| '([^']*)' # the value can be surrounded by quotes: 'value'
|
||||
| `([^`]*)` # the value can be surrounded by quotes: `value`
|
||||
| ([^#\n]*?) # or it is bare, in that case, the trailing whitespace is ignored
|
||||
[\t ]*(?P<key>\w+)[\t ]*=[\t ]*
|
||||
(?:
|
||||
"(?P<double>[^"]*)"
|
||||
| '(?P<single>[^']*)'
|
||||
| `(?P<backtick>[^`]*)`
|
||||
| (?!["'`])(?P<bare>[^\#\r\n]*?)
|
||||
)
|
||||
\s*(?:\#.*)? # comments can be added with the `#` symbol
|
||||
(?=\n|$) # a key-value pairs are separated by new lines
|
||||
[\t ]*(?:\#[^\r\n]*)?(?:\r?\n|$)
|
||||
""",
|
||||
flags=re.UNICODE | re.VERBOSE,
|
||||
)
|
||||
|
||||
ignored_line = re.compile(r"[\t ]*(?:\#[^\r\n]*)?(?:\r?\n|$)")
|
||||
|
|
|
|||
Loading…
Reference in a new issue