utils.py 669 Bytes
Newer Older
1
2
import os

3
import ujson as json
4
5
6
7
8
9
10
11
12

try:
    from django.conf import settings

    DEBUG = settings.DEBUG
except:
    DEBUG = True


Jomaro Rodrigues's avatar
Jomaro Rodrigues committed
13
def invert_dict(d):
14
    return {v: k for k, v in d}
Jomaro Rodrigues's avatar
Jomaro Rodrigues committed
15
16


17
18
19
20
21
22
23
24
def build_path(path):
    if not os.path.exists(path):
        os.mkdir(path)


def ensure_path_exists(complete_path):
    parts = complete_path.split('/')

25
    for i in range(1,len(parts)):
26
27
28
29
30
31
32
33
34
35
36
        if not os.path.exists('/'.join(parts[:i+1])):
            os.mkdir('/'.join(parts[:i+1]))


def save_json(path, data):

    ensure_path_exists(os.path.dirname(path))

    params = {} if not DEBUG else {'indent': 4}

    with open(path, 'w') as f:
Bruno Meyer's avatar
Bruno Meyer committed
37
        json.dump(data, f, **params)
38
39