~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to extras/django_bash_completion

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#
32
32
# To uninstall, just remove the line from your .bash_profile and .bashrc.
33
33
 
34
 
# Enable extended pattern matching operators.
35
 
shopt -s extglob
36
 
 
37
34
_django_completion()
38
35
{
39
 
    local cur prev opts actions action_shell_opts action_runfcgi_opts
40
 
    COMPREPLY=()
41
 
    cur="${COMP_WORDS[COMP_CWORD]}"
42
 
    prev="${COMP_WORDS[COMP_CWORD-1]}"
43
 
 
44
 
    # Standalone options
45
 
    opts="--help --settings --pythonpath --noinput --noreload --format --indent --verbosity --adminmedia --version --locale --domain"
46
 
    # Actions
47
 
    actions="createcachetable createsuperuser compilemessages \
48
 
             dbshell diffsettings dumpdata flush inspectdb loaddata \
49
 
             makemessages reset runfcgi runserver shell sql sqlall sqlclear \
50
 
             sqlcustom sqlflush sqlindexes sqlreset sqlsequencereset startapp \
51
 
             startproject syncdb test validate"
52
 
    # Action's options
53
 
    action_shell_opts="--plain"
54
 
    action_runfcgi_opts="host port socket method maxspare minspare maxchildren daemonize pidfile workdir"
55
 
 
56
 
    if [[ # django-admin.py, django-admin, ./manage, manage.py
57
 
          ( ${COMP_CWORD} -eq 1 &&
58
 
            ( ${COMP_WORDS[0]} == django-admin.py ||
59
 
              ${COMP_WORDS[0]} == django-admin ||
60
 
              ${COMP_WORDS[0]} == ./manage.py ||
61
 
              ${COMP_WORDS[0]} == manage.py ) )
62
 
          ||
63
 
          # python manage.py, /some/path/python manage.py (if manage.py exists)
64
 
          ( ${COMP_CWORD} -eq 2 &&
65
 
            ( $( basename -- ${COMP_WORDS[0]} ) == python?([1-9]\.[0-9]) ) &&
66
 
            ( $( basename -- ${COMP_WORDS[1]} ) == manage.py) &&
67
 
            ( -r ${COMP_WORDS[1]} ) ) 
68
 
          ||
69
 
          ( ${COMP_CWORD} -eq 2 &&
70
 
            ( $( basename -- ${COMP_WORDS[0]} ) == python?([1-9]\.[0-9]) ) &&
71
 
            ( $( basename -- ${COMP_WORDS[1]} ) == django-admin.py) &&
72
 
            ( -r ${COMP_WORDS[1]} ) ) 
73
 
          ||
74
 
          ( ${COMP_CWORD} -eq 2 &&
75
 
            ( $( basename -- ${COMP_WORDS[0]} ) == python?([1-9]\.[0-9]) ) &&
76
 
            ( $( basename -- ${COMP_WORDS[1]} ) == django-admin) &&
77
 
            ( -r ${COMP_WORDS[1]} ) ) ]] ; then
78
 
 
79
 
        case ${cur} in
80
 
            -*)
81
 
                COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
82
 
                action=$COMPREPLY
83
 
                return 0
84
 
                ;;
85
 
            *)
86
 
                COMPREPLY=( $(compgen -W "${actions}" -- ${cur}) )
87
 
                action=$COMPREPLY
88
 
                return 0
89
 
                ;;
90
 
        esac
91
 
    else
92
 
        case ${prev} in
93
 
            dumpdata|reset| \
94
 
            sql|sqlall|sqlclear|sqlcustom|sqlindexes| \
95
 
            sqlreset|sqlsequencereset|test)
96
 
                # App completion
97
 
                settings=""
98
 
                # If settings.py in the PWD, use that
99
 
                if [ -e settings.py ] ; then
100
 
                    settings="$PWD/settings.py"
101
 
                else
102
 
                    # Use the ENV variable if it is set
103
 
                    if [ $DJANGO_SETTINGS_MODULE ] ; then
104
 
                        settings=$DJANGO_SETTINGS_MODULE
105
 
                    fi
106
 
                fi
107
 
                # Couldn't find settings so return nothing
108
 
                if [ -z $settings ] ; then
109
 
                    COMPREPLY=()
110
 
                # Otherwise inspect settings.py file
111
 
                else
112
 
                    apps=`sed -n "/INSTALLED_APPS = (/,/)/p" $settings | \
113
 
                          grep -v "django.contrib" | 
114
 
                          sed -n "s/^[ ]*'\(.*\.\)*\(.*\)'.*$/\2 /pg" | \
115
 
                          tr -d "\n"`
116
 
                    COMPREPLY=( $(compgen -W "${apps}" -- ${cur}) )
117
 
                fi
118
 
                return 0
119
 
                ;;
120
 
 
121
 
            createcachetable|cleanup|compilemessages|dbshell| \
122
 
            diffsettings|inspectdb|makemessages| \
123
 
            runserver|startapp|startproject|syncdb| \
124
 
            validate)
125
 
                COMPREPLY=()
126
 
                return 0
127
 
                ;;
128
 
            shell)
129
 
                COMPREPLY=( $(compgen -W "$action_shell_opts" -- ${cur}) )
130
 
                return 0
131
 
                ;;
132
 
            runfcgi)
133
 
                COMPREPLY=( $(compgen -W "$action_runfcgi_opts" -- ${cur}) )
134
 
                return 0
135
 
                ;;
136
 
            host*|port*|socket*|method*|maxspare*|minspare*|maxchildren*|daemonize*|pidfile*|workdir*)
137
 
                if [ "$action"  == "runfcgi" ] ; then
138
 
                    COMPREPLY=( $(compgen -W "$action_runfcgi_opts" -- ${cur}) )
139
 
                    return 0
140
 
                fi
141
 
                return 0
142
 
                ;;
143
 
            *)
144
 
                #COMPREPLY=( $(compgen -W "auth core" -- ${cur}) )
145
 
                COMPREPLY=()
146
 
                return 0
147
 
                ;;
148
 
        esac
 
36
    COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \
 
37
                   COMP_CWORD=$COMP_CWORD \
 
38
                       DJANGO_AUTO_COMPLETE=1 $1 ) )
 
39
}
 
40
complete -F _django_completion -o default django-admin.py manage.py django-admin
 
41
 
 
42
_python_django_completion()
 
43
{
 
44
    if [[ ${COMP_CWORD} -ge 2 ]]; then
 
45
        PYTHON_EXE=$( basename -- ${COMP_WORDS[0]} )
 
46
        echo $PYTHON_EXE | egrep "python([2-9]\.[0-9])?" >/dev/null 2>&1
 
47
        if [[ $? == 0 ]]; then
 
48
            PYTHON_SCRIPT=$( basename -- ${COMP_WORDS[1]} )
 
49
            echo $PYTHON_SCRIPT | egrep "manage\.py|django-admin(\.py)?" >/dev/null 2>&1
 
50
            if [[ $? == 0 ]]; then
 
51
                COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]:1}" \
 
52
                               COMP_CWORD=$(( COMP_CWORD-1 )) \
 
53
                               DJANGO_AUTO_COMPLETE=1 ${COMP_WORDS[*]} ) )
 
54
            fi
 
55
        fi
149
56
    fi
150
57
}
151
58
 
152
 
complete -F _django_completion django-admin.py manage.py django-admin
153
 
 
154
59
# Support for multiple interpreters.
155
60
unset pythons
156
61
if command -v whereis &>/dev/null; then
157
 
    python_interpreters=$(whereis python | cut -d " " -f 2-) 
 
62
    python_interpreters=$(whereis python | cut -d " " -f 2-)
158
63
    for python in $python_interpreters; do
159
64
        pythons="${pythons} $(basename -- $python)"
160
65
    done
161
66
    pythons=$(echo $pythons | tr " " "\n" | sort -u | tr "\n" " ")
162
67
else
163
 
    pythons=python    
 
68
    pythons=python
164
69
fi
165
70
 
166
 
complete -F _django_completion -o default $pythons
 
71
complete -F _python_django_completion -o default $pythons
 
72