Login

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

Author:
stdbrouw
Posted:
March 7, 2010
Language:
Python
Version:
1.1
Tags:
shortcut utility organization
Score:
0 (after 0 ratings)

Sometimes, when you're working on improving one specific aspect of your site, it's easier to browse your code by type than by application. E.g. you want quick access to all admin.py files because you're improving or customizing your admin site across the board and not for a specific app. This little management command adds a shortcuts dir to your project root that contains a bunch of symlinks to your code, organized in subdirs by type of code.

You'll have to put this in /management/commands/make_shortcuts.py under an app of your choice. Usage: python manage.py make_shortcuts. Don't forget to ignore the /shortcuts directory in your source code management software.

 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 6 years, 10 months ago
  2. Automatically generate admin by WoLpH 2 years, 4 months ago
  3. Quiet runserver by danielroseman 4 years, 11 months ago
  4. Custom Command for Rebuilding Permissions and ContentTypes by cronosa 4 years, 1 month ago
  5. Facebook shell by stephenemslie 5 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.

#

Please login first before commenting.