Shovel can be used as an alternative to Django built-in Command. The reasons to use Shovel instead of the Command are:
1. "Rake, for Python" sounds cool.
2. Shovel is light-weight.
3. I can find all tasks in one place (the shovel/ directory).
4. I hate writing a list of make_option.
5. I hate inheriting LabelCommand, BaseCommand, NoArgsCommand, etc.
Okay, you get it... The reasons to use Shovel are actually: (i) "Rake, for Python" sounds cool, and (ii) I am lazy.
I didn't use the term "replacement for Django Command" because we cannot simply invoke Shovel task in the Django codebase using django.core.management.call_command(). (But we can have work-around, like making a Command to proxy the call)
Let's get back to my attempt to mix the two. I placed the shovel/ folder inside the Django project.
djangoproject/ - manage.py - settings.py - urls.py - shovel/
And I setup the Django environment at the top of the Shovel task file.
try: # Load the django environment import imp import os import sys me = os.path.abspath(os.path.dirname(__file__)) module_info = imp.find_module('context', [me]) imp.load_module('context', *module_info) except: print >> sys.stderr, 'Cannot setup Python environment from context.py'At last, the setup injects a directory to Python sys.path and use Django function to load the settings.py.
def setup_django_env(path): from django.core.management import setup_environ try: module_info = imp.find_module('settings', [path]) settings = imp.load_module('settings', *module_info) setup_environ(settings) except ImportError: msg = "Error: Can't find 'settings.py' in configured PYTHON_PATH", path print >> sys.stderr, msg sys.exit(1) # assume shovel/ directory is placed at the same level of settings.py setup_django_env(os.getcwd())This is a common technique to integrate Django with other Python gears as well. By applying this technique, I can freely use any Django functions / models in Shovel.
NOTE 1: The name of Shovel task file cannot collide with any module/app name of the Django project.
NOTE 2: We can place the shovel/ folder outside the Django project but we have to use proper Python path and import statement (e.g. use `from themix.things.models import Thing` instead of `from things.models import Thing`).
My attempt can be found at - https://github.com/mrkschan/shovel-django-mix.
No response to “How to use Shovel with Django”
Post a Comment