FIXME: hotpatch for compatibility with latest hg
[git] / git_remote_helpers / 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, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17 """Exception classes for fastimport"""
18
19
20 class FastImportError(StandardError):
21     """The base exception class for all import processing exceptions."""
22
23     _fmt = "Unknown Import Error"
24
25     def __str__(self):
26         return self._fmt % self.__dict__
27
28 class ParsingError(FastImportError):
29     """The base exception class for all import processing exceptions."""
30
31     _fmt = "Unknown Import Parsing Error"
32
33     def __init__(self, filename, lineno):
34         FastImportError.__init__(self)
35         self.filename = filename
36         self.lineno = lineno
37
38     def __str__(self):
39         result = []
40         if self.filename:
41             result.append(self.filename)
42             result.append(", ")
43         result.append("line ")
44         result.append(str(self.lineno))
45         result.append(": ")
46         result.append(FastImportError.__str__(self))
47         return "".join(result)
48
49
50 class MissingBytes(ParsingError):
51     """Raised when EOF encountered while expecting to find more bytes."""
52
53     _fmt = ("Unexpected EOF - expected %(expected)d bytes,"
54         " found %(found)d")
55
56     def __init__(self, filename, lineno, expected, found):
57         ParsingError.__init__(self, filename, lineno)
58         self.expected = expected
59         self.found = found
60
61
62 class MissingTerminator(ParsingError):
63     """Raised when EOF encountered while expecting to find a terminator."""
64
65     _fmt = "Unexpected EOF - expected '%(terminator)s' terminator"
66
67     def __init__(self, filename, lineno, terminator):
68         ParsingError.__init__(self, filename, lineno)
69         self.terminator = terminator
70
71
72 class InvalidCommand(ParsingError):
73     """Raised when an unknown command found."""
74
75     _fmt = ("Invalid command '%(cmd)s'")
76
77     def __init__(self, filename, lineno, cmd):
78         ParsingError.__init__(self, filename, lineno)
79         self.cmd = cmd
80
81
82 class MissingSection(ParsingError):
83     """Raised when a section is required in a command but not present."""
84
85     _fmt = ("Command %(cmd)s is missing section %(section)s")
86
87     def __init__(self, filename, lineno, cmd, section):
88         ParsingError.__init__(self, filename, lineno)
89         self.cmd = cmd
90         self.section = section
91
92
93 class BadFormat(ParsingError):
94     """Raised when a section is formatted incorrectly."""
95
96     _fmt = ("Bad format for section %(section)s in "
97             "command %(cmd)s: found '%(text)s'")
98
99     def __init__(self, filename, lineno, cmd, section, text):
100         ParsingError.__init__(self, filename, lineno)
101         self.cmd = cmd
102         self.section = section
103         self.text = text
104
105
106 class InvalidTimezone(ParsingError):
107     """Raised when converting a string timezone to a seconds offset."""
108
109     _fmt = "Timezone %(timezone)r could not be converted.%(reason)s"
110
111     def __init__(self, filename, lineno, timezone, reason=None):
112         ParsingError.__init__(self, filename, lineno)
113         self.timezone = timezone
114         if reason:
115             self.reason = ' ' + reason
116         else:
117             self.reason = ''
118
119
120 class UnknownDateFormat(FastImportError):
121     """Raised when an unknown date format is given."""
122
123     _fmt = ("Unknown date format '%(format)s'")
124
125     def __init__(self, format):
126         FastImportError.__init__(self)
127         self.format = format
128
129
130 class MissingHandler(FastImportError):
131     """Raised when a processor can't handle a command."""
132
133     _fmt = ("Missing handler for command %(cmd)s")
134
135     def __init__(self, cmd):
136         FastImportError.__init__(self)
137         self.cmd = cmd
138
139
140 class UnknownParameter(FastImportError):
141     """Raised when an unknown parameter is passed to a processor."""
142
143     _fmt = ("Unknown parameter - '%(param)s' not in %(knowns)s")
144
145     def __init__(self, param, knowns):
146         FastImportError.__init__(self)
147         self.param = param
148         self.knowns = knowns
149
150
151 class BadRepositorySize(FastImportError):
152     """Raised when the repository has an incorrect number of revisions."""
153
154     _fmt = ("Bad repository size - %(found)d revisions found, "
155         "%(expected)d expected")
156
157     def __init__(self, expected, found):
158         FastImportError.__init__(self)
159         self.expected = expected
160         self.found = found
161
162
163 class BadRestart(FastImportError):
164     """Raised when the import stream and id-map do not match up."""
165
166     _fmt = ("Bad restart - attempted to skip commit %(commit_id)s "
167         "but matching revision-id is unknown")
168
169     def __init__(self, commit_id):
170         FastImportError.__init__(self)
171         self.commit_id = commit_id
172
173
174 class UnknownFeature(FastImportError):
175     """Raised when an unknown feature is given in the input stream."""
176
177     _fmt = ("Unknown feature '%(feature)s' - try a later importer or "
178         "an earlier data format")
179
180     def __init__(self, feature):
181         FastImportError.__init__(self)
182         self.feature = feature