config.py 1.62 KB
Newer Older
1
import json
2
3
import logging
import os
Christophe Benz's avatar
Christophe Benz committed
4
import sys
Pierre Dittgen's avatar
Pierre Dittgen committed
5
from pathlib import Path
6
7
8
9
10
11
12
13
14

import requests
import toml
from dotenv import load_dotenv

log = logging.getLogger(__name__)

load_dotenv()

Christophe Benz's avatar
Christophe Benz committed
15
16
17
18
19
20
21
22
23
LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO")
numeric_log_level = getattr(logging, LOG_LEVEL.upper(), None)
if not isinstance(numeric_log_level, int):
    log.error('Invalid log level: {}'.format(LOG_LEVEL))
logging.basicConfig(
    format="%(levelname)s:%(name)s:%(message)s",
    level=numeric_log_level,
    stream=sys.stderr,  # script outputs data
)
24
25
26

SECRET_KEY = os.environ.get("SECRET_KEY") or None

Pierre Dittgen's avatar
wip    
Pierre Dittgen committed
27
28
29
API_VALIDATE_ENDPOINT = os.environ.get("API_VALIDATE_ENDPOINT") or None
if API_VALIDATE_ENDPOINT is None:
    log.error("API_VALIDATE_ENDPOINT environment variable is not set, validation is not possible")
30
31
32
33
34
35
36
37
38
39
40
41
42
43

BADGE_CONFIG_URL = os.environ.get("BADGE_CONFIG_URL") or None
BADGE_CONFIG = None
if BADGE_CONFIG_URL is None:
    log.warning("BADGE_CONFIG_URL environment variable is not set, disable badge feature")
else:
    response = requests.get(BADGE_CONFIG_URL)
    if not response.ok:
        log.warning("Can't retrieve badge config from [%s], disable badge feature", BADGE_CONFIG_URL)
    else:
        BADGE_CONFIG = toml.loads(response.text)


SHIELDS_IO_BASE_URL = os.environ.get("SHIELDS_IO_BASE_URL") or None
44
if SHIELDS_IO_BASE_URL and not SHIELDS_IO_BASE_URL.endswith('/'):
45
    SHIELDS_IO_BASE_URL += '/'
Pierre Dittgen's avatar
Pierre Dittgen committed
46

47
48
49
50
51
52
HOMEPAGE_CONFIG_FILE = os.environ.get("HOMEPAGE_CONFIG_FILE") or None
HOMEPAGE_CONFIG = None
if HOMEPAGE_CONFIG_FILE:
    HOMEPAGE_CONFIG_FILE = Path(HOMEPAGE_CONFIG_FILE)
    with HOMEPAGE_CONFIG_FILE.open() as fd:
        HOMEPAGE_CONFIG = json.load(fd)