~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/annotate_ui.py

  • Committer: Robey Pointer
  • Date: 2006-12-24 06:44:26 UTC
  • Revision ID: robey@lag.net-20061224064426-bedaid2zrw69igfg
initial homepage

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
#
19
19
 
20
 
import cgi
 
20
import datetime
 
21
import logging
21
22
import os
 
23
import posixpath
 
24
import textwrap
22
25
import time
23
26
 
24
 
import bzrlib.errors
25
 
import bzrlib.textfile
26
 
import bzrlib.osutils
27
 
 
28
 
from paste.httpexceptions import HTTPBadRequest, HTTPServerError
29
 
 
30
 
from loggerhead.controllers import TemplatedBranchView
31
 
try:
32
 
    from loggerhead.highlight import highlight
33
 
except ImportError:
34
 
    highlight = None
 
27
import turbogears
 
28
from cherrypy import HTTPRedirect, session
 
29
 
35
30
from loggerhead import util
36
31
 
37
32
 
38
 
class AnnotateUI(TemplatedBranchView):
39
 
 
40
 
    template_path = 'loggerhead.templates.annotate'
41
 
 
42
 
    def annotate_file(self, file_id, revid):
 
33
log = logging.getLogger("loggerhead.controllers")
 
34
 
 
35
def dirname(path):
 
36
    while path.endswith('/'):
 
37
        path = path[:-1]
 
38
    path = posixpath.dirname(path)
 
39
    return path
 
40
 
 
41
        
 
42
class AnnotateUI (object):
 
43
 
 
44
    def __init__(self, branch):
 
45
        # BranchView object
 
46
        self._branch = branch
 
47
        self.log = branch.log
 
48
 
 
49
    @turbogears.expose(html='loggerhead.templates.annotate')
 
50
    def default(self, *args, **kw):
43
51
        z = time.time()
44
 
        lineno = 1
45
 
        parity = 0
46
 
 
47
 
        file_revid = self._history.get_inventory(revid)[file_id].revision
48
 
        tree = self._history._branch.repository.revision_tree(file_revid)
49
 
 
50
 
        file_name = os.path.basename(self._history.get_path(revid, file_id))
51
 
 
52
 
        file_text = tree.get_file_text(file_id)
53
 
        encoding = 'utf-8'
54
 
        try:
55
 
            file_text = file_text.decode(encoding)
56
 
        except UnicodeDecodeError:
57
 
            encoding = 'iso-8859-15'
58
 
            file_text = file_text.decode(encoding)
59
 
 
60
 
        file_lines = bzrlib.osutils.split_lines(file_text)
61
 
 
62
 
        try:
63
 
            bzrlib.textfile.check_text_lines(file_lines)
64
 
        except bzrlib.errors.BinaryFile:
65
 
                # bail out; this isn't displayable text
66
 
                yield util.Container(parity=0, lineno=1, status='same',
67
 
                                     text='(This is a binary file.)',
68
 
                                     change=util.Container())
 
52
        h = self._branch.get_history()
 
53
        
 
54
        if len(args) > 0:
 
55
            revid = h.fix_revid(args[0])
69
56
        else:
70
 
            if highlight is not None:
71
 
                hl_lines = highlight(file_name, file_text, encoding)
72
 
                hl_lines.extend([u''] * (len(file_lines) - len(hl_lines)))
73
 
            else:
74
 
                hl_lines = map(cgi.escape, file_lines)
75
 
 
76
 
            change_cache = {}
77
 
 
78
 
            last_line_revid = None
79
 
            for line_revid, text in tree.annotate_iter(file_id):
80
 
                if line_revid == last_line_revid:
81
 
                    # remember which lines have a new revno and which don't
82
 
                    status = 'same'
83
 
                else:
84
 
                    status = 'changed'
85
 
                    parity ^= 1
86
 
                    last_line_revid = line_revid
87
 
                    if line_revid in change_cache:
88
 
                        change = change_cache[line_revid]
89
 
                    else:
90
 
                        change = self._history.get_changes([line_revid])[0]
91
 
                        change_cache[line_revid] = change
92
 
 
93
 
                yield util.Container(
94
 
                    parity=parity, lineno=lineno, status=status,
95
 
                    change=change, text=hl_lines[lineno - 1])
96
 
                lineno += 1
97
 
 
98
 
        self.log.debug('annotate: %r secs' % (time.time() - z))
99
 
 
100
 
    def get_values(self, path, kwargs, headers):
101
 
        history = self._history
102
 
        branch = history._branch
103
 
        revid = self.get_revid()
104
 
        revid = history.fix_revid(revid)
105
 
        file_id = kwargs.get('file_id', None)
106
 
        if (file_id is None) and (path is None):
107
 
            raise HTTPBadRequest('No file_id or filename '
108
 
                                 'provided to annotate')
109
 
 
 
57
            revid = None
 
58
        
 
59
        file_id = kw.get('file_id', None)
110
60
        if file_id is None:
111
 
            file_id = history.get_file_id(revid, path)
 
61
            raise HTTPRedirect(self._branch.url('/changes'))
112
62
 
 
63
        try:
 
64
            revid_list, revid = h.get_file_view(revid, file_id)
 
65
        except Exception, x:
 
66
            self.log.error('Exception fetching changes: %s' % (x,))
 
67
            util.log_exception(self.log)
 
68
            raise HTTPRedirect(self._branch.url('/changes'))
 
69
            
113
70
        # no navbar for revisions
114
71
        navigation = util.Container()
115
 
 
116
 
        if path is None:
117
 
            path = history.get_path(revid, file_id)
 
72
        
 
73
        path = h.get_path(revid, file_id)
118
74
        filename = os.path.basename(path)
119
75
 
120
 
        change = history.get_changes([ revid ])[0]
121
 
        # If we're looking at the tip, use head: in the URL instead
122
 
        if revid == branch.last_revision():
123
 
            revno_url = 'head:'
124
 
        else:
125
 
            revno_url = history.get_revno(revid)
126
 
 
127
 
        # Directory Breadcrumbs
128
 
        directory_breadcrumbs = (
129
 
            util.directory_breadcrumbs(
130
 
                self._branch.friendly_name,
131
 
                self._branch.is_root,
132
 
                'files'))
133
 
 
134
 
        # Create breadcrumb trail for the path within the branch
135
 
        try:
136
 
            inv = history.get_inventory(revid)
137
 
        except:
138
 
            self.log.exception('Exception fetching changes')
139
 
            raise HTTPServerError('Could not fetch changes')
140
 
        branch_breadcrumbs = util.branch_breadcrumbs(path, inv, 'files')
141
 
 
142
 
        return {
143
 
            'revno_url': revno_url,
 
76
        vals = {
 
77
            'branch': self._branch,
 
78
            'util': util,
 
79
            'revid': revid,
144
80
            'file_id': file_id,
145
81
            'path': path,
146
82
            'filename': filename,
 
83
            'history': h,
147
84
            'navigation': navigation,
148
 
            'change': change,
149
 
            'contents': list(self.annotate_file(file_id, revid)),
150
 
            'fileview_active': True,
151
 
            'directory_breadcrumbs': directory_breadcrumbs,
152
 
            'branch_breadcrumbs': branch_breadcrumbs,
 
85
            'change': h.get_changes([ revid ])[0],
 
86
            'contents': list(h.annotate_file(file_id, revid)),
153
87
        }
 
88
        h.flush_cache()
 
89
        self.log.info('/annotate: %r secs' % (time.time() - z,))
 
90
        return vals