Porting _django_bash_completion to Windows Powershell

61 views
Skip to first unread message

guillermooo

unread,
Mar 10, 2010, 3:12:03 PM3/10/10
to Django developers
** I might be posting in the wrong place. Apologies if I do, but this
felt like the right place to ask! **

Hi!

I'm trying to port Django's django_bash_completion script to Windows
Powershell, but I only know so much bash, so I'd need a little help in
understanding what's going on.

As far as I understand from reading the code, it just looks at the
arguments to "complete" to see whether they look something like this:

python manage.py

However, I don't see any call to an external script (bash or python).
How do you go from there, once you know you need the candidate
completions for the manage.py script? I can think of a few ways of
accomplishing this, but I'd rather see the standard implementation the
Django devs have come up with.

Thanks!
Guillermo

Message has been deleted

Russell Keith-Magee

unread,
Mar 10, 2010, 6:50:16 PM3/10/10
to django-d...@googlegroups.com

There's no external script; Bash does command line completion by
calling the actual command with some special environment variables --
specifically, COMP_WORDS and COMP_CWORD environment variables.

http://www.gnu.org/software/bash/manual/bash.html#lbCV

Internally, the autocompletion is done using the
ManagementUtility.autocomplete() method, line 264 of
django/core/management/__init__.py.

Yours,
Russ Magee %-)

guillermooo

unread,
Mar 17, 2010, 10:35:31 AM3/17/10
to Django developers
>Internally, the autocompletion is done using the
> ManagementUtility.autocomplete() method, line 264 of
> django/core/management/__init__.py.

I've taken a look the code, but there a few details that I don't
understand.

autocomplete() is executed always, regardless whether the user has
requested completions or not. The only early exit point of
autocomplete() is reached if DJANGO_AUTO_COMPLETE is false. Otherwise,
sys.exit(1) returns to the console. How is this avoided when you do
something like `manage.py sqlall<ENTER>`? Is the variable
DJANGO_AUTO_COMPLETE implicitly unset after each execution of the
completion bash script?

Cheers,
Guillermo

Arthur Koziel

unread,
Mar 17, 2010, 4:14:08 PM3/17/10
to django-d...@googlegroups.com
On Mar 17, 2010, at 3:35 PM, guillermooo wrote:

> autocomplete() is executed always, regardless whether the user has
> requested completions or not. The only early exit point of
> autocomplete() is reached if DJANGO_AUTO_COMPLETE is false. Otherwise,
> sys.exit(1) returns to the console. How is this avoided when you do
> something like `manage.py sqlall<ENTER>`? Is the variable
> DJANGO_AUTO_COMPLETE implicitly unset after each execution of the
> completion bash script?

36 COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \
37 COMP_CWORD=$COMP_CWORD \
38 DJANGO_AUTO_COMPLETE=1 $1 ) )

The Python function is executed in a Bash subshell (that's what the outer round braces are for). COMP_WORDS/COMP_CWORD and DJANGO_AUTO_COMPLETE are local variables and available only within this subshell.

Arthur

guillermooo

unread,
Apr 9, 2010, 8:24:15 AM4/9/10
to Django developers
One final question about the bash script's functionality--I cannot
test easily on Linux now, so I'd love to hear some feedback.

As far as I can tell, django-admin.py sqlall <TAB> will only show
completions if DJANGO_SETTINGS_MODULE is set. Is this correct?

guillermooo

unread,
Apr 9, 2010, 2:16:11 PM4/9/10
to Django developers
For the two Django devs on Windows out there, you might want to use
this:

http://bitbucket.org/guillermooo/powershell-utilities-for-django-developers/

Tests, docs, etc. to come soon.

Reply all
Reply to author
Forward
0 new messages