/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/controllers/view_ui.py

  • Committer: Colin Watson
  • Date: 2019-09-19 08:10:36 UTC
  • mfrom: (491.2.62 breezy)
  • Revision ID: cjwatson@canonical.com-20190919081036-q1symc2h2iedtlh3
[r=cjwatson] Switch loggerhead over to using the Breezy rather than Bazaar APIs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
import os
21
21
 
22
 
from bzrlib.errors import (
 
22
from breezy.errors import (
23
23
    BinaryFile,
24
24
    NoSuchId,
25
25
    NoSuchRevision,
26
26
    )
27
 
import bzrlib.textfile
28
 
import bzrlib.osutils
 
27
from breezy import (
 
28
    osutils,
 
29
    urlutils,
 
30
    )
 
31
import breezy.textfile
29
32
 
30
33
from paste.httpexceptions import (
31
34
    HTTPBadRequest,
34
37
    HTTPServerError,
35
38
    )
36
39
 
37
 
from loggerhead.controllers import TemplatedBranchView
 
40
from ..controllers import TemplatedBranchView
38
41
try:
39
 
    from loggerhead.highlight import highlight
 
42
    from ..highlight import highlight
40
43
except ImportError:
41
44
    highlight = None
42
 
from loggerhead import util
 
45
from .. import util
43
46
 
44
47
 
45
48
class ViewUI(TemplatedBranchView):
46
49
 
47
 
    template_path = 'loggerhead.templates.view'
48
 
    
 
50
    template_name = 'view'
 
51
 
49
52
    def tree_for(self, file_id, revid):
50
 
        file_revid = self._history.get_inventory(revid)[file_id].revision
 
53
        if not isinstance(file_id, bytes):
 
54
            raise TypeError(file_id)
 
55
        if not isinstance(revid, bytes):
 
56
            raise TypeError(revid)
 
57
        rev_tree = self._history.revision_tree(revid)
 
58
        file_revid = rev_tree.get_file_revision(rev_tree.id2path(file_id))
51
59
        return self._history._branch.repository.revision_tree(file_revid)
52
60
 
53
61
    def text_lines(self, file_id, revid):
54
 
        file_name = os.path.basename(self._history.get_path(revid, file_id))
55
 
        
 
62
        path = self._history.get_path(revid, file_id)
 
63
        file_name = os.path.basename(path)
 
64
 
56
65
        tree = self.tree_for(file_id, revid)
57
 
        file_text = tree.get_file_text(file_id)
 
66
        file_text = tree.get_file_text(path)
 
67
 
58
68
        encoding = 'utf-8'
59
69
        try:
60
 
            file_text = file_text.decode(encoding)
 
70
            file_text.decode(encoding)
61
71
        except UnicodeDecodeError:
62
72
            encoding = 'iso-8859-15'
63
 
            file_text = file_text.decode(encoding)
64
 
 
65
 
        file_lines = bzrlib.osutils.split_lines(file_text)
66
 
        # This can throw bzrlib.errors.BinaryFile (which our caller catches).
67
 
        bzrlib.textfile.check_text_lines(file_lines)
68
 
        
 
73
            file_text.decode(encoding)
 
74
 
 
75
        file_lines = osutils.split_lines(file_text)
 
76
        # This can throw breezy.errors.BinaryFile (which our caller catches).
 
77
        breezy.textfile.check_text_lines(file_lines)
 
78
 
 
79
        file_text = file_text.decode(encoding)
 
80
 
69
81
        if highlight is not None:
70
82
            hl_lines = highlight(file_name, file_text, encoding)
71
83
            # highlight strips off extra newlines at the end of the file.
73
85
            hl_lines.extend([u''] * extra_lines)
74
86
        else:
75
87
            hl_lines = map(util.html_escape, file_lines)
76
 
        
77
 
        return hl_lines;
 
88
 
 
89
        return hl_lines
78
90
 
79
91
    def file_contents(self, file_id, revid):
80
92
        try:
89
101
        history = self._history
90
102
        branch = history._branch
91
103
        revid = self.get_revid()
92
 
        revid = history.fix_revid(revid)
93
104
        file_id = kwargs.get('file_id', None)
 
105
        if file_id is not None:
 
106
            file_id = urlutils.unquote_to_bytes(osutils.safe_utf8(file_id))
94
107
        if (file_id is None) and (path is None):
95
108
            raise HTTPBadRequest('No file_id or filename '
96
109
                                 'provided to view')
119
132
                self._branch.is_root,
120
133
                'files'))
121
134
 
 
135
        tree = history.revision_tree(revid)
 
136
 
122
137
        # Create breadcrumb trail for the path within the branch
123
 
        try:
124
 
            inv = history.get_inventory(revid)
125
 
        except:
126
 
            self.log.exception('Exception fetching changes')
127
 
            raise HTTPServerError('Could not fetch changes')
128
 
        branch_breadcrumbs = util.branch_breadcrumbs(path, inv, 'files')
 
138
        branch_breadcrumbs = util.branch_breadcrumbs(path, tree, 'files')
129
139
 
130
 
        try:
131
 
            file = inv[file_id]
132
 
        except NoSuchId:
 
140
        if not tree.has_id(file_id):
133
141
            raise HTTPNotFound()
134
142
 
135
 
        if file.kind == "directory":
 
143
        if tree.kind(path) == "directory":
136
144
            raise HTTPMovedPermanently(self._branch.context_url(['/files', revno_url, path]))
137
145
 
138
146
        # no navbar for revisions