-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.py
More file actions
74 lines (53 loc) · 1.65 KB
/
config.py
File metadata and controls
74 lines (53 loc) · 1.65 KB
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
"""Flask configuration classes and environment setup."""
import os
import sys
from pathlib import Path
# Load environment variables from .env file (development only)
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass # dotenv not available in production
# Add the root of the project to sys.path
PROJECT_ROOT = Path(__file__).parent.absolute()
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
class Config:
"""Base configuration."""
ENV_NAME = "default"
DEBUG = False
TESTING = False
JSON_SORT_KEYS = False
PROPAGATE_EXCEPTIONS = False
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
@staticmethod
def init_app(app):
"""Hook for additional initialization."""
return None
class DevelopmentConfig(Config):
"""Development configuration."""
ENV_NAME = "development"
DEBUG = True
class TestingConfig(Config):
"""Testing configuration."""
ENV_NAME = "testing"
TESTING = True
LOG_LEVEL = "DEBUG" # Enable debug logging for tests
# Disable CSRF for testing
WTF_CSRF_ENABLED = False
class ProductionConfig(Config):
"""Production configuration."""
ENV_NAME = "production"
CONFIG_BY_NAME = {
"development": DevelopmentConfig,
"testing": TestingConfig,
"production": ProductionConfig,
"default": Config,
}
def get_config(config_name: str | None = None):
"""Resolve a configuration class by name."""
if not config_name:
config_name = (
os.environ.get("FLASK_CONFIG") or os.environ.get("APP_ENV") or "default"
)
return CONFIG_BY_NAME.get(config_name.lower(), Config)