1
# Copyright (C) 2008 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
Views are contained within a working tree and normally constructed
20
when first accessed. Clients should do, for example, ...
22
tree.views.lookup_view()
33
_VIEWS_FORMAT_MARKER_RE = re.compile(r'Bazaar views format (\d+)')
34
_VIEWS_FORMAT1_MARKER = "Bazaar views format 1\n"
38
"""Base class for View managers."""
40
def supports_views(self):
41
raise NotImplementedError(self.supports_views)
44
class PathBasedViews(_Views):
45
"""View storage in an unversioned tree control file.
47
Views are stored in terms of paths relative to the tree root.
49
The top line of the control file is a format marker in the format:
53
where X is an integer number. Version 1 format is stored as follows:
55
* the line after the format marker holds the name of the current view
57
* subsequent lines hold view definitions, one per line is the format
61
where the fields are separated by a nul character (\0). The views file
65
def __init__(self, tree):
71
def supports_views(self):
74
def get_view_info(self):
75
"""Get the current view and dictionary of views.
77
:return: current, views where
78
current = the name of the current view or None if no view is enabled
79
views = a map from view name to list of files/directories
81
self._load_view_info()
82
return self._current, self._views
84
def set_view_info(self, current, views):
85
"""Set the current view and dictionary of views.
87
:param current: the name of the current view or None if no view is
89
:param views: a map from view name to list of files/directories
91
if current is not None and current not in views:
92
raise errors.NoSuchView(current)
93
self.tree.lock_write()
95
self._current = current
97
self._save_view_info()
101
def lookup_view(self, view_name=None):
102
"""Return the contents of a view.
104
:param view_Name: name of the view or None to lookup the current view
105
:return: the list of files/directories in the requested view
107
self._load_view_info()
109
if view_name is None:
111
view_name = self._current
114
return self._views[view_name]
116
raise errors.NoSuchView(view_name)
118
def set_view(self, view_name, view_files, make_current=True):
119
"""Add or update a view definition.
121
:param view_name: the name of the view
122
:param view_files: the list of files/directories in the view
123
:param make_current: make this view the current one or not
125
self.tree.lock_write()
127
self._load_view_info()
128
self._views[view_name] = view_files
130
self._current = view_name
131
self._save_view_info()
135
def delete_view(self, view_name):
136
"""Delete a view definition.
138
If the view deleted is the current one, the current view is reset.
140
self.tree.lock_write()
142
self._load_view_info()
144
del self._views[view_name]
146
raise errors.NoSuchView(view_name)
147
if view_name == self._current:
149
self._save_view_info()
153
def _save_view_info(self):
154
"""Save the current view and all view definitions.
156
Be sure to have initialised self._current and self._views before
159
self.tree.lock_write()
161
self.tree._transport.put_bytes('views',
162
self._serialize_view_content(self._current, self._views))
166
def _load_view_info(self):
167
"""Load the current view and dictionary of view definitions."""
169
self.tree.lock_read()
172
view_content = self.tree._transport.get_bytes('views')
173
except errors.NoSuchFile, e:
174
self._current, self._views = None, {}
176
self._current, self._views = \
177
self._deserialize_view_content(view_content)
182
def _serialize_view_content(self, current, view_dict):
183
"""Convert a current view and view dictionary into a stream."""
184
lines = [_VIEWS_FORMAT1_MARKER]
188
lines.append((current + "\n").encode('utf-8'))
189
for view in sorted(view_dict):
190
view_data = "%s\0%s\n" % (view, "\0".join(view_dict[view]))
191
lines.append(view_data.encode('utf-8'))
192
return "".join(lines)
194
def _deserialize_view_content(self, view_content):
195
"""Convert a stream into a current view and dictionary of views."""
196
# as a special case to make initialization easy, an empty definition
197
# maps to no current view and an empty view dictionary
198
if view_content == '':
200
lines = view_content.splitlines()
201
match = _VIEWS_FORMAT_MARKER_RE.match(lines[0])
204
"format marker missing from top of views file")
205
elif match.group(1) != '1':
207
"cannot decode views format %s" % match.group(1))
209
current = lines[1].decode('utf-8')
213
for line in lines[2:]:
214
parts = line.decode('utf-8').split('\0')
217
return current, views
218
except ValueError, e:
219
raise ValueError("failed to deserialize views content %r: %s"
223
class DisabledViews(_Views):
224
"""View storage that refuses to store anything.
226
This is used by older formats that can't store views.
229
def __init__(self, tree):
232
def supports_views(self):
235
def _not_supported(self, *a, **k):
236
raise errors.ViewsNotSupported(self.tree)
238
get_view_info = _not_supported
239
set_view_info = _not_supported
240
lookup_view = _not_supported
241
set_view = _not_supported
242
delete_view = _not_supported