Skip to content

Commit

Permalink
fix: improve regexp for shell vars in shellvars backend parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ssato committed Aug 19, 2016
1 parent da5fd12 commit 6784e0b
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions anyconfig/backend/shellvars.py
Expand Up @@ -18,6 +18,7 @@
from __future__ import absolute_import

import logging
import itertools
import re

import anyconfig.backend.base
Expand All @@ -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):
Expand Down

0 comments on commit 6784e0b

Please sign in to comment.