Add support for omitting VALUE

This will remove error reported in #853, but not its cause
This commit is contained in:
ZyX 2014-05-04 12:58:09 +04:00
parent 5d03fdebe1
commit be2fe98a21
5 changed files with 20 additions and 4 deletions

View File

@ -472,11 +472,17 @@ Powerline script has a number of options controlling powerline behavior. Here
example: ``{"K1": V1, "K2": V2}``) is recursively merged with the contents example: ``{"K1": V1, "K2": V2}``) is recursively merged with the contents
of the file. of the file.
If ``VALUE`` is omitted then corresponding key will be removed from the
configuration (if it was present).
``-t THEME_NAME.KEY.NESTED_KEY=VALUE`` or ``--theme_option=THEME_NAME.KEY.NESTED_KEY=VALUE`` ``-t THEME_NAME.KEY.NESTED_KEY=VALUE`` or ``--theme_option=THEME_NAME.KEY.NESTED_KEY=VALUE``
Overrides options from :file:`powerline/themes/{ext}/{THEME_NAME}.json`. Overrides options from :file:`powerline/themes/{ext}/{THEME_NAME}.json`.
``KEY.NESTED_KEY=VALUE`` is processed like described above, ``{ext}`` is the ``KEY.NESTED_KEY=VALUE`` is processed like described above, ``{ext}`` is the
first argument to powerline script. May be passed multiple times. first argument to powerline script. May be passed multiple times.
If ``VALUE`` is omitted then corresponding key will be removed from the
configuration (if it was present).
``-p PATH`` or ``--config_path=PATH`` ``-p PATH`` or ``--config_path=PATH``
Sets directory where configuration should be read from. If present, no Sets directory where configuration should be read from. If present, no
default locations are searched for configuration. No expansions are default locations are searched for configuration. No expansions are

View File

@ -3,6 +3,9 @@ from functools import wraps
import json import json
REMOVE_THIS_KEY = object()
def wraps_saveargs(wrapped): def wraps_saveargs(wrapped):
def dec(wrapper): def dec(wrapper):
r = wraps(wrapped)(wrapper) r = wraps(wrapped)(wrapper)
@ -17,6 +20,8 @@ def mergedicts(d1, d2):
for k in d2: for k in d2:
if k in d1 and type(d1[k]) is dict and type(d2[k]) is dict: if k in d1 and type(d1[k]) is dict and type(d2[k]) is dict:
mergedicts(d1[k], d2[k]) mergedicts(d1[k], d2[k])
elif d2[k] is REMOVE_THIS_KEY:
d1.pop(k, None)
else: else:
d1[k] = d2[k] d1[k] = d2[k]
@ -45,7 +50,9 @@ def keyvaluesplit(s):
idx = s.index('=') idx = s.index('=')
o = s[:idx] o = s[:idx]
rest = s[idx + 1:] rest = s[idx + 1:]
if rest[0] in '"{[0193456789' or rest in ('null', 'true', 'false'): if not rest:
val = REMOVE_THIS_KEY
elif rest[0] in '"{[0193456789' or rest in ('null', 'true', 'false'):
val = json.loads(s[idx + 1:]) val = json.loads(s[idx + 1:])
else: else:
val = rest val = rest

View File

@ -7,8 +7,7 @@ from powerline.lib import mergedicts, parsedotval
def mergeargs(argvalue): def mergeargs(argvalue):
if not argvalue: if not argvalue:
return None return None
argvalue = iter(argvalue) r = {}
r = dict([next(argvalue)])
for subval in argvalue: for subval in argvalue:
mergedicts(r, dict([subval])) mergedicts(r, dict([subval]))
return r return r

View File

@ -94,6 +94,8 @@ class TestParser(TestCase):
'renderer_arg': {'smth': {'abc': 'def'}}, 'renderer_arg': {'smth': {'abc': 'def'}},
}), }),
(['shell', '-R', 'arg=true'], {'ext': ['shell'], 'renderer_arg': {'arg': True}}), (['shell', '-R', 'arg=true'], {'ext': ['shell'], 'renderer_arg': {'arg': True}}),
(['shell', '-R', 'arg=true', '-R', 'arg='], {'ext': ['shell'], 'renderer_arg': {}}),
(['shell', '-R', 'arg='], {'ext': ['shell'], 'renderer_arg': {}}),
(['shell', '-t', 'default.segment_info={"hostname": {}}'], { (['shell', '-t', 'default.segment_info={"hostname": {}}'], {
'ext': ['shell'], 'ext': ['shell'],
'theme_option': { 'theme_option': {

View File

@ -1,7 +1,7 @@
# vim:fileencoding=utf-8:noet # vim:fileencoding=utf-8:noet
from __future__ import division from __future__ import division
from powerline.lib import mergedicts, add_divider_highlight_group from powerline.lib import mergedicts, add_divider_highlight_group, REMOVE_THIS_KEY
from powerline.lib.humanize_bytes import humanize_bytes from powerline.lib.humanize_bytes import humanize_bytes
from powerline.lib.vcs import guess from powerline.lib.vcs import guess
from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment
@ -354,6 +354,8 @@ class TestLib(TestCase):
self.assertEqual(d, {'abc': {'def': {'ghi': 'jkl'}}}) self.assertEqual(d, {'abc': {'def': {'ghi': 'jkl'}}})
mergedicts(d, {'abc': {'mno': 'pqr'}}) mergedicts(d, {'abc': {'mno': 'pqr'}})
self.assertEqual(d, {'abc': {'def': {'ghi': 'jkl'}, 'mno': 'pqr'}}) self.assertEqual(d, {'abc': {'def': {'ghi': 'jkl'}, 'mno': 'pqr'}})
mergedicts(d, {'abc': {'def': REMOVE_THIS_KEY}})
self.assertEqual(d, {'abc': {'mno': 'pqr'}})
def test_add_divider_highlight_group(self): def test_add_divider_highlight_group(self):
def decorated_function_name(**kwargs): def decorated_function_name(**kwargs):