Complete processing pipeline for Signal
This commit is contained in:
0
core/util/__init__.py
Normal file
0
core/util/__init__.py
Normal file
69
core/util/django_settings_export.py
Normal file
69
core/util/django_settings_export.py
Normal file
@@ -0,0 +1,69 @@
|
||||
"""
|
||||
Export Django settings to templates
|
||||
|
||||
https://github.com/jakubroztocil/django-settings-export
|
||||
|
||||
"""
|
||||
from django.conf import settings as django_settings
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
__version__ = "1.2.1"
|
||||
|
||||
|
||||
VARIABLE_NAME = getattr(django_settings, "SETTINGS_EXPORT_VARIABLE_NAME", "settings")
|
||||
|
||||
|
||||
class SettingsExportError(ImproperlyConfigured):
|
||||
"""Base error indicating misconfiguration."""
|
||||
|
||||
|
||||
class UndefinedSettingError(SettingsExportError):
|
||||
"""An undefined setting name included in SETTINGS_EXPORT."""
|
||||
|
||||
|
||||
class UnexportedSettingError(SettingsExportError):
|
||||
"""An unexported setting has been accessed from a template."""
|
||||
|
||||
|
||||
def settings_export(request):
|
||||
"""
|
||||
The template context processor that adds settings defined in
|
||||
`SETTINGS_EXPORT` to the context. If SETTINGS_EXPORT_VARIABLE_NAME is not
|
||||
set, the context variable will be `settings`.
|
||||
|
||||
"""
|
||||
variable_name = getattr(
|
||||
django_settings, "SETTINGS_EXPORT_VARIABLE_NAME", "settings"
|
||||
)
|
||||
return {variable_name: _get_exported_settings()}
|
||||
|
||||
|
||||
class ExportedSettings(dict):
|
||||
def __getitem__(self, item):
|
||||
"""Fail loudly if accessing a setting that is not exported."""
|
||||
try:
|
||||
return super(ExportedSettings, self).__getitem__(item)
|
||||
except KeyError:
|
||||
if hasattr(self, item):
|
||||
# Let the KeyError propagate so that Django templates
|
||||
# can access the existing attribute (e.g. `items()`).
|
||||
raise
|
||||
raise UnexportedSettingError(
|
||||
"The `{key}` setting key is not accessible"
|
||||
' from templates: add "{key}" to'
|
||||
" `settings.SETTINGS_EXPORT` to change that.".format(key=item)
|
||||
)
|
||||
|
||||
|
||||
def _get_exported_settings():
|
||||
exported_settings = ExportedSettings()
|
||||
for key in getattr(django_settings, "SETTINGS_EXPORT", []):
|
||||
try:
|
||||
value = getattr(django_settings, key)
|
||||
except AttributeError:
|
||||
raise UndefinedSettingError(
|
||||
'"settings.%s" is included in settings.SETTINGS_EXPORT '
|
||||
"but it does not exist. " % key
|
||||
)
|
||||
exported_settings[key] = value
|
||||
return exported_settings
|
||||
68
core/util/logs.py
Normal file
68
core/util/logs.py
Normal file
@@ -0,0 +1,68 @@
|
||||
# Other library imports
|
||||
import logging
|
||||
|
||||
log = logging.getLogger("util")
|
||||
|
||||
debug = True
|
||||
|
||||
# Color definitions
|
||||
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
|
||||
COLORS = {
|
||||
"WARNING": YELLOW,
|
||||
"INFO": WHITE,
|
||||
"DEBUG": BLUE,
|
||||
"CRITICAL": YELLOW,
|
||||
"ERROR": RED,
|
||||
}
|
||||
RESET_SEQ = "\033[0m"
|
||||
COLOR_SEQ = "\033[1;%dm"
|
||||
BOLD_SEQ = "\033[1m"
|
||||
|
||||
|
||||
def formatter_message(message, use_color=True):
|
||||
if use_color:
|
||||
message = message.replace("$RESET", RESET_SEQ).replace("$BOLD", BOLD_SEQ)
|
||||
else:
|
||||
message = message.replace("$RESET", "").replace("$BOLD", "")
|
||||
return message
|
||||
|
||||
|
||||
class ColoredFormatter(logging.Formatter):
|
||||
def __init__(self, msg, use_color=True):
|
||||
logging.Formatter.__init__(self, msg)
|
||||
self.use_color = use_color
|
||||
|
||||
def format(self, record):
|
||||
levelname = record.levelname
|
||||
if self.use_color and levelname in COLORS:
|
||||
levelname_color = (
|
||||
COLOR_SEQ % (30 + COLORS[levelname]) + levelname + RESET_SEQ
|
||||
)
|
||||
record.levelname = levelname_color
|
||||
return logging.Formatter.format(self, record)
|
||||
|
||||
|
||||
def get_logger(name):
|
||||
# Define the logging format
|
||||
FORMAT = "%(asctime)s %(levelname)18s $BOLD%(name)13s$RESET - %(message)s"
|
||||
COLOR_FORMAT = formatter_message(FORMAT, True)
|
||||
color_formatter = ColoredFormatter(COLOR_FORMAT)
|
||||
# formatter = logging.Formatter(
|
||||
|
||||
# Why is this so complicated?
|
||||
ch = logging.StreamHandler()
|
||||
ch.setLevel(logging.INFO)
|
||||
# ch.setFormatter(formatter)
|
||||
ch.setFormatter(color_formatter)
|
||||
|
||||
# Define the logger on the base class
|
||||
log = logging.getLogger(name)
|
||||
log.setLevel(logging.INFO)
|
||||
if debug:
|
||||
log.setLevel(logging.DEBUG)
|
||||
ch.setLevel(logging.DEBUG)
|
||||
|
||||
# Add the handler and stop it being silly and printing everything twice
|
||||
log.addHandler(ch)
|
||||
log.propagate = False
|
||||
return log
|
||||
Reference in New Issue
Block a user