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
17 """Exception classes for fastimport"""
19 # Prefix to messages to show location information
20 _LOCATION_FMT = "line %(lineno)d: "
22 # ImportError is heavily based on BzrError
24 class ImportError(StandardError):
25 """The base exception class for all import processing exceptions."""
27 _fmt = "Unknown Import Error"
29 def __init__(self, msg=None, **kwds):
30 StandardError.__init__(self)
32 self._preformatted_string = msg
34 self._preformatted_string = None
35 for key, value in kwds.items():
36 setattr(self, key, value)
39 s = getattr(self, '_preformatted_string', None)
41 # contains a preformatted message
46 d = dict(self.__dict__)
48 # __str__() should always return a 'str' object
49 # never a 'unicode' object.
51 except (AttributeError, TypeError, NameError, ValueError, KeyError), e:
52 return 'Unprintable exception %s: dict=%r, fmt=%r, error=%r' \
53 % (self.__class__.__name__,
55 getattr(self, '_fmt', None),
58 def __unicode__(self):
60 if isinstance(u, str):
61 # Try decoding the str using the default encoding.
63 elif not isinstance(u, unicode):
64 # Try to make a unicode object from it, because __unicode__ must
65 # return a unicode object.
71 if isinstance(s, unicode):
74 # __str__ must return a str.
79 return '%s(%s)' % (self.__class__.__name__, str(self))
81 def __eq__(self, other):
82 if self.__class__ is not other.__class__:
84 return self.__dict__ == other.__dict__
87 class ParsingError(ImportError):
88 """The base exception class for all import processing exceptions."""
90 _fmt = _LOCATION_FMT + "Unknown Import Parsing Error"
92 def __init__(self, lineno):
93 ImportError.__init__(self)
97 class MissingBytes(ParsingError):
98 """Raised when EOF encountered while expecting to find more bytes."""
100 _fmt = (_LOCATION_FMT + "Unexpected EOF - expected %(expected)d bytes,"
103 def __init__(self, lineno, expected, found):
104 ParsingError.__init__(self, lineno)
105 self.expected = expected
109 class MissingTerminator(ParsingError):
110 """Raised when EOF encountered while expecting to find a terminator."""
112 _fmt = (_LOCATION_FMT +
113 "Unexpected EOF - expected '%(terminator)s' terminator")
115 def __init__(self, lineno, terminator):
116 ParsingError.__init__(self, lineno)
117 self.terminator = terminator
120 class InvalidCommand(ParsingError):
121 """Raised when an unknown command found."""
123 _fmt = (_LOCATION_FMT + "Invalid command '%(cmd)s'")
125 def __init__(self, lineno, cmd):
126 ParsingError.__init__(self, lineno)
130 class MissingSection(ParsingError):
131 """Raised when a section is required in a command but not present."""
133 _fmt = (_LOCATION_FMT + "Command %(cmd)s is missing section %(section)s")
135 def __init__(self, lineno, cmd, section):
136 ParsingError.__init__(self, lineno)
138 self.section = section
141 class BadFormat(ParsingError):
142 """Raised when a section is formatted incorrectly."""
144 _fmt = (_LOCATION_FMT + "Bad format for section %(section)s in "
145 "command %(cmd)s: found '%(text)s'")
147 def __init__(self, lineno, cmd, section, text):
148 ParsingError.__init__(self, lineno)
150 self.section = section
154 class InvalidTimezone(ParsingError):
155 """Raised when converting a string timezone to a seconds offset."""
157 _fmt = (_LOCATION_FMT +
158 "Timezone %(timezone)r could not be converted.%(reason)s")
160 def __init__(self, lineno, timezone, reason=None):
161 ParsingError.__init__(self, lineno)
162 self.timezone = timezone
164 self.reason = ' ' + reason
169 class UnknownDateFormat(ImportError):
170 """Raised when an unknown date format is given."""
172 _fmt = ("Unknown date format '%(format)s'")
174 def __init__(self, format):
175 ImportError.__init__(self)
179 class MissingHandler(ImportError):
180 """Raised when a processor can't handle a command."""
182 _fmt = ("Missing handler for command %(cmd)s")
184 def __init__(self, cmd):
185 ImportError.__init__(self)
189 class UnknownParameter(ImportError):
190 """Raised when an unknown parameter is passed to a processor."""
192 _fmt = ("Unknown parameter - '%(param)s' not in %(knowns)s")
194 def __init__(self, param, knowns):
195 ImportError.__init__(self)
200 class BadRepositorySize(ImportError):
201 """Raised when the repository has an incorrect number of revisions."""
203 _fmt = ("Bad repository size - %(found)d revisions found, "
204 "%(expected)d expected")
206 def __init__(self, expected, found):
207 ImportError.__init__(self)
208 self.expected = expected
212 class BadRestart(ImportError):
213 """Raised when the import stream and id-map do not match up."""
215 _fmt = ("Bad restart - attempted to skip commit %(commit_id)s "
216 "but matching revision-id is unknown")
218 def __init__(self, commit_id):
219 ImportError.__init__(self)
220 self.commit_id = commit_id
223 class UnknownFeature(ImportError):
224 """Raised when an unknown feature is given in the input stream."""
226 _fmt = ("Unknown feature '%(feature)s' - try a later importer or "
227 "an earlier data format")
229 def __init__(self, feature):
230 ImportError.__init__(self)
231 self.feature = feature