Completed
Push — master ( ea8775...b21323 )
by Fabio
01:31
created

benedict.utils.keypath_util.check_keys()   B

Complexity

Conditions 6

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nop 2
dl 0
loc 13
rs 8.6666
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
3
from benedict.utils import dict_util
4
5
from six import string_types
6
7
8
def check_keys(d, separator):
9
    """
10
    Check if dict keys contain keypath separator.
11
    """
12
    if not isinstance(d, dict) or not separator:
13
        return
14
15
    def check_key(parent, key, value):
16
        if key and isinstance(key, string_types) and separator in key:
17
            raise ValueError(
18
                'keys should not contain keypath separator '
19
                '\'{}\', found: \'{}\'.'.format(separator, key))
20
    dict_util.traverse(d, check_key)
21
22
23
def list_keys(keypath, separator):
24
    """
25
    List keys splitting a keypath using the given separator.
26
    """
27
    if isinstance(keypath, string_types):
28
        if separator:
29
            return keypath.split(separator)
30
    elif isinstance(keypath, (list, tuple, )):
31
        keys = []
32
        for key in keypath:
33
            keys += list_keys(key, separator)
34
        return keys
35
    return [keypath]
36