Shortcuts to your code organized by type (views, models, admin, ...)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# encoding: utf-8

import os
import shutil

from django.core.management.base import NoArgsCommand

# given that a management command belongs in an app, the base dir containing all 
# apps is three levels down (/apps/some-app/management/commands/this-file)
APP_DIR = os.path.realpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../../'))
SHORTCUTS_DIR = './shortcuts'
TRACKED_FILENAMES = ["views", "models", "admin", "urls", "util", "ajax"]
TRACKED_DIRS = ["templates", "templatetags", "management"]

def ascertain_type_of_file(filename):
    if not filename.endswith('.py'):
        return None
        
    for type in TRACKED_FILENAMES:
        if filename.startswith(type):
            return type
                   
    return None

class Command(NoArgsCommand):
    help = """creates a bunch of symlinks in ./shortcuts to make browsing of your
    code by type (views, models, admin, ...) easier"""

    def handle_noargs(self, **options):
        # we recreate the directory every time this command is called
        # that way we're sure we'll never have any symlinks to files
        # that have since been deleted
        if os.path.exists(SHORTCUTS_DIR):
            shutil.rmtree(SHORTCUTS_DIR)
        os.mkdir(SHORTCUTS_DIR)
        for kind in TRACKED_FILENAMES + TRACKED_DIRS:
            pad = SHORTCUTS_DIR + '/' + kind
            os.mkdir(pad)
    
        # app code
        counter = 0
        for root, subdirs, files in os.walk(APP_DIR):
            for file in files:
                type = ascertain_type_of_file(file)
                ancestor, parent = root.split('/')[-2:]
                
                if type:
                    to = root + '/' + file
                    frm = '%s/%s/%s_%s' % (SHORTCUTS_DIR, type, parent, file)
                    os.symlink(to, frm)
                    counter += 1
    
                if parent in TRACKED_DIRS:
                    to = root
                    frm = SHORTCUTS_DIR + '/' + parent + '/' + ancestor
                    if not os.path.exists(frm):
                        os.symlink(to, frm)
                        counter += 1
        
        print "Made %i shortcuts in %i directories under ./shortcuts" % (counter, len(TRACKED_DIRS))

More like this

  1. backupdb command by msaelices 5 years, 9 months ago
  2. Automatically generate admin by WoLpH 1 year, 4 months ago
  3. Quiet runserver by danielroseman 3 years, 10 months ago
  4. Custom Command for Rebuilding Permissions and ContentTypes by cronosa 3 years ago
  5. Facebook shell by stephenemslie 4 years, 7 months ago

Comments

davedash (on March 7, 2010):

Hmm... am I missing something, or is this any different than using zsh and doing:

**/admin.py

**/views.py

**/models.py

**/ajax.py

etc?

#

stdbrouw (on March 7, 2010):

@davedash: if you're coding from the commandline, probably not, but if you use an editor with a project browser, the symlinks in the /shortcuts directory make for easier browsing. It's not about finding files per type, it's about having them all one click away.

#

(Forgotten your password?)