Blame view

flaskr/core.py 1.56 KB
e0a7b516   Goutte   Try out ABC.
1
import abc
5e995dfe   Antoine Goutenoir   Refactor how we c...
2
import importlib
b8ad872b   Antoine Goutenoir   Prepare path rela...
3
from os.path import isfile, join, abspath, dirname
984b691d   Goutte   Add the core libr...
4
5
6
from datetime import datetime
from uuid import uuid4

7bc7b9ae   Antoine Goutenoir   More.
7
from .content import get_path, content
b8ad872b   Antoine Goutenoir   Prepare path rela...
8

795f09e1   Antoine Goutenoir   Clarify "core" fi...
9
10
11
# This is not really a core. Look in __init__.py
# Using this as misc bag

b8ad872b   Antoine Goutenoir   Prepare path rela...
12
13

hit_count_path = get_path("VISITS")
8c465c94   Antoine Goutenoir   Implement methods...
14
15


984b691d   Goutte   Add the core libr...
16
17
18
19
def generate_unique_id():
    """
    :return: a unique identifier that can be sorted chronologically.
    """
2bdcd05b   Antoine Goutenoir   Set unique id as ...
20
21
22
    return u"%s" % (
            datetime.now().strftime('%Y-%m-%d_%H:%M:%S_') + str(uuid4())[0:4]
    )
e0a7b516   Goutte   Try out ABC.
23
24


5e995dfe   Antoine Goutenoir   Refactor how we c...
25
26
27
28
29
30
31
32
def get_emission_models():
    emission_models_confs = content.models
    emission_models = []

    for model_conf in emission_models_confs:
        model_file = model_conf.file
        the_module = importlib.import_module("flaskr.laws.%s" % model_file)

a03065f3   Antoine Goutenoir   Squeeze in anothe...
33
        model = the_module.EmissionModel(model_conf, content.shared_config)
5e995dfe   Antoine Goutenoir   Refactor how we c...
34
35
36
37
38
39
40
41
        # model.configure(extra_model_conf)

        emission_models.append(model)

    return emission_models


models = get_emission_models()
e0a7b516   Goutte   Try out ABC.
42
43


8c465c94   Antoine Goutenoir   Implement methods...
44
45
46
47
48
49
50
51
52
53
54
def get_hit_counter():
    hit_count = 1
    if isfile(hit_count_path):
        with open(hit_count_path) as hcf:
            hit_count = int(hcf.read().strip())

    return hit_count


def increment_hit_counter():
    if isfile(hit_count_path):
a03065f3   Antoine Goutenoir   Squeeze in anothe...
55
        hit_count = get_hit_counter()
8c465c94   Antoine Goutenoir   Implement methods...
56
57
58
59
        hit_count += 1
    else:
        hit_count = 1

a03065f3   Antoine Goutenoir   Squeeze in anothe...
60
61
    with open(hit_count_path, 'w') as hcf:
        hcf.write(str(hit_count))
8c465c94   Antoine Goutenoir   Implement methods...
62
63
64
65

    return hit_count


8c465c94   Antoine Goutenoir   Implement methods...
66
67
68
69
70
# # unused
# class FootprintEstimatorDriver(abc.ABCMeta):
#     @abc.abstractmethod
#     def get_travel_footprint(self, from_location, to_location):  # TBD
#         pass