Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@raises(dpath.exceptions.InvalidKeyName)
def test_path_paths_empty_key_disallowed():
tdict = {
"Empty": {
"": {
"Key": ""
}
}
}
for x in dpath.path.paths(tdict):
pass
Note that a string path with the separator at index[0] will have the
separator stripped off. If you pass a list path, the separator is
ignored, and is assumed to be part of each key glob. It will not be
stripped.
'''
if not dpath.segments.leaf(path):
segments = path
else:
segments = path.lstrip(separator).split(separator)
# FIXME: This check was in the old internal library, but I can't
# see a way it could fail...
for i, segment in enumerate(segments):
if (separator and (separator in segment)):
raise InvalidKeyName("{} at {}[{}] contains the separator '{}'"
"".format(segment, segments, i, separator))
# Attempt to convert integer segments into actual integers.
final = []
for segment in segments:
try:
final.append(int(segment))
except:
final.append(segment)
segments = final
return segments
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 validate(path, regex=None):
"""
Validate that all the keys in the given list of path components are valid, given that they do not contain the separator, and match any optional regex given.
"""
validated = []
for elem in path:
key = elem[0]
strkey = str(key)
if (regex and (not regex.findall(strkey))):
raise dpath.exceptions.InvalidKeyName("{} at {} does not match the expression {}"
"".format(strkey,
validated,
regex.pattern))
validated.append(strkey)
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))
# Path not present in destination, create it.