/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/views.py

  • Committer: Jelmer Vernooij
  • Date: 2018-02-18 19:18:40 UTC
  • mto: This revision was merged to the branch mainline in revision 6928.
  • Revision ID: jelmer@jelmer.uk-20180218191840-2wezg20u9ffbfmed
Fix more bees, use with rather than try/finally for some files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
  tree.views.lookup_view()
23
23
"""
24
24
 
 
25
from __future__ import absolute_import
25
26
 
26
27
import re
27
28
 
28
 
from bzrlib import (
 
29
from . import (
29
30
    errors,
30
31
    osutils,
31
32
    )
35
36
_VIEWS_FORMAT1_MARKER = "Bazaar views format 1\n"
36
37
 
37
38
 
 
39
class NoSuchView(errors.BzrError):
 
40
    """A view does not exist.
 
41
    """
 
42
 
 
43
    _fmt = u"No such view: %(view_name)s."
 
44
 
 
45
    def __init__(self, view_name):
 
46
        self.view_name = view_name
 
47
 
 
48
 
 
49
class ViewsNotSupported(errors.BzrError):
 
50
    """Views are not supported by a tree format.
 
51
    """
 
52
 
 
53
    _fmt = ("Views are not supported by %(tree)s;"
 
54
            " use 'brz upgrade' to change your tree to a later format.")
 
55
 
 
56
    def __init__(self, tree):
 
57
        self.tree = tree
 
58
 
 
59
 
 
60
class FileOutsideView(errors.BzrError):
 
61
 
 
62
    _fmt = ('Specified file "%(file_name)s" is outside the current view: '
 
63
            '%(view_str)s')
 
64
 
 
65
    def __init__(self, file_name, view_files):
 
66
        self.file_name = file_name
 
67
        self.view_str = ", ".join(view_files)
 
68
 
 
69
 
38
70
class _Views(object):
39
71
    """Base class for View managers."""
40
72
 
94
126
        :param views: a map from view name to list of files/directories
95
127
        """
96
128
        if current is not None and current not in views:
97
 
            raise errors.NoSuchView(current)
 
129
            raise NoSuchView(current)
98
130
        self.tree.lock_write()
99
131
        try:
100
132
            self._current = current
118
150
                    return []
119
151
            return self._views[view_name]
120
152
        except KeyError:
121
 
            raise errors.NoSuchView(view_name)
 
153
            raise NoSuchView(view_name)
122
154
 
123
155
    def set_view(self, view_name, view_files, make_current=True):
124
156
        """Add or update a view definition.
148
180
            try:
149
181
                del self._views[view_name]
150
182
            except KeyError:
151
 
                raise errors.NoSuchView(view_name)
 
183
                raise NoSuchView(view_name)
152
184
            if view_name == self._current:
153
185
                self._current = None
154
186
            self._save_view_info()
175
207
    def _load_view_info(self):
176
208
        """Load the current view and dictionary of view definitions."""
177
209
        if not self._loaded:
178
 
            self.tree.lock_read()
179
 
            try:
 
210
            with self.tree.lock_read():
180
211
                try:
181
212
                    view_content = self.tree._transport.get_bytes('views')
182
 
                except errors.NoSuchFile, e:
 
213
                except errors.NoSuchFile as e:
183
214
                    self._current, self._views = None, {}
184
215
                else:
185
216
                    keywords, self._views = \
186
217
                        self._deserialize_view_content(view_content)
187
218
                    self._current = keywords.get('current')
188
 
            finally:
189
 
                self.tree.unlock()
190
219
            self._loaded = True
191
220
 
192
221
    def _serialize_view_content(self, keywords, view_dict):
193
222
        """Convert view keywords and a view dictionary into a stream."""
194
223
        lines = [_VIEWS_FORMAT1_MARKER]
195
224
        for key in keywords:
196
 
            line = "%s=%s\n" % (key,keywords[key])
 
225
            line = "%s=%s\n" % (key, keywords[key])
197
226
            lines.append(line.encode('utf-8'))
198
227
        if view_dict:
199
228
            lines.append("views:\n".encode('utf-8'))
237
266
                    raise ValueError("failed to deserialize views line %s",
238
267
                        text)
239
268
            return keywords, views
240
 
        except ValueError, e:
 
269
        except ValueError as e:
241
270
            raise ValueError("failed to deserialize views content %r: %s"
242
271
                % (view_content, e))
243
272
 
255
284
        return False
256
285
 
257
286
    def _not_supported(self, *a, **k):
258
 
        raise errors.ViewsNotSupported(self.tree)
 
287
        raise ViewsNotSupported(self.tree)
259
288
 
260
289
    get_view_info = _not_supported
261
290
    set_view_info = _not_supported
281
310
    if tree.supports_views():
282
311
        view_files = tree.views.lookup_view()
283
312
        if  view_files and not osutils.is_inside_any(view_files, relpath):
284
 
            raise errors.FileOutsideView(relpath, view_files)
 
313
            raise FileOutsideView(relpath, view_files)