Exemple #1
0
def __safe_path__(path, separator):
    """
    Given a path and separator, return a list of path components. If path
    is already a list, return it.

    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 issubclass(path.__class__, (MutableSequence)):
        return path
    path = path.lstrip(separator).split(separator)
    validated = []
    for elem in path:
        if not elem:
            if not dpath.options.ALLOW_EMPTY_STRING_KEYS:
                raise dpath.exceptions.InvalidKeyName(
                    "Empty string keys not allowed without "
                    "dpath.options.ALLOW_EMPTY_STRING_KEYS=True")
            strkey = elem
        else:
            key = elem[0]
            strkey = str(key)
            if (separator and (separator in strkey)):
                raise dpath.exceptions.InvalidKeyName(
                    "{0} at {1} contains the separator {2}"
                    "".format(strkey, separator.join(validated), separator))
        validated.append(strkey)
    return path
Exemple #2
0
def new(obj, path, value, separator="/"):
    """
    Set the element at the terminus of path to value, and create
    it if it does not exist (as opposed to 'set' that can only
    change existing keys).

    path will NOT be treated like a glob. If it has globbing
    characters in it, they will become part of the resulting
    keys
    """
    pathobj = dpath.path.path_types(obj, path.lstrip(separator).split(separator))
    return dpath.path.set(obj, pathobj, value, create_missing=True)
Exemple #3
0
def __safe_path__(path, separator):
    """
    Given a path and separator, return a list of path components. If path
    is already a list, return it.

    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 issubclass(path.__class__, (list)):
        return path
    path = path.lstrip(separator).split(separator)
    validated = []
    for elem in path:
        key = elem[0]
        strkey = str(key)
        if (separator and (separator in strkey)):
            raise dpath.exceptions.InvalidKeyName(
                "{0} at {1} contains the separator {2}"
                "".format(strkey, separator.join(validated), separator))
        validated.append(strkey)
    return path
Exemple #4
0
def __safe_path__(path, separator):
    """
    Given a path and separator, return a list of path components. If path
    is already a list, return it.

    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 issubclass(path.__class__, (MutableSequence)):
        return path
    path = path.lstrip(separator).split(separator)
    validated = []
    for elem in path:
        key = elem[0]
        strkey = str(key)
        if separator and (separator in strkey):
            raise dpath.exceptions.InvalidKeyName(
                "{0} at {1} contains the separator {2}" "".format(strkey, separator.join(validated), separator)
            )
        validated.append(strkey)
    return path