mirror of
https://github.com/httpie/cli.git
synced 2025-02-03 13:01:58 +02:00
Fixed escaping for long separators.
This commit is contained in:
parent
16df8848e8
commit
90af1f7422
@ -36,14 +36,24 @@ class KeyValueType(object):
|
|||||||
"""A type used with `argparse`."""
|
"""A type used with `argparse`."""
|
||||||
def __init__(self, *separators):
|
def __init__(self, *separators):
|
||||||
self.separators = separators
|
self.separators = separators
|
||||||
|
self.escapes = ['\\\\' + sep for sep in separators]
|
||||||
|
|
||||||
def __call__(self, string):
|
def __call__(self, string):
|
||||||
found = {}
|
found = {}
|
||||||
|
found_escapes = []
|
||||||
|
for esc in self.escapes:
|
||||||
|
found_escapes += [m.span() for m in re.finditer(esc, string)]
|
||||||
for sep in self.separators:
|
for sep in self.separators:
|
||||||
regex = '[^\\\\]' + sep
|
matches = re.finditer(sep, string)
|
||||||
match = re.search(regex, string)
|
for match in matches:
|
||||||
if match:
|
start, end = match.span()
|
||||||
found[match.start() + 1] = sep
|
inside_escape = False
|
||||||
|
for estart, eend in found_escapes:
|
||||||
|
if start >= estart and end <= eend:
|
||||||
|
inside_escape = True
|
||||||
|
break
|
||||||
|
if not inside_escape:
|
||||||
|
found[start] = sep
|
||||||
|
|
||||||
if not found:
|
if not found:
|
||||||
#noinspection PyExceptionInherit
|
#noinspection PyExceptionInherit
|
||||||
|
@ -76,6 +76,13 @@ class TestItemParsing(BaseTest):
|
|||||||
})
|
})
|
||||||
self.assertIn('bar@baz', files)
|
self.assertIn('bar@baz', files)
|
||||||
|
|
||||||
|
def test_escape_longsep(self):
|
||||||
|
headers, data, files = cli.parse_items([
|
||||||
|
self.kv('bob\\:==foo'),
|
||||||
|
])
|
||||||
|
self.assertDictEqual(data, {
|
||||||
|
'bob:=': 'foo',
|
||||||
|
})
|
||||||
|
|
||||||
def test_valid_items(self):
|
def test_valid_items(self):
|
||||||
headers, data, files = cli.parse_items([
|
headers, data, files = cli.parse_items([
|
||||||
|
Loading…
x
Reference in New Issue
Block a user