config.py 1.54 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()

15
16

def without_trailing_slash(url):
Pierre Dittgen's avatar
Pierre Dittgen committed
17
    return url[:-1] if url.endswith("/") else url
18
19


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

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

SHIELDS_IO_BASE_URL = os.environ.get("SHIELDS_IO_BASE_URL") or None
33
34
if SHIELDS_IO_BASE_URL:
    SHIELDS_IO_BASE_URL = without_trailing_slash(SHIELDS_IO_BASE_URL)
Pierre Dittgen's avatar
Pierre Dittgen committed
35

36
37
38
39
40
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:
41
42
43
44
45
46
        try:
            HOMEPAGE_CONFIG = json.load(fd)
        except ValueError as exc:
            raise ValueError(
                f"Could not load homepage config JSON file {HOMEPAGE_CONFIG_FILE}"
            ) from exc
Christophe Benz's avatar
Christophe Benz committed
47
48

MATOMO_BASE_URL = os.getenv("MATOMO_BASE_URL") or None
49
50
if MATOMO_BASE_URL:
    MATOMO_BASE_URL = without_trailing_slash(MATOMO_BASE_URL)
Christophe Benz's avatar
Christophe Benz committed
51
52
53
MATOMO_SITE_ID = os.getenv("MATOMO_SITE_ID") or None
if MATOMO_SITE_ID:
    MATOMO_SITE_ID = int(MATOMO_SITE_ID)
Pierre Dittgen's avatar
Pierre Dittgen committed
54
55

SENTRY_DSN = os.environ.get("SENTRY_DSN")