Move some functions to powerline.lib.dict

Moved functions: `merge*` from `powerline.lib` and `mergeargs` from
`powerline.commands.main`.
This commit is contained in:
ZyX 2014-12-13 13:42:46 +03:00
parent 9ae76a9971
commit 7181c1d9b6
10 changed files with 66 additions and 60 deletions

View File

@ -11,7 +11,7 @@ from powerline.colorscheme import Colorscheme
from powerline.lib.config import ConfigLoader from powerline.lib.config import ConfigLoader
from powerline.lib.unicode import safe_unicode, FailedUnicode from powerline.lib.unicode import safe_unicode, FailedUnicode
from powerline.config import DEFAULT_SYSTEM_CONFIG_DIR from powerline.config import DEFAULT_SYSTEM_CONFIG_DIR
from powerline.lib import mergedicts from powerline.lib.dict import mergedicts
from powerline.lib.encoding import get_preferred_output_encoding from powerline.lib.encoding import get_preferred_output_encoding

View File

@ -5,7 +5,8 @@ from __future__ import (division, absolute_import, print_function)
import argparse import argparse
import sys import sys
from powerline.lib import mergedicts, parsedotval from powerline.lib import parsedotval
from powerline.lib.dict import mergeargs
from powerline.lib.encoding import get_preferred_arguments_encoding from powerline.lib.encoding import get_preferred_arguments_encoding
@ -19,15 +20,6 @@ else:
return s return s
def mergeargs(argvalue):
if not argvalue:
return None
r = {}
for subval in argvalue:
mergedicts(r, dict([subval]))
return r
def finish_args(args): def finish_args(args):
if args.config: if args.config:
args.config = mergeargs((parsedotval(v) for v in args.config)) args.config = mergeargs((parsedotval(v) for v in args.config))

View File

@ -2,7 +2,7 @@
from __future__ import (unicode_literals, division, absolute_import, print_function) from __future__ import (unicode_literals, division, absolute_import, print_function)
from powerline import Powerline from powerline import Powerline
from powerline.lib import mergedicts from powerline.lib.dict import mergedicts
from powerline.lib.unicode import string from powerline.lib.unicode import string

View File

@ -5,8 +5,7 @@ import json
from functools import wraps from functools import wraps
from powerline.lib.dict import REMOVE_THIS_KEY
REMOVE_THIS_KEY = object()
def wraps_saveargs(wrapped): def wraps_saveargs(wrapped):
@ -17,47 +16,6 @@ def wraps_saveargs(wrapped):
return dec return dec
def mergedicts(d1, d2):
'''Recursively merge two dictionaries
First dictionary is modified in-place.
'''
for k in d2:
if k in d1 and isinstance(d1[k], dict) and isinstance(d2[k], dict):
mergedicts(d1[k], d2[k])
elif d2[k] is REMOVE_THIS_KEY:
d1.pop(k, None)
else:
d1[k] = d2[k]
def mergedefaults(d1, d2):
'''Recursively merge two dictionaries, keeping existing values
First dictionary is modified in-place.
'''
for k in d2:
if k in d1 and isinstance(d1[k], dict) and isinstance(d2[k], dict):
mergedefaults(d1[k], d2[k])
else:
d1.setdefault(k, d2[k])
def mergedicts_copy(d1, d2):
'''Recursively merge two dictionaries.
Dictionaries are not modified. Copying happens only if necessary. Assumes
that first dictionary supports .copy() method.
'''
ret = d1.copy()
for k in d2:
if k in d1 and isinstance(d1[k], dict) and isinstance(d2[k], dict):
ret[k] = mergedicts_copy(d1[k], d2[k])
else:
ret[k] = d2[k]
return ret
def add_divider_highlight_group(highlight_group): def add_divider_highlight_group(highlight_group):
def dec(func): def dec(func):
@wraps_saveargs(func) @wraps_saveargs(func)

55
powerline/lib/dict.py Normal file
View File

@ -0,0 +1,55 @@
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)
REMOVE_THIS_KEY = object()
def mergeargs(argvalue):
if not argvalue:
return None
r = {}
for subval in argvalue:
mergedicts(r, dict([subval]))
return r
def mergedicts(d1, d2):
'''Recursively merge two dictionaries
First dictionary is modified in-place.
'''
for k in d2:
if k in d1 and isinstance(d1[k], dict) and isinstance(d2[k], dict):
mergedicts(d1[k], d2[k])
elif d2[k] is REMOVE_THIS_KEY:
d1.pop(k, None)
else:
d1[k] = d2[k]
def mergedefaults(d1, d2):
'''Recursively merge two dictionaries, keeping existing values
First dictionary is modified in-place.
'''
for k in d2:
if k in d1 and isinstance(d1[k], dict) and isinstance(d2[k], dict):
mergedefaults(d1[k], d2[k])
else:
d1.setdefault(k, d2[k])
def mergedicts_copy(d1, d2):
'''Recursively merge two dictionaries.
Dictionaries are not modified. Copying happens only if necessary. Assumes
that first dictionary supports .copy() method.
'''
ret = d1.copy()
for k in d2:
if k in d1 and isinstance(d1[k], dict) and isinstance(d2[k], dict):
ret[k] = mergedicts_copy(d1[k], d2[k])
else:
ret[k] = d2[k]
return ret

View File

@ -9,7 +9,7 @@ from functools import partial
from powerline import generate_config_finder, get_config_paths, load_config from powerline import generate_config_finder, get_config_paths, load_config
from powerline.segments.vim import vim_modes from powerline.segments.vim import vim_modes
from powerline.lib import mergedicts_copy from powerline.lib.dict import mergedicts_copy
from powerline.lib.config import ConfigLoader from powerline.lib.config import ConfigLoader
from powerline.lib.unicode import unicode from powerline.lib.unicode import unicode
from powerline.lint.markedjson import load from powerline.lint.markedjson import load

View File

@ -2,7 +2,7 @@
from __future__ import (unicode_literals, division, absolute_import, print_function) from __future__ import (unicode_literals, division, absolute_import, print_function)
from powerline import Powerline from powerline import Powerline
from powerline.lib import mergedicts from powerline.lib.dict import mergedicts
class ShellPowerline(Powerline): class ShellPowerline(Powerline):

View File

@ -10,7 +10,7 @@ import vim
from powerline.bindings.vim import vim_get_func, vim_getvar, get_vim_encoding from powerline.bindings.vim import vim_get_func, vim_getvar, get_vim_encoding
from powerline import Powerline, FailedUnicode from powerline import Powerline, FailedUnicode
from powerline.lib import mergedicts from powerline.lib.dict import mergedicts
def _override_from(config, override_varname): def _override_from(config, override_varname):

View File

@ -8,7 +8,7 @@ from subprocess import check_call
from operator import add from operator import add
from shutil import rmtree from shutil import rmtree
from powerline.lib import mergedicts_copy as mdc from powerline.lib.dict import mergedicts_copy as mdc
from powerline import Powerline from powerline import Powerline
from tests import TestCase from tests import TestCase

View File

@ -10,7 +10,8 @@ import shutil
from time import sleep from time import sleep
from subprocess import call, PIPE from subprocess import call, PIPE
from powerline.lib import mergedicts, add_divider_highlight_group, REMOVE_THIS_KEY from powerline.lib import add_divider_highlight_group
from powerline.lib.dict import mergedicts, 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, get_fallback_create_watcher from powerline.lib.vcs import guess, get_fallback_create_watcher
from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment