Fix zpython overrides

This commit is contained in:
ZyX 2014-12-13 14:11:38 +03:00
parent 7181c1d9b6
commit 029dc92da0
2 changed files with 29 additions and 8 deletions

View File

@ -12,6 +12,7 @@ from powerline.lib import parsedotval
from powerline.lib.unicode import unicode from powerline.lib.unicode import unicode
from powerline.lib.encoding import (get_preferred_output_encoding, from powerline.lib.encoding import (get_preferred_output_encoding,
get_preferred_environment_encoding) get_preferred_environment_encoding)
from powerline.lib.dict import mergeargs
used_powerlines = WeakValueDictionary() used_powerlines = WeakValueDictionary()
@ -24,7 +25,7 @@ def shutdown():
def get_var_config(var): def get_var_config(var):
try: try:
return [parsedotval(i) for i in zsh.getvalue(var).items()] return mergeargs([parsedotval(i) for i in zsh.getvalue(var).items()])
except: except:
return None return None

View File

@ -32,6 +32,31 @@ def add_divider_highlight_group(highlight_group):
return dec 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): def keyvaluesplit(s):
if '=' not in s: if '=' not in s:
raise TypeError('Option must look like option=json_value') raise TypeError('Option must look like option=json_value')
@ -39,19 +64,14 @@ def keyvaluesplit(s):
raise ValueError('Option names must not start with `_\'') raise ValueError('Option names must not start with `_\'')
idx = s.index('=') idx = s.index('=')
o = s[:idx] o = s[:idx]
rest = s[idx + 1:] val = parse_value(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
return (o, val) return (o, val)
def parsedotval(s): def parsedotval(s):
if type(s) is tuple: if type(s) is tuple:
o, val = s o, val = s
val = parse_value(val)
else: else:
o, val = keyvaluesplit(s) o, val = keyvaluesplit(s)