Blame view

flaskr/content.py 1.41 KB
809d6747   Antoine Goutenoir   Structure the con...
1
from collections import namedtuple
2d25a68c   Antoine Goutenoir   Fix path relativity.
2
3
from os.path import abspath, dirname, join

f0903e16   Antoine Goutenoir   chore: move to py...
4
5
6
7
from yaml import safe_load as yaml_safe_load

# Move this to ENV, perhaps
base_url = "https://travel-footprint-calculator.irap.omp.eu"
2d25a68c   Antoine Goutenoir   Fix path relativity.
8
9
10
11
12
13
14
15
16

PROJECT_DIRECTORY = abspath(dirname(dirname(__file__)))


def get_path(relative_path):
    """
    Absolutize a relative path to this project's root directory.
    """
    return abspath(join(PROJECT_DIRECTORY, relative_path))
35a99ac7   Goutte   Move more text co...
17

bf3a1089   Antoine Goutenoir   Clean up.
18

262fe96c   Antoine Goutenoir   Use path relativity.
19
with open(get_path('content.yml'), 'r') as content_file:
809d6747   Antoine Goutenoir   Structure the con...
20
21
22
23
24
25
26
    content_dict = yaml_safe_load(content_file.read())


class Struct(object):
    def __new__(cls, data):
        if isinstance(data, dict):
            return namedtuple(
f0903e16   Antoine Goutenoir   chore: move to py...
27
                'Struct', data.keys()
809d6747   Antoine Goutenoir   Structure the con...
28
29
30
31
32
33
34
35
36
37
38
            )(
                *(Struct(val) for val in data.values())
            )
        elif isinstance(data, (tuple, list, set, frozenset)):
            return type(data)(Struct(_) for _ in data)
        else:
            return data


content = Struct(content_dict)

d8a2ae6b   Antoine Goutenoir   Provide configura...
39

809d6747   Antoine Goutenoir   Structure the con...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# For Python3?
# def dict2obj(d):
#     """
#     Convert a dict to an object
#
#     >>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
#     >>> obj = dict2obj(d)
#     >>> obj.b.c
#     2
#     >>> obj.d
#     ["hi", {'foo': "bar"}]
#     """
#     try:
#         d = dict(d)
#     except (TypeError, ValueError):
#         return d
#     obj = Object()
#     for k, v in d.iteritems():
#         obj.__dict__[k] = dict2obj(v)
#     return obj