diff --git a/powerline/bindings/zsh/__init__.py b/powerline/bindings/zsh/__init__.py index d982c5b6..a5cf05e6 100644 --- a/powerline/bindings/zsh/__init__.py +++ b/powerline/bindings/zsh/__init__.py @@ -12,6 +12,7 @@ from powerline.lib import parsedotval from powerline.lib.unicode import unicode from powerline.lib.encoding import (get_preferred_output_encoding, get_preferred_environment_encoding) +from powerline.lib.dict import mergeargs used_powerlines = WeakValueDictionary() @@ -24,7 +25,7 @@ def shutdown(): def get_var_config(var): try: - return [parsedotval(i) for i in zsh.getvalue(var).items()] + return mergeargs([parsedotval(i) for i in zsh.getvalue(var).items()]) except: return None diff --git a/powerline/lib/__init__.py b/powerline/lib/__init__.py index 80b378ef..af3b9913 100644 --- a/powerline/lib/__init__.py +++ b/powerline/lib/__init__.py @@ -32,6 +32,31 @@ def add_divider_highlight_group(highlight_group): return dec +def parse_value(s): + '''Convert string to Python object + + Rules: + + * Empty string means that corresponding key should be removed from the + dictionary. + * Strings that start with a minus, digit or with some character that starts + JSON collection or string object are parsed as JSON. + * JSON special values ``null``, ``true``, ``false`` (case matters) are + parsed as JSON. + * All other values are considered to be raw strings. + + :param str s: Parsed string. + + :return: Python object. + ''' + if not s: + return REMOVE_THIS_KEY + elif s[0] in '"{[0193456789-' or s in ('null', 'true', 'false'): + return json.loads(s) + else: + return s + + def keyvaluesplit(s): if '=' not in s: raise TypeError('Option must look like option=json_value') @@ -39,19 +64,14 @@ def keyvaluesplit(s): raise ValueError('Option names must not start with `_\'') idx = s.index('=') o = s[:idx] - rest = s[idx + 1:] - if not rest: - val = REMOVE_THIS_KEY - elif rest[0] in '"{[0193456789' or rest in ('null', 'true', 'false'): - val = json.loads(s[idx + 1:]) - else: - val = rest + val = parse_value(s[idx + 1:]) return (o, val) def parsedotval(s): if type(s) is tuple: o, val = s + val = parse_value(val) else: o, val = keyvaluesplit(s)