mirror of https://github.com/tLDP/python-tldp
and deal with the problem ofg None
This commit is contained in:
parent
b6edf39aeb
commit
6fe55dc912
|
@ -45,10 +45,20 @@ def dict_to_argv_longform(d):
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
def empty2none_values(d):
|
||||||
|
'''creates None values for values holding the empty string ("")
|
||||||
|
|
||||||
|
:param: d, a dictionary
|
||||||
|
'''
|
||||||
|
for k, v in d.items():
|
||||||
|
if v == '':
|
||||||
|
d[k] = None
|
||||||
|
return d
|
||||||
|
|
||||||
def convert_multivalues(d, multivaluesep=MULTIVALUESEP):
|
def convert_multivalues(d, multivaluesep=MULTIVALUESEP):
|
||||||
'''creates multivalued values in an argument dict()
|
'''creates multivalued values in an argument dict()
|
||||||
|
|
||||||
:param: d, should be a dictionary
|
:param: d, a dictionary
|
||||||
:param: separator; optional, desired string separator
|
:param: separator; optional, desired string separator
|
||||||
|
|
||||||
Returns: a dictionary where any values containing the separator
|
Returns: a dictionary where any values containing the separator
|
||||||
|
@ -211,15 +221,18 @@ def envdict_from_ns(tag, ns):
|
||||||
d = prepend_tag(tag, d, sep=ENVSEP)
|
d = prepend_tag(tag, d, sep=ENVSEP)
|
||||||
d = dict([(k.upper(), v) for k, v in d.items()])
|
d = dict([(k.upper(), v) for k, v in d.items()])
|
||||||
for k, v in sorted(d.items()):
|
for k, v in sorted(d.items()):
|
||||||
|
if v is None:
|
||||||
|
d[k] = ''
|
||||||
if isinstance(v, (list, tuple)):
|
if isinstance(v, (list, tuple)):
|
||||||
v = MULTIVALUESEP.join([str(x) for x in v])
|
v = MULTIVALUESEP.join([str(x) for x in v])
|
||||||
|
d[k] = v
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
||||||
def clilist_from_ns(ns):
|
def clilist_from_ns(ns):
|
||||||
cli = list()
|
cli = list()
|
||||||
d = dict_from_ns(ns)
|
d = dict_from_ns(ns)
|
||||||
for k, v in d.items():
|
for k, v in sorted(d.items()):
|
||||||
k = ''.join(('--', k.replace(NSSEP, CLISEP)))
|
k = ''.join(('--', k.replace(NSSEP, CLISEP)))
|
||||||
if isinstance(v, (list, tuple)):
|
if isinstance(v, (list, tuple)):
|
||||||
for val in v:
|
for val in v:
|
||||||
|
@ -259,6 +272,9 @@ def argv_from_env(args, tag, **kw):
|
||||||
listify_values = kw.get('convert_multivalues', convert_multivalues)
|
listify_values = kw.get('convert_multivalues', convert_multivalues)
|
||||||
if listify_values is not None:
|
if listify_values is not None:
|
||||||
d = listify_values(d)
|
d = listify_values(d)
|
||||||
|
fix_empty = kw.get('empty2none_values', empty2none_values)
|
||||||
|
if fix_empty is not None:
|
||||||
|
d = fix_empty(d, **kw)
|
||||||
d = strip_tag(tag, d)
|
d = strip_tag(tag, d)
|
||||||
d = dict_to_argv_longform(d)
|
d = dict_to_argv_longform(d)
|
||||||
return d
|
return d
|
||||||
|
@ -303,6 +319,9 @@ def argv_from_cfg(args, tag, **kw):
|
||||||
listify_values = kw.get('convert_multivalues', convert_multivalues)
|
listify_values = kw.get('convert_multivalues', convert_multivalues)
|
||||||
if listify_values is not None:
|
if listify_values is not None:
|
||||||
d = listify_values(d, **kw)
|
d = listify_values(d, **kw)
|
||||||
|
fix_empty = kw.get('empty2none_values', empty2none_values)
|
||||||
|
if fix_empty is not None:
|
||||||
|
d = fix_empty(d, **kw)
|
||||||
d = strip_tag(tag, d, **kw)
|
d = strip_tag(tag, d, **kw)
|
||||||
d = dict_to_argv_longform(d, **kw)
|
d = dict_to_argv_longform(d, **kw)
|
||||||
return d
|
return d
|
||||||
|
|
Loading…
Reference in New Issue