diff options
author | 2014-12-28 08:54:10 -0800 | |
---|---|---|
committer | 2014-12-30 13:42:26 -0800 | |
commit | eef89beeb23c6c539944b9f09d038fa821912667 (patch) | |
tree | e55c2f7851dc4562dead9c3cc5c3b34dcc8816c3 /gkeys | |
parent | gkeys/actions.py: Alignment tweak for key-search message (diff) | |
download | gentoo-keys-eef89beeb23c6c539944b9f09d038fa821912667.tar.gz gentoo-keys-eef89beeb23c6c539944b9f09d038fa821912667.tar.bz2 gentoo-keys-eef89beeb23c6c539944b9f09d038fa821912667.zip |
gkeys/exception.py: Initial creation of GkeyException classes
Mostly copied, modified from PortageException code.
Diffstat (limited to 'gkeys')
-rw-r--r-- | gkeys/gkeys/exception.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/gkeys/gkeys/exception.py b/gkeys/gkeys/exception.py new file mode 100644 index 0000000..21a2e97 --- /dev/null +++ b/gkeys/gkeys/exception.py @@ -0,0 +1,78 @@ +# Copyright 1998-2014 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + + +import sys + + +if sys.version_info[0] >= 3: + # pylint: disable=W0622 + basestring = str + + def _unicode_encode(s, encoding='utf_8', errors='backslashreplace'): + if isinstance(s, str): + s = s.encode(encoding, errors) + return s + + def _unicode_decode(s, encoding='utf_8', errors='replace'): + if isinstance(s, bytes): + s = str(s, encoding=encoding, errors=errors) + return s + +else: + + def _unicode_encode(s, encoding='utf_8', errors='backslashreplace'): + if isinstance(s, unicode): + s = s.encode(encoding, errors) + return s + + def _unicode_decode(s, encoding='utf_8', errors='replace'): + if isinstance(s, bytes): + s = unicode(s, encoding=encoding, errors=errors) + return s + + +class GkeysException(Exception): + if sys.version_info[0] >= 3: + def __init__(self, value): + self.value = value[:] + + def __str__(self): + if isinstance(self.value, str): + return self.value + else: + return repr(self.value) + else: + def __init__(self, value): + self.value = value[:] + if isinstance(self.value, basestring): + self.value = _unicode_decode(self.value, + encoding='utf_8', errors='replace') + + def __unicode__(self): + if isinstance(self.value, unicode): + return self.value + else: + return _unicode_decode(repr(self.value), + encoding='utf_8', errors='replace') + + def __str__(self): + if isinstance(self.value, unicode): + return _unicode_encode(self.value, + encoding='utf_8', errors='backslashreplace') + else: + return repr(self.value) + + +class UpdateDbError(GkeysException): + '''%s + + Please Run: 'gkeys refresh-key -C {foo}' + on all installed keyring categories + Then continue with normal gkey operations.''' + def __init__(self, value): + doc = self.__doc__ % (value) + GkeysException.__init__(self, doc) + + + |