recursive_get()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
1
"""Some helper functions."""
2
3
import couchdb
4
from . import config
5
6
connection = couchdb.Server(config.COUCHDB_URL)
7
8
9
def get_download_token(document: couchdb.Document):
10
    """Get the download token of the specified document.
11
12
    :returns: The download token or None if it isn't defined.
13
14
    """
15
    return recursive_get(document, config.DOWNLOAD_TOKEN_KEY_NAME)
16
17
18
def recursive_get(dict_, key):
19
    """Get nested dictionary keys.
20
21
    >>> recursive_get({"a": {"b": {"c": "d"}}}, "a.b.c")
22
    "d"
23
24
    Returns None if there are type conflicts.
25
    """
26
    keys = key.split('.')
27
    last = keys.pop(-1)
28
29
    dict_ = dict_.copy()
30
    for key in keys:
31
        value = dict_.get(key)
32
        if not isinstance(value, dict):
33
            return
34
        dict_ = value
35
    return dict_.get(last)
36