/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: Ubuntu One Auto Copilot
  • Author(s): Colin Watson
  • Date: 2022-09-21 12:08:21 UTC
  • mfrom: (539.1.2 fix-tox)
  • Revision ID: otto-copilot@canonical.com-20220921120821-fan53lzjaborq3h1
Fix running tests in tox.

Merged from https://code.launchpad.net/~cjwatson/loggerhead/fix-tox/+merge/430053

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
    NoSuchId,
25
25
    NoSuchRevision,
26
26
    )
 
27
try:
 
28
    from breezy.transport import NoSuchFile
 
29
except ImportError:
 
30
    from breezy.errors import NoSuchFile
 
31
from breezy import (
 
32
    osutils,
 
33
    urlutils,
 
34
    )
27
35
import breezy.textfile
28
 
import breezy.osutils
29
36
 
30
37
from paste.httpexceptions import (
31
38
    HTTPBadRequest,
32
39
    HTTPMovedPermanently,
33
40
    HTTPNotFound,
34
 
    HTTPServerError,
35
41
    )
36
42
 
37
 
from loggerhead.controllers import TemplatedBranchView
 
43
from ..controllers import TemplatedBranchView
38
44
try:
39
 
    from loggerhead.highlight import highlight
 
45
    from ..highlight import highlight
40
46
except ImportError:
41
47
    highlight = None
42
 
from loggerhead import util
 
48
from .. import util
43
49
 
44
50
 
45
51
class ViewUI(TemplatedBranchView):
46
52
 
47
 
    template_path = 'loggerhead.templates.view'
48
 
    
49
 
    def tree_for(self, file_id, revid):
50
 
        file_revid = self._history.get_inventory(revid)[file_id].revision
51
 
        return self._history._branch.repository.revision_tree(file_revid)
52
 
 
53
 
    def text_lines(self, file_id, revid):
54
 
        file_name = os.path.basename(self._history.get_path(revid, file_id))
55
 
        
56
 
        tree = self.tree_for(file_id, revid)
57
 
        file_text = tree.get_file_text(file_id)
 
53
    template_name = 'view'
 
54
 
 
55
    def tree_for(self, path, revid):
 
56
        if not isinstance(path, str):
 
57
            raise TypeError(path)
 
58
        if not isinstance(revid, bytes):
 
59
            raise TypeError(revid)
 
60
        return self._history._branch.repository.revision_tree(revid)
 
61
 
 
62
    def text_lines(self, path, revid):
 
63
        file_name = os.path.basename(path)
 
64
 
 
65
        tree = self.tree_for(path, revid)
 
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)
 
73
            file_text.decode(encoding)
64
74
 
65
 
        file_lines = breezy.osutils.split_lines(file_text)
 
75
        file_lines = osutils.split_lines(file_text)
66
76
        # This can throw breezy.errors.BinaryFile (which our caller catches).
67
77
        breezy.textfile.check_text_lines(file_lines)
68
 
        
 
78
 
 
79
        file_text = file_text.decode(encoding)
 
80
        file_lines = osutils.split_lines(file_text)
 
81
 
69
82
        if highlight is not None:
70
83
            hl_lines = highlight(file_name, file_text, encoding)
71
84
            # highlight strips off extra newlines at the end of the file.
72
85
            extra_lines = len(file_lines) - len(hl_lines)
73
86
            hl_lines.extend([u''] * extra_lines)
74
87
        else:
75
 
            hl_lines = map(util.html_escape, file_lines)
76
 
        
77
 
        return hl_lines;
78
 
 
79
 
    def file_contents(self, file_id, revid):
 
88
            hl_lines = [util.html_escape(line) for line in file_lines]
 
89
 
 
90
        return hl_lines
 
91
 
 
92
    def file_contents(self, path, revid):
80
93
        try:
81
 
            file_lines = self.text_lines(file_id, revid)
 
94
            file_lines = self.text_lines(path, revid)
82
95
        except BinaryFile:
83
96
            # bail out; this isn't displayable text
84
97
            return ['(This is a binary file.)']
89
102
        history = self._history
90
103
        branch = history._branch
91
104
        revid = self.get_revid()
92
 
        revid = history.fix_revid(revid)
93
 
        file_id = kwargs.get('file_id', None)
94
 
        if (file_id is None) and (path is None):
95
 
            raise HTTPBadRequest('No file_id or filename '
96
 
                                 'provided to view')
 
105
        if path is None:
 
106
            raise HTTPBadRequest('No filename provided to view')
97
107
 
98
 
        try:
99
 
            if file_id is None:
100
 
                file_id = history.get_file_id(revid, path)
101
 
            if path is None:
102
 
                path = history.get_path(revid, file_id)
103
 
        except (NoSuchId, NoSuchRevision):
 
108
        if not history.file_exists(revid, path):
104
109
            raise HTTPNotFound()
105
110
 
106
111
        filename = os.path.basename(path)
119
124
                self._branch.is_root,
120
125
                'files'))
121
126
 
 
127
        tree = history.revision_tree(revid)
 
128
 
122
129
        # 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')
 
130
        branch_breadcrumbs = util.branch_breadcrumbs(path, tree, 'files')
129
131
 
130
132
        try:
131
 
            file = inv[file_id]
132
 
        except NoSuchId:
 
133
            if tree.kind(path) == "directory":
 
134
                raise HTTPMovedPermanently(
 
135
                    self._branch.context_url(['/files', revno_url, path]))
 
136
        except NoSuchFile:
133
137
            raise HTTPNotFound()
134
138
 
135
 
        if file.kind == "directory":
136
 
            raise HTTPMovedPermanently(self._branch.context_url(['/files', revno_url, path]))
137
 
 
138
139
        # no navbar for revisions
139
140
        navigation = util.Container()
140
141
 
141
142
        return {
142
 
            # In AnnotateUI, "annotated" is a dictionary mapping lines to changes.
143
 
            # We exploit the fact that bool({}) is False when checking whether
144
 
            # we're in "annotated" mode.
 
143
            # In AnnotateUI, "annotated" is a dictionary mapping lines to
 
144
            # changes.  We exploit the fact that bool({}) is False when
 
145
            # checking whether we're in "annotated" mode.
145
146
            'annotated': {},
146
147
            'revno_url': revno_url,
147
 
            'file_id': file_id,
148
148
            'file_path': path,
149
149
            'filename': filename,
150
150
            'navigation': navigation,
151
151
            'change': change,
152
 
            'contents':  self.file_contents(file_id, revid),
 
152
            'contents':  self.file_contents(path, revid),
153
153
            'fileview_active': True,
154
154
            'directory_breadcrumbs': directory_breadcrumbs,
155
155
            'branch_breadcrumbs': branch_breadcrumbs,