From b02412ef656c58191f2713ec39bd458f29e9a086 Mon Sep 17 00:00:00 2001 From: Giles Knap Date: Tue, 20 Feb 2024 07:18:53 +0000 Subject: [PATCH] Fix regex for parsing records Now accepts records with these features: - no quotes around the record name - no whitespace between record() and { ... } Also adds raise of RecordError if the record does not parse. --- src/pvi/_convert/_template_convert.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/pvi/_convert/_template_convert.py b/src/pvi/_convert/_template_convert.py index f13567e5..4bb5ceca 100644 --- a/src/pvi/_convert/_template_convert.py +++ b/src/pvi/_convert/_template_convert.py @@ -88,8 +88,11 @@ def _parse_record(self, record_str: str) -> Tuple: # info(autosaveFields, "VAL") # https://regex101.com/r/MZz1oa - record_parser = re.compile(r'record\((\w+),\s*"([^"]+)"\)\s{([^}]*)}') - return re.findall(record_parser, record_str)[0] + record_parser = re.compile(r'record\((\w+),\s*"?([^"]+)"?\)\s*{([^}]*)}') + matches = re.findall(record_parser, record_str) + if len(matches) != 1: + raise RecordError(f"Parse failed on record: {record_str}") + return matches[0] def _extract_fields(self, fields_str: str) -> List[Tuple[str, str]]: # extract two groups from a field e.g.