1
# (C) 2005 Canonical Development 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
17
"""Serializer factory for reading and writing changesets.
22
import bzrlib.errors as errors
24
# New changesets should try to use this header format
25
CHANGESET_HEADER = '# Bazaar changeset v'
26
CHANGESET_HEADER_RE = re.compile(r'^# Bazaar changeset v(?P<version>\d+[\w.]*)\n$')
27
CHANGESET_OLD_HEADER_RE = re.compile(r'^# Bazaar-NG changeset v(?P<version>\d+[\w.]*)\n$')
34
if hasattr(f, 'name'):
40
"""Read in a changeset from a filelike object.
42
:param f: A file-like object
43
:return: A list of Changeset objects
47
m = CHANGESET_HEADER_RE.match(line)
49
version = m.group('version')
51
m = CHANGESET_OLD_HEADER_RE.match(line)
53
version = m.group('version')
54
raise errors.ChangesetNotSupported(version, 'old format changesets not supported')
57
raise errors.NoChangesetFound(_get_filename(f))
59
# Now we have a version, to figure out how to read the changeset
60
if not _serializers.has_key(version):
61
raise errors.ChangesetNotSupported(version, 'version not listed in known versions')
63
serializer = _serializers[version](version)
65
return serializer.read(f)
68
def write(csets, f, version=None):
69
"""Serialize a list of changesets to a filelike object.
71
:param csets: The list of changesets to serialize
72
:param f: The file to output to
73
:param version: [optional] target serialization version
76
if not _serializers.has_key(version):
77
raise errors.ChangesetNotSupported(version, 'unknown changeset format')
79
serializer = serializer[version](version)
80
return serializer.write(csets, f)
83
def ChangesetSerializer(object):
84
"""The base class for Serializers.
86
Common functionality should be included here.
88
def __init__(self, version):
89
self.version = version
92
"""Read the rest of the changesets from the supplied file.
94
:param f: The file to read from
95
:return: A list of changesets
97
raise NotImplementedError
99
def write(self, csets, f):
100
"""Write the changesets to the supplied files.
102
:param csets: A list of changesets to be serialized
103
:param f: The file to write to
106
def register(version, klass, overwrite=False):
107
"""Register a ChangesetSerializer version.
109
:param version: The version associated with this format
110
:param klass: The class to instantiate, which must take a version argument
114
_serializers[version] = klass
117
if not _serializers.has_key(version):
118
_serializers[version] = klass
121
def register_lazy(version, module, classname, overwrite=False):
122
"""Register lazy-loaded changeset serializer.
124
:param version: The version associated with this reader
125
:param module: String indicating what module should be loaded
126
:param classname: Name of the class that will be instantiated
127
:param overwrite: Should this version override a default
129
def _loader(version):
130
mod = __import__(module, globals(), locals(), [classname])
131
klass = getattr(mod, classname)
132
return klass(version)
133
register(scheme, _loader, overwrite=overwrite)
136
register_lazy('0.6', 'bzrlib.changeset.serializer.v06', 'ChangesetSerializerV06')