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
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 %-)
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
> 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
As far as I can tell, django-admin.py sqlall <TAB> will only show
completions if DJANGO_SETTINGS_MODULE is set. Is this correct?
http://bitbucket.org/guillermooo/powershell-utilities-for-django-developers/
Tests, docs, etc. to come soon.