config.py 8.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
"""Application configuration classes for different environments / use cases"""
import os

from dotenv import load_dotenv
from graphannis.cs import CorpusStorageManager

basedir = os.path.abspath(os.path.dirname(__file__))
load_dotenv(os.path.join(basedir, ".env"))


class Config(object):
    """Base configuration for this application.

    Contains information about the current environment,
    different levels of logging, the database, and hosting options.
    The values are shared with all subclasses (if not overridden)."""
    # these have to be at the top
    CURRENT_WORKING_DIRECTORY = os.getcwd()
    CURRENT_WORKING_DIRECTORY_PARENT = os.path.dirname(CURRENT_WORKING_DIRECTORY)
    CURRENT_WORKING_DIRECTORY_PARTS = os.path.split(CURRENT_WORKING_DIRECTORY)  # [::-1]
    GRAPH_DATABASE_DIR = os.path.join(os.sep, "tmp", "graphannis-data")
    MC_SERVER_DIRECTORY = CURRENT_WORKING_DIRECTORY if \
        os.path.split(CURRENT_WORKING_DIRECTORY)[-1] == "mcserver" else os.path.join(CURRENT_WORKING_DIRECTORY,
                                                                                     "mcserver")
    SERVER_URI_BASE = "/mc/api/v1.0/"
    # dirty hack to get the app working either with the Gunicorn/Flask CLI or the PyCharm debugger
    MC_SERVER_APP_DIRECTORY = os.path.join(MC_SERVER_DIRECTORY, "app") if os.path.split(MC_SERVER_DIRECTORY)[
                                                                              -1] == "mcserver" else MC_SERVER_DIRECTORY
    IS_DOCKER = os.environ.get("IS_THIS_A_DOCKER_CONTAINER", False)
    ASSETS_DIRECTORY = os.path.join(MC_SERVER_APP_DIRECTORY, "assets")
    FILES_DIRECTORY = os.path.join(MC_SERVER_APP_DIRECTORY, "files")
    TMP_DIRECTORY = os.path.join(FILES_DIRECTORY, "tmp")
    TREEBANKS_PATH = os.path.join(ASSETS_DIRECTORY, "treebanks")
    TREEBANKS_PROIEL_PATH = os.path.join(TREEBANKS_PATH, "proiel")

    AQL_CASE = "/.*Case=.*/"
    AQL_DEP = "->dep"
    AQL_DEPREL = "deprel"
    AQL_TOK = "tok"
    CACHE_DIRECTORY = os.path.join(MC_SERVER_APP_DIRECTORY, "cache")
    CONLLU2SVG_PATH_LINUX = os.path.join(ASSETS_DIRECTORY, "conllu2svg_linux64")
    CONLLU2SVG_PATH_OSX = os.path.join(ASSETS_DIRECTORY, "conllu2svg_osx")
    CORPUS_STORAGE_MANAGER: CorpusStorageManager = None
    CORPUS_STORAGE_MANAGER_PORT = 6555
    COVERAGE_CONFIGURATION_FILE_NAME = ".coveragerc"
    COVERAGE_ENVIRONMENT_VARIABLE = "COVERAGE_PROCESS_START"
    CSM_DIRECTORY = os.path.join(CURRENT_WORKING_DIRECTORY, "csm")
    CSM_NETWORK_DOCKER = "csm"
    CSRF_ENABLED = True
    CTS_API_BASE_URL = "https://cts.perseids.org/api/cts/"
    CUSTOM_CORPUS_CAES_GAL_FILE_PATH = os.path.join(TREEBANKS_PROIEL_PATH, "caes-gal.conllu")
    CUSTOM_CORPUS_CIC_ATT_FILE_PATH = os.path.join(TREEBANKS_PROIEL_PATH, "cic-att.conllu")
    CUSTOM_CORPUS_CIC_OFF_FILE_PATH = os.path.join(TREEBANKS_PROIEL_PATH, "cic-off.conllu")
    CUSTOM_CORPUS_LATIN_NT_FILE_PATH = os.path.join(TREEBANKS_PROIEL_PATH, "latin-nt.conllu")
    CUSTOM_CORPUS_PAL_AGR_FILE_PATH = os.path.join(TREEBANKS_PROIEL_PATH, "pal-agr.conllu")
    CUSTOM_CORPUS_PER_AET_FILE_PATH = os.path.join(TREEBANKS_PROIEL_PATH, "per-aeth.conllu")
    CUSTOM_CORPUS_VIVA_FILE_PATH = os.path.join(ASSETS_DIRECTORY, "viva_lektionstexte1-32.txt")
    CUSTOM_CORPUS_VIVA_URN = "urn:custom:latinLit:viva.lat"
    CUSTOM_CORPUS_PROIEL_URN_TEMPLATE = "urn:custom:latinLit:proiel.{0}.lat"
    DATABASE_LOCAL_URL = "postgresql://postgres@0.0.0.0:5432/postgres"
    DATABASE_TABLE_ALEMBIC = "alembic_version"
    DATABASE_URL = os.environ.get("DATABASE_URL",
                                  ("postgresql://postgres@db:5432/" if IS_DOCKER else
                                   "postgresql://0.0.0.0:5432/postgres"))
    DEBUG = False
    FLASK_MIGRATE = "migrate"
    GRAPHANNIS_DEPENDENCY_LINK = "dep"
    GRAPHANNIS_LOG_PATH = os.path.join(os.getcwd(), "graphannis.log")
    H5P_DRAG_TEXT = "drag_text"
    H5P_FILL_BLANKS = "fill_blanks"
    H5P_MULTI_CHOICE = "multi_choice"
    H5P_VOC_LIST = "voc_list"
    HOST_IP = os.environ.get("HOST_IP", "0.0.0.0" if IS_DOCKER else "127.0.0.1")
    HOST_IP_CSM = CSM_NETWORK_DOCKER if IS_DOCKER else HOST_IP
    HOST_PORT = 5000
    INTERNET_PROTOCOL = "http://"
    INTERVAL_CORPUS_AGE_CHECK = 60 * 60
    INTERVAL_CORPUS_UPDATE = 60 * 60 * 24
    INTERVAL_EXERCISE_DELETE = 60 * 60 * 24 * 30 * 12
    INTERVAL_FILE_DELETE = 60 * 60 * 24
    INTERVAL_STATIC_EXERCISES = 60 * 60 * 24
    IS_PRODUCTION = os.environ.get("FLASK_ENV_VARIABLE", "development") == "production"
    LEARNING_ANALYTICS_DIRECTORY = os.path.join(FILES_DIRECTORY, "learning_analytics")
    LOG_PATH_CSM = "csm.log"
    LOG_PATH_MCSERVER = "mcserver.log"
    MIGRATIONS_DIRECTORY = os.path.join(MC_SERVER_DIRECTORY, "migrations")
    NETWORK_GRAPH_TMP_PATH = os.path.join(TMP_DIRECTORY, "graph.svg")
    PANEGYRICI_LATINI_DIRECTORY = os.path.join(ASSETS_DIRECTORY, "panegyrici_latini")
    PANEGYRICI_LATINI_MODEL_PATH = os.path.join(PANEGYRICI_LATINI_DIRECTORY, "panegyrici_latini.model")
    PANEGYRICI_LATINI_TEXT_PATH = os.path.join(PANEGYRICI_LATINI_DIRECTORY, "panegyrici_latini_tokenized.txt")
    PLATFORM_MACOS = "darwin"
    PLATFORM_WINDOWS = "win32"
    PUBLIC_FRONTEND_URL = os.environ.get("PUBLIC_FRONTEND_URL", "http://localhost:8100/")
    REFF_CACHE_DIRECTORY = os.path.join(CACHE_DIRECTORY, "reff")
    SECRET_KEY = 'this-really-needs-to-be-changed'
    # BEGIN endpoints
    # use these endpoints to access the REST API by appending them to the host name (e.g. "http://127.0.0.1:5000")
    SERVER_URI_ANNIS_FIND = SERVER_URI_BASE + "find"
    SERVER_URI_CORPORA = SERVER_URI_BASE + "corpora"
    SERVER_URI_CSM = "/"
    SERVER_URI_CSM_SUBGRAPH = SERVER_URI_CSM + "subgraph"
    SERVER_URI_EXERCISE = SERVER_URI_BASE + "exercise"
    SERVER_URI_EXERCISE_LIST = SERVER_URI_BASE + "exerciseList"
    SERVER_URI_FILE = SERVER_URI_BASE + "file"
    SERVER_URI_FREQUENCY = SERVER_URI_BASE + "frequency"
    SERVER_URI_H5P = SERVER_URI_BASE + "h5p"
    SERVER_URI_KWIC = SERVER_URI_BASE + "kwic"
    SERVER_URI_RAW_TEXT = SERVER_URI_BASE + "rawtext"
    SERVER_URI_STATIC_EXERCISES = SERVER_URI_BASE + "exercises"
    SERVER_URI_TEXT_COMPLEXITY = SERVER_URI_BASE + "textcomplexity"
    SERVER_URI_VALID_REFF = SERVER_URI_BASE + "validReff"
    SERVER_URI_VECTOR_NETWORK = SERVER_URI_BASE + "vectorNetwork"
    SERVER_URI_VOCABULARY = SERVER_URI_BASE + "vocabulary"
    # END endpoints
    SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL") or "sqlite:///" + os.path.join(basedir, "app.db")
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    STATIC_EXERCISES_REPOSITORY_URL = "https://scm.cms.hu-berlin.de/callidus/mc_frontend/-/archive/master/mc_frontend-master.zip?path=src%2Fassets%2Fh5p"
    STOP_WORDS_LATIN_PATH = os.path.join(CACHE_DIRECTORY, "stop_words_latin.json")
    STOP_WORDS_URL = "https://raw.githubusercontent.com/aurelberra/stopwords/master/stopwords_latin.json"
    TEST_FLAG = "-test"
    TESTING = False
    TRAP_HTTP_EXCEPTIONS = True
    TREEBANKS_CACHE_DIRECTORY = os.path.join(CACHE_DIRECTORY, "treebanks")
    UDPIPE_MODEL_PATH = os.path.join(ASSETS_DIRECTORY, "latin-ittb-ud-2.0-conll17-170315.udpipe")
    UDPIPE_PATH_LINUX = os.path.join(ASSETS_DIRECTORY, "udpipe_linux64")
    UDPIPE_PATH_OSX = os.path.join(ASSETS_DIRECTORY, "udpipe_osx")
    UDPIPE_PATH_WIN64 = os.path.join(ASSETS_DIRECTORY, "udpipe_win64.exe")
    VOCABULARY_AGLDT_FILE_NAME = "vocabulary_ancient_greek_latin_dependency_treebank.json"
    VOCABULARY_BWS_FILE_NAME = "vocabulary_bamberg_core.json"
    VOCABULARY_PROIEL_FILE_NAME = "vocabulary_proiel_treebank.json"
    VOCABULARY_VIVA_FILE_NAME = "vocabulary_viva.json"


class ProductionConfig(Config):
    """Configuration for the production environment"""
    DEBUG = False
    SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL_PROD",
                                             "postgresql://postgres@db:5432/" if
                                             Config.IS_DOCKER else "postgresql://0.0.0.0:5432/postgres")


class StagingConfig(Config):
    """Configuration for staging"""
    DEVELOPMENT = True
    DEBUG = True


class DevelopmentConfig(Config):
    """Configuration for the development environment"""
    DEVELOPMENT = True
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = os.environ.get(
        "DATABASE_URL", "postgresql://postgres@db:5432/" if Config.IS_DOCKER else Config.DATABASE_LOCAL_URL)


class TestingConfig(Config):
    """Configuration for testing"""
    CTS_API_GET_PASSAGE_URL = "https://cts.perseids.org/api/cts/?request=GetPassage&urn=urn:cts:latinLit:phi1351.phi002.perseus-lat1:2.2"
    HOST_IP = "0.0.0.0"
    PRESERVE_CONTEXT_ON_EXCEPTION = False
    SERVER_NAME = Config.HOST_IP + ":{0}".format(Config.HOST_PORT)
    SESSION_COOKIE_DOMAIN = False
    SIMULATE_CORPUS_NOT_FOUND = False
    SIMULATE_EMPTY_GRAPH = False
    SIMULATE_HTTP_ERROR = False
    SQLALCHEMY_DATABASE_URI = f"sqlite:///{os.path.join(basedir, 'app.db')}"
    STATIC_EXERCISES_ZIP_FILE_PATH = os.path.join(Config.TMP_DIRECTORY, "static_exercises.zip")
    TESTING = True


if __name__ == "__main__":
    a = 0
    # TODO: Write config tests