diff options
author | pommicket <pommicket@gmail.com> | 2025-09-23 01:24:06 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-09-23 01:24:06 -0400 |
commit | b5cdae01c4186f7b52ca1966267a8c735756a891 (patch) | |
tree | e0b693fa4fa47538903afb3856be52c2924dbd6a | |
parent | a042aded798fa33ce39a195a89e161aa6c80679f (diff) |
list parsing
-rw-r--r-- | examples/read_conf.py | 2 | ||||
-rw-r--r-- | pom_parser/__init__.py | 28 |
2 files changed, 29 insertions, 1 deletions
diff --git a/examples/read_conf.py b/examples/read_conf.py index 2c547eb..936157e 100644 --- a/examples/read_conf.py +++ b/examples/read_conf.py @@ -9,6 +9,6 @@ import pom_parser try: filename = 'examples/conf.pom' if len(sys.argv) < 2 else sys.argv[1] conf = pom_parser.load_path(filename) - print(conf.get_float('tab-size',17)) + print(conf.get_list('file-extensions.C',['a'])) except pom_parser.Error as e: print('Parse error:', str(e), sep = '\n') diff --git a/pom_parser/__init__.py b/pom_parser/__init__.py index 29bf843..c179158 100644 --- a/pom_parser/__init__.py +++ b/pom_parser/__init__.py @@ -92,6 +92,26 @@ class Item: return False return None + def _parse_list(self) -> list[str]: + chars = iter(self.value) + list_ = [] + entry: list[str] = [] + while (c := next(chars, '')): + if c == ',': + list_.append(''.join(entry).strip(' \t')) + entry = [] + elif c == '\\': + c = next(chars, '') + if c not in ',\\': + entry.append('\\') + entry.append(c) + else: + entry.append(c) + last_entry = ''.join(entry).strip(' \t') + if last_entry: + list_.append(last_entry) + return list_ + class Configuration: _items: dict[str, Item] def __repr__(self) -> str: @@ -156,6 +176,14 @@ class Configuration: raise item._error(f'Value {repr(item.value)} for {item.key} is invalid (want on/off/yes/no/true/false)') return boolv + def get_list(self, key: str, default: Optional[list[str]] = None) -> Optional[list[str]]: + item = self._items.get(key) + if item is None: + return None if default is None else default + item.read = True + return item._parse_list() + + def items(self) -> Iterable[Item]: import copy return map(copy.copy, self._items.values()) |