Skip to content

Commit

Permalink
fix(logging): weird settings import
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjeannesson committed Mar 14, 2024
1 parent 742e13f commit 5aefd6c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 72 deletions.
123 changes: 61 additions & 62 deletions django_napse/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,72 +62,71 @@ def NAPSE_MASTER_KEY(self) -> str: # noqa: D102


napse_settings = DjangoNapseSettings()
hasattr(settings, "LOGGING") # query setting to make sure it is loaded

if settings.configured:
for app in [
"rest_framework",
"rest_framework_api_key",
"django_celery_beat",
"corsheaders",
]:
if app not in settings.INSTALLED_APPS:
warning = f"{app} not found in settings.INSTALLED_APPS. Please add it to your settings file."
logger.warning(warning)
settings.INSTALLED_APPS += (app,)

if "REST_FRAMEWORK" not in settings.__dir__():
warning = "REST_FRAMEWORK not found in settings. Please add it to your settings file."
logger.warning(warning)
settings.REST_FRAMEWORK = {
"DEFAULT_PERMISSION_CLASSES": ["django_napse.api.custom_permissions.HasAdminPermission"],
}
for permission in ["django_napse.api.custom_permissions.HasAdminPermission"]:
if permission not in settings.REST_FRAMEWORK["DEFAULT_PERMISSION_CLASSES"]:
warning = f"{permission} not found in settings. Please add it to your settings file."
logger.warning(warning)

if "NAPSE_SECRETS_FILE_PATH" not in settings.__dir__():
warning = "NAPSE_SECRETS_FILE_PATH not found in settings. Please add it to your settings file."
logger.warning(warning)
if not napse_settings.NAPSE_SECRETS_FILE_PATH.exists():
logger.warning(
"WARNING: No secrets file found at %s. Creating one now.",
napse_settings.NAPSE_SECRETS_FILE_PATH,
)
secrets_file = napse_settings.NAPSE_SECRETS_FILE_PATH.open("w")
json.dump({"Exchange Accounts": {}}, secrets_file)
secrets_file.close()

if "NAPSE_ENV_FILE_PATH" not in settings.__dir__():
warning = "NAPSE_ENV_FILE_PATH not found in settings. Please add it to your settings file."
for app in [
"rest_framework",
"rest_framework_api_key",
"django_celery_beat",
"corsheaders",
]:
if app not in settings.INSTALLED_APPS:
warning = f"{app} not found in settings.INSTALLED_APPS. Please add it to your settings file."
logger.warning(warning)
settings.INSTALLED_APPS += (app,)

if not napse_settings.NAPSE_ENV_FILE_PATH.exists():
logger.warning(
"WARNING: No env file found at %s. Creating one now.",
napse_settings.NAPSE_ENV_FILE_PATH,
)
env_file = napse_settings.NAPSE_ENV_FILE_PATH.open("w")
env_file.write("")
env_file.close()

if {*list(napse_settings.NAPSE_EXCHANGE_CONFIGS.keys())} != set(EXCHANGES):
error_msg = "NAPSE_EXCHANGE_CONFIGS does not match the list of exchanges. Can't start the server."
raise NapseError.SettingsError(error_msg)

if "LOGGING" not in settings.__dir__():
settings.LOGGING = {}
settings.LOGGING["version"] = 1
settings.LOGGING["disable_existing_loggers"] = False
settings.LOGGING["handlers"] = {"console": {"class": "logging.StreamHandler"}, **settings.LOGGING.get("handlers", {})}
settings.LOGGING["loggers"] = {
"django": {
"handlers": ["console"],
"level": "INFO",
},
**settings.LOGGING.get("loggers", {}),
if "REST_FRAMEWORK" not in settings.__dir__():
warning = "REST_FRAMEWORK not found in settings. Please add it to your settings file."
logger.warning(warning)
settings.REST_FRAMEWORK = {
"DEFAULT_PERMISSION_CLASSES": ["django_napse.api.custom_permissions.HasAdminPermission"],
}
for permission in ["django_napse.api.custom_permissions.HasAdminPermission"]:
if permission not in settings.REST_FRAMEWORK["DEFAULT_PERMISSION_CLASSES"]:
warning = f"{permission} not found in settings. Please add it to your settings file."
logger.warning(warning)

if "NAPSE_SECRETS_FILE_PATH" not in settings.__dir__():
warning = "NAPSE_SECRETS_FILE_PATH not found in settings. Please add it to your settings file."
logger.warning(warning)
if not napse_settings.NAPSE_SECRETS_FILE_PATH.exists():
logger.warning(
"WARNING: No secrets file found at %s. Creating one now.",
napse_settings.NAPSE_SECRETS_FILE_PATH,
)
secrets_file = napse_settings.NAPSE_SECRETS_FILE_PATH.open("w")
json.dump({"Exchange Accounts": {}}, secrets_file)
secrets_file.close()

if "NAPSE_ENV_FILE_PATH" not in settings.__dir__():
warning = "NAPSE_ENV_FILE_PATH not found in settings. Please add it to your settings file."
logger.warning(warning)

if not napse_settings.NAPSE_ENV_FILE_PATH.exists():
logger.warning(
"WARNING: No env file found at %s. Creating one now.",
napse_settings.NAPSE_ENV_FILE_PATH,
)
env_file = napse_settings.NAPSE_ENV_FILE_PATH.open("w")
env_file.write("")
env_file.close()

if {*list(napse_settings.NAPSE_EXCHANGE_CONFIGS.keys())} != set(EXCHANGES):
error_msg = "NAPSE_EXCHANGE_CONFIGS does not match the list of exchanges. Can't start the server."
raise NapseError.SettingsError(error_msg)

if "LOGGING" not in settings.__dir__():
settings.LOGGING = {}
settings.LOGGING["version"] = 1
settings.LOGGING["disable_existing_loggers"] = False
settings.LOGGING["handlers"] = {"console": {"class": "logging.StreamHandler"}, **settings.LOGGING.get("handlers", {})}
settings.LOGGING["loggers"] = {
"django": {
"handlers": ["console"],
"level": "INFO",
},
**settings.LOGGING.get("loggers", {}),
}
if not settings.DEBUG:
settings.LOGGING["root"] = {
"handlers": ["console"],
"level": "INFO",
Expand Down
6 changes: 2 additions & 4 deletions django_napse/simulations/tasks/dataset_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ class DataSetQueueTask(BaseTask):
time_limit = 60 * 60
soft_time_limit = 60 * 60

def run(self) -> None:
def _run(self) -> None:
"""Run a task to process all DataSetQueues.
Raises:
SimulationError.DataSetQueueError: If the DataSetQueue is not finished.
"""
if not self.avoid_overlap(verbose=False):
return
self.info("Running DataSetQueueTask")
queue = DataSetQueue.objects.all().order_by("created_at").first()
if queue is None:
Expand All @@ -36,4 +34,4 @@ def run(self) -> None:


DataSetQueueTask().delete_task()
# DataSetQueueTask().register_task()
DataSetQueueTask().register_task()
6 changes: 2 additions & 4 deletions django_napse/simulations/tasks/simulation_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ class SimulationQueueTask(BaseTask):
time_limit = 60 * 60
soft_time_limit = 60 * 60

def run(self) -> None:
def _run(self) -> None:
"""Run a task to process all SimulationQueues.
Raises:
e: If the SimulationQueue gebe
SimulationError.BotSimQueueError: _description_
"""
if not self.avoid_overlap(verbose=False):
return
self.info("Running SimQueueTask")
queue = SimulationQueue.objects.filter(error=False).order_by("created_at").first()

Expand Down Expand Up @@ -50,4 +48,4 @@ def run(self) -> None:


SimulationQueueTask().delete_task()
# SimulationQueueTask().register_task()
SimulationQueueTask().register_task()
2 changes: 1 addition & 1 deletion tests/test_app/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.test_app.settings")

application = get_asgi_application()
4 changes: 4 additions & 0 deletions tests/test_app/celery_app.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.test_app.settings")

from django_napse.core.celery_app import celery_app # noqa
2 changes: 1 addition & 1 deletion tests/test_app/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.test_app.settings")

application = get_wsgi_application()

0 comments on commit 5aefd6c

Please sign in to comment.