From 88b20220c96e4588b803bb57a2391d475ee7041c Mon Sep 17 00:00:00 2001 From: Mike Burr Date: Wed, 5 Sep 2018 14:38:50 -0700 Subject: [PATCH] Generalize: make gitbored work with other sites, apps ----------------------------------------------------- * relocate `gitbored/db.py` -> `gitbored/daemon/db.py`. It is only used by the daemon. * add magic to `db.py` to get `settings` based upon (required) `DJANGO_SETTINGS_MODULE`. * new `logger.py` module to "centralize" logging. * notes/docs describing the changes/hacks --- gitbored/daemon/base.py | 7 +++--- gitbored/daemon/db.py | 49 +++++++++++++++++++++++++++++++++++++++++ gitbored/db.py | 31 -------------------------- gitbored/logger.py | 15 +++++++++++++ 4 files changed, 67 insertions(+), 35 deletions(-) create mode 100644 gitbored/daemon/db.py delete mode 100644 gitbored/db.py create mode 100644 gitbored/logger.py diff --git a/gitbored/daemon/base.py b/gitbored/daemon/base.py index 4db1bce..274e807 100644 --- a/gitbored/daemon/base.py +++ b/gitbored/daemon/base.py @@ -20,10 +20,9 @@ import daemon import daemon.pidfile -from gitbored import db - -logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG) +# this module "proxies" the django ORM using magic +import db +from gitbored import logger class GithubFeed(object): diff --git a/gitbored/daemon/db.py b/gitbored/daemon/db.py new file mode 100644 index 0000000..fc973bb --- /dev/null +++ b/gitbored/daemon/db.py @@ -0,0 +1,49 @@ +# -*- mode: python; coding: utf-8 -*- +"""re-export `models.*` and make models (and ORM) usable outside of web framework. + +>>> import db +>>> record = db.SomeTable(**kwargs) +>>> record.save() + +et cetera. +""" + +import os +import importlib +import django + +from gitbored import logger + +# There is a bit of hackery here. Read comments starting with 'HACK' + +try: + # HACK -- we require that DJANGO_SETTINGS_MODULE be set to the the name + # of the site's settings' module, e.g. `mysite.settings` + django_settings_module_name = os.environ['DJANGO_SETTINGS_MODULE'] +except KeyError: + raise ImportError(""" +The "DJANGO_SETTINGS_MODULE" environment must be set to the name of your site's setting's module. For example: + + export DJANGO_SETTINGS_MODULE="mysite.settings" +""") + +# HACK -- this is magic for "from mysite.settings import *" +globals().update(importlib.import_module( + django_settings_module_name).__dict__) + +from django.conf import settings +from django.db import connections +import atexit + +# HACK -- note that the rhs "DATABASES" here comes from mysite.settings, above +settings.configure(DATABASES=DATABASES) +django.setup() + +#globals().update(importlib.import_module(app_models_module_name).__dict__) +from gitbored.models import * + +def cleanup(): + logger.info('closing all django database connections for this process') + connections.close_all() + +atexit.register(cleanup) diff --git a/gitbored/db.py b/gitbored/db.py deleted file mode 100644 index 8c3c6e4..0000000 --- a/gitbored/db.py +++ /dev/null @@ -1,31 +0,0 @@ -# -*- mode: python; coding: utf-8 -*- -"""re-export `modules.*` and make models (and ORM) usable outside of web framework. - -So, if you have django "app" `foo` installed as a package, from anywhere you should be able to - ->>> record = foo.db.SomeTable(**kwargs) ->>> record.save() - -et cetera. -""" - -import django -from .settings import * -from django.conf import settings -from django.db import connections -import atexit -import logging - -logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG) - -settings.configure(DATABASES=DATABASES) -django.setup() - -from .models import * - -def cleanup(): - logger.info('closing all django database connections for this process') - connections.close_all() - -atexit.register(cleanup) diff --git a/gitbored/logger.py b/gitbored/logger.py new file mode 100644 index 0000000..2d7456a --- /dev/null +++ b/gitbored/logger.py @@ -0,0 +1,15 @@ +# -*- mode: python; coding: utf-8 -*- +"""A small amount of justified magic: with this module, + +>>> import logger +>>> logger.debug('yay!') +""" + +import logging + +_logger = logging.getLogger(__name__) +_logger.setLevel(logging.DEBUG) + +for level in 'debug', 'info', 'warning', 'error', 'exception', 'critical', 'addHandler': + globals()[level] = getattr(_logger, level) +