/
exceptions.py
69 lines (50 loc) · 1.7 KB
/
exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
class CustomException(Exception):
"""
A base exception that handles pretty-printing errors for command-line tools.
"""
def __init__(self, msg):
self.msg = msg
def __unicode__(self):
return self.msg
def __str__(self):
return self.msg
class ColumnIdentifierError(CustomException):
"""
Exception raised when the user supplies an invalid column identifier.
"""
pass
class CSVTestException(CustomException):
"""
Superclass for all row-test-failed exceptions.
All must have a line number, the problematic row, and a text explanation.
"""
def __init__(self, line_number, row, msg):
super().__init__(msg)
self.line_number = line_number
self.row = row
class LengthMismatchError(CSVTestException):
"""
Encapsulate information about a row which as the wrong length.
"""
def __init__(self, line_number, row, expected_length):
msg = 'Expected %i columns, found %i columns' % (expected_length, len(row))
super().__init__(line_number, row, msg)
@property
def length(self):
return len(self.row)
class InvalidValueForTypeException(CustomException):
"""
Exception raised when a value can not be normalized to a specified type.
"""
def __init__(self, index, value, normal_type):
self.index = index
self.value = value
self.normal_type = normal_type
msg = 'Unable to convert "%s" to type %s (at index %i)' % (value, normal_type, index)
super().__init__(msg)
class RequiredHeaderError(CustomException):
"""
Exception raised when an operation requires a CSV file to have a header row.
"""
pass