22
22
tree.views.lookup_view()
25
from __future__ import absolute_import
34
_VIEWS_FORMAT_MARKER_RE = re.compile(r'Bazaar views format (\d+)')
35
_VIEWS_FORMAT1_MARKER = "Bazaar views format 1\n"
35
_VIEWS_FORMAT_MARKER_RE = re.compile(b'Bazaar views format (\\d+)')
36
_VIEWS_FORMAT1_MARKER = b"Bazaar views format 1\n"
39
class NoSuchView(errors.BzrError):
40
"""A view does not exist.
43
_fmt = u"No such view: %(view_name)s."
45
def __init__(self, view_name):
46
self.view_name = view_name
49
class ViewsNotSupported(errors.BzrError):
50
"""Views are not supported by a tree format.
53
_fmt = ("Views are not supported by %(tree)s;"
54
" use 'brz upgrade' to change your tree to a later format.")
56
def __init__(self, tree):
60
class FileOutsideView(errors.BzrError):
62
_fmt = ('Specified file "%(file_name)s" is outside the current view: '
65
def __init__(self, file_name, view_files):
66
self.file_name = file_name
67
self.view_str = ", ".join(view_files)
38
70
class _Views(object):
94
126
:param views: a map from view name to list of files/directories
96
128
if current is not None and current not in views:
97
raise errors.NoSuchView(current)
98
self.tree.lock_write()
129
raise NoSuchView(current)
130
with self.tree.lock_write():
100
131
self._current = current
101
132
self._views = views
102
133
self._save_view_info()
106
135
def lookup_view(self, view_name=None):
107
136
"""Return the contents of a view.
119
148
return self._views[view_name]
121
raise errors.NoSuchView(view_name)
150
raise NoSuchView(view_name)
123
152
def set_view(self, view_name, view_files, make_current=True):
124
153
"""Add or update a view definition.
127
156
:param view_files: the list of files/directories in the view
128
157
:param make_current: make this view the current one or not
130
self.tree.lock_write()
159
with self.tree.lock_write():
132
160
self._load_view_info()
133
161
self._views[view_name] = view_files
135
163
self._current = view_name
136
164
self._save_view_info()
140
166
def delete_view(self, view_name):
141
167
"""Delete a view definition.
143
169
If the view deleted is the current one, the current view is reset.
145
self.tree.lock_write()
171
with self.tree.lock_write():
147
172
self._load_view_info()
149
174
del self._views[view_name]
151
raise errors.NoSuchView(view_name)
176
raise NoSuchView(view_name)
152
177
if view_name == self._current:
153
178
self._current = None
154
179
self._save_view_info()
158
181
def _save_view_info(self):
159
182
"""Save the current view and all view definitions.
161
184
Be sure to have initialised self._current and self._views before
162
185
calling this method.
164
self.tree.lock_write()
187
with self.tree.lock_write():
166
188
if self._current is None:
169
191
keywords = {'current': self._current}
170
self.tree._transport.put_bytes('views',
171
self._serialize_view_content(keywords, self._views))
192
self.tree._transport.put_bytes(
193
'views', self._serialize_view_content(keywords, self._views))
175
195
def _load_view_info(self):
176
196
"""Load the current view and dictionary of view definitions."""
177
197
if not self._loaded:
178
self.tree.lock_read()
198
with self.tree.lock_read():
181
200
view_content = self.tree._transport.get_bytes('views')
182
except errors.NoSuchFile, e:
201
except errors.NoSuchFile:
183
202
self._current, self._views = None, {}
185
204
keywords, self._views = \
186
205
self._deserialize_view_content(view_content)
187
206
self._current = keywords.get('current')
190
207
self._loaded = True
192
209
def _serialize_view_content(self, keywords, view_dict):
193
210
"""Convert view keywords and a view dictionary into a stream."""
194
211
lines = [_VIEWS_FORMAT1_MARKER]
195
212
for key in keywords:
196
line = "%s=%s\n" % (key,keywords[key])
213
line = "%s=%s\n" % (key, keywords[key])
197
214
lines.append(line.encode('utf-8'))
199
216
lines.append("views:\n".encode('utf-8'))
200
217
for view in sorted(view_dict):
201
218
view_data = "%s\0%s\n" % (view, "\0".join(view_dict[view]))
202
219
lines.append(view_data.encode('utf-8'))
203
return "".join(lines)
220
return b"".join(lines)
205
222
def _deserialize_view_content(self, view_content):
206
223
"""Convert a stream into view keywords and a dictionary of views."""
207
224
# as a special case to make initialization easy, an empty definition
208
225
# maps to no current view and an empty view dictionary
209
if view_content == '':
226
if view_content == b'':
211
228
lines = view_content.splitlines()
212
229
match = _VIEWS_FORMAT_MARKER_RE.match(lines[0])
214
231
raise ValueError(
215
232
"format marker missing from top of views file")
216
elif match.group(1) != '1':
233
elif match.group(1) != b'1':
217
234
raise ValueError(
218
235
"cannot decode views format %s" % match.group(1))
257
274
def _not_supported(self, *a, **k):
258
raise errors.ViewsNotSupported(self.tree)
275
raise ViewsNotSupported(self.tree)
260
277
get_view_info = _not_supported
261
278
set_view_info = _not_supported
280
297
"""If a working tree has a view enabled, check the path is within it."""
281
298
if tree.supports_views():
282
299
view_files = tree.views.lookup_view()
283
if view_files and not osutils.is_inside_any(view_files, relpath):
284
raise errors.FileOutsideView(relpath, view_files)
300
if view_files and not osutils.is_inside_any(view_files, relpath):
301
raise FileOutsideView(relpath, view_files)