Debian Codegraph @

scripts/python/TerraSync/terrasync/exceptions.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # exceptions.py --- Custom exception classes for terrasync.py
4 #
5 # Copyright (C) 2018 Florent Rougon
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 2 of the
10 # License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
21 # Generic exception class for terrasync.py, to be subclassed for each specific
22 # kind of exception.
23 class TerraSyncPyException(Exception):
24 def __init__(self, message=None, *, mayCapitalizeMsg=True):
25 """Initialize a TerraSyncPyException instance.
26
27 Except in cases where 'message' starts with a proper noun or
28 something like that, its first character should be given in
29 lower case. Automated treatments of this exception may print the
30 message with its first character changed to upper case, unless
31 'mayCapitalizeMsg' is False. In other words, if the case of the
32 first character of 'message' must not be changed under any
33 circumstances, set 'mayCapitalizeMsg' to False.
34
35 """
36 self.message = message
37 self.mayCapitalizeMsg = mayCapitalizeMsg
38
39 def __str__(self):
40 return self.completeMessage()
41
42 def __repr__(self):
43 return "{}.{}({!r})".format(__name__, type(self).__name__, self.message)
44
45 # Typically overridden by subclasses with a custom constructor
46 def detail(self):
47 return self.message
48
49 def completeMessage(self):
50 if self.message:
51 return "{shortDesc}: {detail}".format(
52 shortDesc=self.ExceptionShortDescription,
53 detail=self.detail())
54 else:
55 return self.ExceptionShortDescription
56
57 ExceptionShortDescription = "terrasync.py generic exception"
58
59
60 class UserError(TerraSyncPyException):
61 """Exception raised when the program is used in an incorrect way."""
62 ExceptionShortDescription = "User error"
63
64 class NetworkError(TerraSyncPyException):
65 """Exception raised when getting a network error even after retrying."""
66 ExceptionShortDescription = "Network error"
67
68 class UnsupportedURLScheme(TerraSyncPyException):
69 """Exception raised when asked to handle an unsupported URL scheme."""
70 ExceptionShortDescription = "Unsupported URL scheme"
71
72 class RepoDataError(TerraSyncPyException):
73 """
74 Exception raised when getting invalid data from the TerraSync repository."""
75 ExceptionShortDescription = "Invalid data from the TerraSync repository"
76
77 class InvalidDirIndexFile(RepoDataError):
78 """Exception raised when getting invalid data from a .dirindex file."""
79 ExceptionShortDescription = "Invalid .dirindex file"