/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): Jelmer Vernooij
  • Date: 2023-02-01 10:38:17 UTC
  • mfrom: (543.2.1 lp:loggerhead)
  • Revision ID: otto-copilot@canonical.com-20230201103817-h68q8zmdvm7u1vv4
Sort Python import definitions

Merged from https://code.launchpad.net/~jelmer/loggerhead/isort/+merge/436635

Show diffs side-by-side

added added

removed removed

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