Skip to content

Commit

Permalink
issue #21
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtepkeev committed Nov 4, 2015
1 parent 838bcde commit 709af45
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
3 changes: 2 additions & 1 deletion architect/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pkgutil
import argparse

from .. import __version__
from .. import __version__, orms
from ..exceptions import (
BaseArchitectError,
CommandNotProvidedError,
Expand Down Expand Up @@ -88,6 +88,7 @@ def main():
except AttributeError:
parser.error('too few arguments')
else:
orms.init()
try:
commands[command]['parser'].result(args.func(vars(args)))
except BaseArchitectError as e:
Expand Down
27 changes: 16 additions & 11 deletions architect/orms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import os
import pkgutil
def init():
"""
Some ORMs, e.g. Django, may require initialization to be performed, but only at some certain
point, e.g. after some variables are set and so on, this function provides an ability to run
this initialization for all supported ORMs automatically but only when needed by the caller
"""
import os
import pkgutil

# Some ORMs, e.g Django, requires some setup to be performed before one can use them
for _, name, is_pkg in pkgutil.iter_modules([os.path.dirname(__file__)]):
if is_pkg:
try:
__import__(name, level=0) # try to import globally and see if ORM is installed
except ImportError:
pass
else:
__import__(name, globals(), level=1) # if yes, run Architect's init code for this ORM
for _, name, is_pkg in pkgutil.iter_modules([os.path.dirname(__file__)]):
if is_pkg:
try:
__import__(name, level=0) # try to import globally and see if ORM is installed
except ImportError:
pass
else:
getattr(__import__(name, globals(), level=1), 'init', lambda: None)() # if yes, run init() for this ORM
4 changes: 1 addition & 3 deletions architect/orms/django/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os

if 'DJANGO_SETTINGS_MODULE' in os.environ:
def init():
try:
import django
django.setup()
Expand Down

0 comments on commit 709af45

Please sign in to comment.