1 |
commit: eef89beeb23c6c539944b9f09d038fa821912667 |
2 |
Author: Brian Dolbec <dolsen <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sun Dec 28 16:54:10 2014 +0000 |
4 |
Commit: Brian Dolbec <dolsen <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Dec 30 21:42:26 2014 +0000 |
6 |
URL: http://sources.gentoo.org/gitweb/?p=proj/gentoo-keys.git;a=commit;h=eef89bee |
7 |
|
8 |
gkeys/exception.py: Initial creation of GkeyException classes |
9 |
|
10 |
Mostly copied, modified from PortageException code. |
11 |
|
12 |
--- |
13 |
gkeys/gkeys/exception.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ |
14 |
1 file changed, 78 insertions(+) |
15 |
|
16 |
diff --git a/gkeys/gkeys/exception.py b/gkeys/gkeys/exception.py |
17 |
new file mode 100644 |
18 |
index 0000000..21a2e97 |
19 |
--- /dev/null |
20 |
+++ b/gkeys/gkeys/exception.py |
21 |
@@ -0,0 +1,78 @@ |
22 |
+# Copyright 1998-2014 Gentoo Foundation |
23 |
+# Distributed under the terms of the GNU General Public License v2 |
24 |
+ |
25 |
+ |
26 |
+import sys |
27 |
+ |
28 |
+ |
29 |
+if sys.version_info[0] >= 3: |
30 |
+ # pylint: disable=W0622 |
31 |
+ basestring = str |
32 |
+ |
33 |
+ def _unicode_encode(s, encoding='utf_8', errors='backslashreplace'): |
34 |
+ if isinstance(s, str): |
35 |
+ s = s.encode(encoding, errors) |
36 |
+ return s |
37 |
+ |
38 |
+ def _unicode_decode(s, encoding='utf_8', errors='replace'): |
39 |
+ if isinstance(s, bytes): |
40 |
+ s = str(s, encoding=encoding, errors=errors) |
41 |
+ return s |
42 |
+ |
43 |
+else: |
44 |
+ |
45 |
+ def _unicode_encode(s, encoding='utf_8', errors='backslashreplace'): |
46 |
+ if isinstance(s, unicode): |
47 |
+ s = s.encode(encoding, errors) |
48 |
+ return s |
49 |
+ |
50 |
+ def _unicode_decode(s, encoding='utf_8', errors='replace'): |
51 |
+ if isinstance(s, bytes): |
52 |
+ s = unicode(s, encoding=encoding, errors=errors) |
53 |
+ return s |
54 |
+ |
55 |
+ |
56 |
+class GkeysException(Exception): |
57 |
+ if sys.version_info[0] >= 3: |
58 |
+ def __init__(self, value): |
59 |
+ self.value = value[:] |
60 |
+ |
61 |
+ def __str__(self): |
62 |
+ if isinstance(self.value, str): |
63 |
+ return self.value |
64 |
+ else: |
65 |
+ return repr(self.value) |
66 |
+ else: |
67 |
+ def __init__(self, value): |
68 |
+ self.value = value[:] |
69 |
+ if isinstance(self.value, basestring): |
70 |
+ self.value = _unicode_decode(self.value, |
71 |
+ encoding='utf_8', errors='replace') |
72 |
+ |
73 |
+ def __unicode__(self): |
74 |
+ if isinstance(self.value, unicode): |
75 |
+ return self.value |
76 |
+ else: |
77 |
+ return _unicode_decode(repr(self.value), |
78 |
+ encoding='utf_8', errors='replace') |
79 |
+ |
80 |
+ def __str__(self): |
81 |
+ if isinstance(self.value, unicode): |
82 |
+ return _unicode_encode(self.value, |
83 |
+ encoding='utf_8', errors='backslashreplace') |
84 |
+ else: |
85 |
+ return repr(self.value) |
86 |
+ |
87 |
+ |
88 |
+class UpdateDbError(GkeysException): |
89 |
+ '''%s |
90 |
+ |
91 |
+ Please Run: 'gkeys refresh-key -C {foo}' |
92 |
+ on all installed keyring categories |
93 |
+ Then continue with normal gkey operations.''' |
94 |
+ def __init__(self, value): |
95 |
+ doc = self.__doc__ % (value) |
96 |
+ GkeysException.__init__(self, doc) |
97 |
+ |
98 |
+ |
99 |
+ |