Задать вопрос

Тел: +7 965 3737 888

497

Просмотров

2

Ответов

Management command decorator

<p>A quick-and-dirty, and extremely simple, decorator to turn a simple function into a management command.
</p>
<p>This still requires you to have the management directory structure, but allows you to name your primary method whatever you want, and encapsulates the basic functionality of an argument-accepting management commmand.
</p>
<p>The function's docstring will be used for the command's help text if the help arg is not passed to the decorator.
</p>
<p>Simple usage:
</p>
from myapp.utils import command

@command()
def my_command():
    print "Hello, world"
<p>I'm not too familiar with the intricacies of decorators and management commands, so this could probably (most likely) be improved upon, but it's a start.
</p>
<p><strong>Update</strong>:
   I've taken this a bit farther and put my work up on bitbucket: https://bitbucket.org/eternicode/django-management-decorators/src
</p>

Вопрос полезен? Да0/Нет0
file_2694.py(472.0б)
None

Ответы (2):

Ответeternicode:31.08.2010
Ответ полезен? Да0/Нет0

gmandx, interesting idea. I've done some light reading around, and it looks like the functools module has some stuff that could do this.

Ответgmandx:31.08.2010
Ответ полезен? Да0/Нет0

Well, some how (and I think it can be done) the decorator can introspect the function's positional arguments and keyword arguments, and then use those for the command's arguments and options, respectively. Then the only parameter needed for the decorator usage would be the help text.