From 6784e0b752677c46105cfe97199adb8c8017cc2a Mon Sep 17 00:00:00 2001 From: Satoru SATOH Date: Sat, 20 Aug 2016 08:13:28 +1200 Subject: [PATCH] fix: improve regexp for shell vars in shellvars backend parser --- anyconfig/backend/shellvars.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/anyconfig/backend/shellvars.py b/anyconfig/backend/shellvars.py index 4c8a9c80..70b9d840 100644 --- a/anyconfig/backend/shellvars.py +++ b/anyconfig/backend/shellvars.py @@ -18,6 +18,7 @@ from __future__ import absolute_import import logging +import itertools import re import anyconfig.backend.base @@ -37,22 +38,25 @@ def _parseline(line): ('aaa', '') >>> _parseline("aaa=bbb") ('aaa', 'bbb') - >>> _parseline("aaa='bbb'") - ('aaa', 'bbb') - >>> _parseline('aaa="bbb"') - ('aaa', 'bbb') + >>> _parseline("aaa='bb b'") + ('aaa', 'bb b') + >>> _parseline('aaa="bb#b"') + ('aaa', 'bb#b') + >>> _parseline('aaa="bb\\"b"') + ('aaa', 'bb"b') >>> _parseline("aaa=bbb # ccc") ('aaa', 'bbb') """ - match = re.match(r"^(?:\s+)?(\S+)[:=]" - r"(?:(?:(?:(?:[\"'])(.+)(?:[\"']))|([^# ]+))(?:\s+)?#?)?", - line) + match = re.match(r"^\s*(\S+)=(?:(?:" + r"(?:\"(.*[^\\])\")|(?:'(.*[^\\])')|" + r"(?:([^\"'#\s]+)))?)\s*#*", line) if not match: LOGGER.warning("Invalid line found: %s", line) return (None, None) tpl = match.groups() - return (tpl[0], tpl[1] or (tpl[2] or '')) + vals = list(itertools.dropwhile(lambda x: x is None, tpl[1:])) + return (tpl[0], vals[0] if vals else '') def load(stream, to_container=dict):