Skip to content

Commit

Permalink
Generalize: make gitbored work with other sites, apps
Browse files Browse the repository at this point in the history
-----------------------------------------------------

* 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
  • Loading branch information
stnbu committed Sep 5, 2018
1 parent fab420b commit 88b2022
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 35 deletions.
7 changes: 3 additions & 4 deletions gitbored/daemon/base.py
Expand Up @@ -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):

Expand Down
49 changes: 49 additions & 0 deletions 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)
31 changes: 0 additions & 31 deletions gitbored/db.py

This file was deleted.

15 changes: 15 additions & 0 deletions 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)

0 comments on commit 88b2022

Please sign in to comment.