Projekt

Obecné

Profil

« Předchozí | Další » 

Revize c073a0fc

Přidáno uživatelem Jan Pašek před asi 4 roky(ů)

Re #8569 Added support for handling configuration files

Zobrazit rozdíly:

app.py
66 66
    """
67 67
    global certificate_controller
68 68

  
69
    injector = Injector([configuration.configure_default, ConnectionProvider])
69
    injector = Injector([configuration.configure_env_variable, ConnectionProvider])
70 70
    certificate_controller = injector.get(CertController)
71 71

  
72 72
    # There's a little dependency on the CryptoService, which is not a pretty thing from
src/config/configuration.py
1
import os
2

  
3
import configparser
1 4
from injector import singleton
2 5

  
6
from src.constants import DEFAULT_CONNECTION_STRING
7

  
8
DATABASE_SECTION = "Database"
9
DATABASE_CONNECTION_STRING = "ConnectionString"
3 10

  
4 11
class Configuration:
12
    """
13
    Configuration class servers for injecting current application
14
    configuration all over the application
15
    """
16

  
17
    def __init__(self):
18
        """
19
        Constructor
20
        It must initialize all variables to their default values
21
        """
22
        self.connection_string = DEFAULT_CONNECTION_STRING
23

  
5 24

  
6
    def __init__(self, connection_string):
7
        self.connection_string = connection_string
25
def configure_env_variable(binder):
26
    """
27
    Load configuration file stored in X509_CONFIG environment variable.
28
    If the file is not specified, use the default configuration
29
    :param binder: injector configuration binder instance
30
    :return: N/A
31
    """
32
    # TODO log which configuration is going to be used
33
    config_file = os.environ.get("X509_CONFIG")
34
    app_configuration = Configuration()
35
    # if configuration file is not specified use the default configuration
36
    if config_file is not None:
37
        config = configparser.ConfigParser()
38
        config.read(config_file)
8 39

  
40
        if config[DATABASE_SECTION] is not None:
41
            database_config = config[DATABASE_SECTION]
42
            app_configuration.connection_string = database_config.get(DATABASE_CONNECTION_STRING,
43
                                                                      DEFAULT_CONNECTION_STRING)
9 44

  
10
def configure_default(binder):
11
    configuration = Configuration("db/database_sqlite.db")
12
    binder.bind(Configuration, to=configuration, scope=singleton)
45
    binder.bind(Configuration, to=app_configuration, scope=singleton)
src/constants.py
42 42
# DB column names of PrivateKey table
43 43
COL_CERTIFICATE_ID = "certificate_id"
44 44
COL_USAGE_TYPE_ID = "usage_type_id"
45

  
46
# configuration default
47
DEFAULT_CONNECTION_STRING = "db/database_sqlite.db"

Také k dispozici: Unified diff