Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_path_paths_empty_key_allowed():
tdict = {
"Empty": {
"": {
"Key": ""
}
}
}
parts=[]
dpath.options.ALLOW_EMPTY_STRING_KEYS=True
for x in dpath.path.paths(tdict, dirs=False, leaves=True):
path = x
for x in path[:-1]:
parts.append(x[0])
dpath.options.ALLOW_EMPTY_STRING_KEYS=False
assert("/".join(parts) == "Empty//Key")
def test_path_paths_empty_key_allowed():
tdict = {
"Empty": {
"": {
"Key": ""
}
}
}
parts=[]
dpath.options.ALLOW_EMPTY_STRING_KEYS=True
for x in dpath.path.paths(tdict, dirs=False, leaves=True):
path = x
for x in path[:-1]:
parts.append(x[0])
dpath.options.ALLOW_EMPTY_STRING_KEYS=False
assert("/".join(parts) == "Empty//Key")
def setup():
# Allow empty strings in segments.
options.ALLOW_EMPTY_STRING_KEYS = True
def teardown():
# Revert back to default.
options.ALLOW_EMPTY_STRING_KEYS = False
path -- A list of keys representing the path.
skip -- Skip special keys beginning with '+'.
"""
if isinstance(obj, MutableMapping):
# Python 3 support
if PY3:
iteritems = obj.items()
string_class = str
else: # Default to PY2
iteritems = obj.iteritems()
string_class = basestring
for (k, v) in iteritems:
if issubclass(k.__class__, (string_class)):
if (not k) and (not dpath.options.ALLOW_EMPTY_STRING_KEYS):
raise dpath.exceptions.InvalidKeyName("Empty string keys not allowed without "
"dpath.options.ALLOW_EMPTY_STRING_KEYS=True")
elif (skip and k and k[0] == '+'):
continue
newpath = path + [[k, v.__class__]]
validate(newpath)
if dirs:
yield newpath
for child in paths(v, dirs, leaves, newpath, skip):
yield child
elif isinstance(obj, MutableSequence):
for (i, v) in enumerate(obj):
newpath = path + [[i, v.__class__]]
if dirs:
yield newpath
for child in paths(obj[i], dirs, leaves, newpath, skip):
'''
Yield all valid (segments, value) pairs (from a breadth-first
search, right-to-left on sequences).
walk(obj) -> (generator -> (segments, value))
'''
if not leaf(obj):
for k, v in kvs(obj):
length = None
try:
length = len(k)
except:
pass
if length is not None and length == 0 and not options.ALLOW_EMPTY_STRING_KEYS:
raise InvalidKeyName("Empty string keys not allowed without "
"dpath.options.ALLOW_EMPTY_STRING_KEYS=True: "
"{}".format(location + (k,)))
yield ((location + (k,)), v)
for k, v in kvs(obj):
for found in walk(v, location + (k,)):
yield found
def merger(dst, src, _segments=()):
for key, found in dpath.segments.kvs(src):
# Our current path in the source.
segments = _segments + (key,)
if len(key) == 0 and not options.ALLOW_EMPTY_STRING_KEYS:
raise InvalidKeyName("Empty string keys not allowed without "
"dpath.options.ALLOW_EMPTY_STRING_KEYS=True: "
"{}".format(segments))
# Validate src and dst types match.
if flags & MERGE_TYPESAFE:
if dpath.segments.has(dst, segments):
target = dpath.segments.get(dst, segments)
tt = type(target)
ft = type(found)
if tt != ft:
path = separator.join(segments)
raise TypeError("Cannot merge objects of type"
"{0} and {1} at {2}"
"".format(tt, ft, path))