app completion in django_bash_completion - testing please

2 views
Skip to first unread message

Rob Hudson

unread,
Feb 22, 2007, 3:18:52 PM2/22/07
to Django developers
I implemented app completion and would like comments and testing for
those on *nix.

What's the easiest way to provide a patch? Open a new ticket and
attached? Or attach it to the original bash_completion ticket here:
http://code.djangoproject.com/ticket/1240 ? I'm pasting the diff
below so people can test for now.

A couple notes:

* My patch uses sed, grep, and tr instead of the previous patch which
uses Python. The Python way does seem nice since Django already knows
about its own apps. But Adrian made a comment on the ticket above
that it didn't work for him.

* Depends on settings.py being named "settings.py" and depends on the
user being in the project root directory (where settings.py is). The
previous patch had this dependency too.

* This essentially does the following (each command):

1. Filters settings.py and prints everything in INSTALLED_APPS
2. Strips any lines with django in them
3. Does pattern matching to get the app name.
4. Finally, tr merges the separate lines into a single line with
apps separated by spaces.

It's working on my Mac. If I type "./manage.py sql " and press tab
twice, I get a list of apps and starting to type one does tab
completion.

Comments appreciated.

-Rob

Index: extras/django_bash_completion
===================================================================
--- extras/django_bash_completion (revision 4557)
+++ extras/django_bash_completion (working copy)
@@ -79,10 +79,12 @@
adminindex|install|reset| \
sql|sqlall|sqlclear|sqlindexes| \
sqlinitialdata|sqlreset|sqlsequencereset)
- # App completion isn't yet implemented, but here's where
that
- # would go.
- # COMPREPLY=( $(compgen -W "auth core" -- ${cur}) )
- COMPREPLY=()
+ # App completion
+ apps=`sed -n '/INSTALLED_APPS = (/,/)/p' settings.py | \
+ grep -v django |
+ sed -n "s/^[ ]*'.*\.\(.*\)'.*$/\1 /pg" | \
+ tr -d '\n'`
+ COMPREPLY=( $(compgen -W "${apps}" -- ${cur}) )
return 0
;;

Rob Hudson

unread,
Feb 23, 2007, 12:04:11 PM2/23/07
to Django developers
I re-opened bug 1240 and added my patch there:
http://code.djangoproject.com/ticket/1240

Simon G.

unread,
Feb 24, 2007, 6:28:09 AM2/24/07
to Django developers
Hi Rob,

This looks good, thanks. I've triage'd that ticket on to "Ready to
checkin" and we'll let one of the core developers check it out and see
if it meets their approval :-)

-Simon G.

paolo

unread,
Feb 24, 2007, 7:48:24 AM2/24/07
to Django developers
Hi Rob, your patch works pretty well, thanks! Finally the script
supports full completion of names ;-)

Just a couple of things.

I'd like to change the "grep -v django" line with "grep -v
django.contrib" so that applications containing "django" as a part of
their name (ex. djangotestproject.appname) are not discarded.

Then I'd like that completion script would support names as 'appname'
instead of 'projectname.appname'. I got this work for myself using
s/^[ ]*'\(.*\.\)*\(.*\)'.*$/\2 /pg as regular expression in sed.

Regards

Rob Hudson

unread,
Feb 24, 2007, 12:57:03 PM2/24/07
to Django developers
This morning I'm working on a few things with this...

> I'd like to change the "grep -v django" line with "grep -v
> django.contrib" so that applications containing "django" as a part of
> their name (ex. djangotestproject.appname) are not discarded.

Good point, I'll add it in to the next patch.

> Then I'd like that completion script would support names as 'appname'
> instead of 'projectname.appname'. I got this work for myself using
> s/^[ ]*'\(.*\.\)*\(.*\)'.*$/\2 /pg as regular expression in sed.

Right, another good point. I added in support for stuff after the
closing quote because in our settings.py we usually set PROJECT_NAME
near the top and re-use it in our script, so our installed apps look
like this:

'%s.appname' % PROJECT_NAME,

And this supports that. I'll update it to support just 'appname' as
well.

I'm also adding checks for the environment variable
DJANGO_SETTINGS_MODULE so this doesn't have to be in the same
directory to work.

I'll post an updated patch.

Thanks,
Rob

Reply all
Reply to author
Forward
0 new messages