Passed
Push — master ( f9f6ee...eaec94 )
by Fabio
01:14
created

benedict.utils.keypath_util.all_keys()   A

Complexity

Conditions 4

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 4
nop 1
1
# -*- coding: utf-8 -*-
2
3
from six import string_types
4
5
6
def all_keys(d):
7
    keys = []
8
    for key, val in d.items():
9
        if key not in keys:
10
            keys.append(key)
11
        if isinstance(val, dict):
12
            keys += all_keys(val)
13
    return keys
14
15
16
def check_keys(keys, separator):
17
    if separator:
18
        for key in keys:
19
            if key and isinstance(key, string_types):
20
                if separator in key:
21
                    raise ValueError(
22
                        'keys should not contain keypath separator '
23
                        '\'{}\'.'.format(separator))
24
25
26
def join_keys(keys, separator):
27
    return separator.join(keys)
28
29
30
def split_keys(key, separator):
31
    if separator:
32
        if isinstance(key, string_types):
33
            keypath = key
34
            if separator in keypath:
35
                keys = list(keypath.split(separator))
36
                return keys
37
            else:
38
                return [key]
39
        elif isinstance(key, (list, tuple, )):
40
            keys = []
41
            for key_item in key:
42
                keys += split_keys(key_item, separator)
43
            return keys
44
        else:
45
            return [key]
46
    else:
47
        return [key]
48