Add setup.cfg.
[jelmer/python-fastimport.git] / fastimport / errors.py
1 # Copyright (C) 2008 Canonical Ltd
2 #
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.
7 #
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.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 """Exception classes for fastimport"""
17
18 # Prefix to messages to show location information
19 _LOCATION_FMT = "line %(lineno)d: "
20
21 # ImportError is heavily based on BzrError
22
23 class ImportError(Exception):
24     """The base exception class for all import processing exceptions."""
25
26     def __init__(self):
27         super(ImportError, self).__init__(self._fmt % self.__dict__)
28
29
30 class ParsingError(ImportError):
31     """The base exception class for all import processing exceptions."""
32
33     _fmt = _LOCATION_FMT + "Unknown Import Parsing Error"
34
35     def __init__(self, lineno):
36         self.lineno = lineno
37         ImportError.__init__(self)
38
39
40 class MissingBytes(ParsingError):
41     """Raised when EOF encountered while expecting to find more bytes."""
42
43     _fmt = (_LOCATION_FMT + "Unexpected EOF - expected %(expected)d bytes,"
44         " found %(found)d")
45
46     def __init__(self, lineno, expected, found):
47         self.expected = expected
48         self.found = found
49         ParsingError.__init__(self, lineno)
50
51
52 class MissingTerminator(ParsingError):
53     """Raised when EOF encountered while expecting to find a terminator."""
54
55     _fmt = (_LOCATION_FMT +
56         "Unexpected EOF - expected '%(terminator)s' terminator")
57
58     def __init__(self, lineno, terminator):
59         self.terminator = terminator
60         ParsingError.__init__(self, lineno)
61
62
63 class InvalidCommand(ParsingError):
64     """Raised when an unknown command found."""
65
66     _fmt = (_LOCATION_FMT + "Invalid command '%(cmd)s'")
67
68     def __init__(self, lineno, cmd):
69         self.cmd = cmd
70         ParsingError.__init__(self, lineno)
71
72
73 class MissingSection(ParsingError):
74     """Raised when a section is required in a command but not present."""
75
76     _fmt = (_LOCATION_FMT + "Command %(cmd)s is missing section %(section)s")
77
78     def __init__(self, lineno, cmd, section):
79         self.cmd = cmd
80         self.section = section
81         ParsingError.__init__(self, lineno)
82
83
84 class BadFormat(ParsingError):
85     """Raised when a section is formatted incorrectly."""
86
87     _fmt = (_LOCATION_FMT + "Bad format for section %(section)s in "
88         "command %(cmd)s: found '%(text)s'")
89
90     def __init__(self, lineno, cmd, section, text):
91         self.cmd = cmd
92         self.section = section
93         self.text = text
94         ParsingError.__init__(self, lineno)
95
96
97 class InvalidTimezone(ParsingError):
98     """Raised when converting a string timezone to a seconds offset."""
99
100     _fmt = (_LOCATION_FMT +
101         "Timezone %(timezone)r could not be converted.%(reason)s")
102
103     def __init__(self, lineno, timezone, reason=None):
104         self.timezone = timezone
105         if reason:
106             self.reason = ' ' + reason
107         else:
108             self.reason = ''
109         ParsingError.__init__(self, lineno)
110
111
112 class PrematureEndOfStream(ParsingError):
113     """Raised when the 'done' feature was specified but missing."""
114
115     _fmt = (_LOCATION_FMT + "Stream end before 'done' command")
116
117     def __init__(self, lineno):
118         ParsingError.__init__(self, lineno)
119
120
121 class UnknownDateFormat(ImportError):
122     """Raised when an unknown date format is given."""
123
124     _fmt = ("Unknown date format '%(format)s'")
125
126     def __init__(self, format):
127         self.format = format
128         ImportError.__init__(self)
129
130
131 class MissingHandler(ImportError):
132     """Raised when a processor can't handle a command."""
133
134     _fmt = ("Missing handler for command %(cmd)s")
135
136     def __init__(self, cmd):
137         self.cmd = cmd
138         ImportError.__init__(self)
139
140
141 class UnknownParameter(ImportError):
142     """Raised when an unknown parameter is passed to a processor."""
143
144     _fmt = ("Unknown parameter - '%(param)s' not in %(knowns)s")
145
146     def __init__(self, param, knowns):
147         self.param = param
148         self.knowns = knowns
149         ImportError.__init__(self)
150
151
152 class BadRepositorySize(ImportError):
153     """Raised when the repository has an incorrect number of revisions."""
154
155     _fmt = ("Bad repository size - %(found)d revisions found, "
156         "%(expected)d expected")
157
158     def __init__(self, expected, found):
159         self.expected = expected
160         self.found = found
161         ImportError.__init__(self)
162
163
164 class BadRestart(ImportError):
165     """Raised when the import stream and id-map do not match up."""
166
167     _fmt = ("Bad restart - attempted to skip commit %(commit_id)s "
168         "but matching revision-id is unknown")
169
170     def __init__(self, commit_id):
171         self.commit_id = commit_id
172         ImportError.__init__(self)
173
174
175 class UnknownFeature(ImportError):
176     """Raised when an unknown feature is given in the input stream."""
177
178     _fmt = ("Unknown feature '%(feature)s' - try a later importer or "
179         "an earlier data format")
180
181     def __init__(self, feature):
182         self.feature = feature
183         ImportError.__init__(self)