Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / commissioning / exception.py @ 2005b18e

History | View | Annotate | Download (3.9 kB)

1 54069d1b Stavros Sachtouris
# Copyright 2012 GRNET S.A. All rights reserved.
2 54069d1b Stavros Sachtouris
#
3 54069d1b Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 54069d1b Stavros Sachtouris
# without modification, are permitted provided that the following
5 54069d1b Stavros Sachtouris
# conditions are met:
6 54069d1b Stavros Sachtouris
#
7 54069d1b Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 54069d1b Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 54069d1b Stavros Sachtouris
#      disclaimer.
10 54069d1b Stavros Sachtouris
#
11 54069d1b Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 54069d1b Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 54069d1b Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 54069d1b Stavros Sachtouris
#      provided with the distribution.
15 54069d1b Stavros Sachtouris
#
16 54069d1b Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 54069d1b Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 54069d1b Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 54069d1b Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 54069d1b Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 54069d1b Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 54069d1b Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 54069d1b Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 54069d1b Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 54069d1b Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 54069d1b Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 54069d1b Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 54069d1b Stavros Sachtouris
#
29 54069d1b Stavros Sachtouris
# The views and conclusions contained in the software and
30 54069d1b Stavros Sachtouris
# documentation are those of the authors and should not be
31 54069d1b Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 54069d1b Stavros Sachtouris
# or implied, of GRNET S.A.
33 54069d1b Stavros Sachtouris
34 54069d1b Stavros Sachtouris
35 d1304043 Stavros Sachtouris
def str_or_utf8(s):
36 d1304043 Stavros Sachtouris
    if isinstance(s, unicode):
37 d1304043 Stavros Sachtouris
        return s.encode('utf8')
38 d1304043 Stavros Sachtouris
    return str(s)
39 d1304043 Stavros Sachtouris
40 d1304043 Stavros Sachtouris
41 54069d1b Stavros Sachtouris
class CallError(Exception):
42 54069d1b Stavros Sachtouris
    exceptions = {}
43 54069d1b Stavros Sachtouris
44 54069d1b Stavros Sachtouris
    def __new__(cls, *args, **kw):
45 54069d1b Stavros Sachtouris
        call_error = kw.get('call_error', None)
46 54069d1b Stavros Sachtouris
        if call_error is None:
47 54069d1b Stavros Sachtouris
            call_error = cls.__name__
48 54069d1b Stavros Sachtouris
        else:
49 54069d1b Stavros Sachtouris
            call_error = str(call_error)
50 54069d1b Stavros Sachtouris
        cls = CallError.exceptions.get(call_error, cls)
51 54069d1b Stavros Sachtouris
        self = Exception.__new__(cls)
52 54069d1b Stavros Sachtouris
        return self
53 54069d1b Stavros Sachtouris
54 54069d1b Stavros Sachtouris
    def __init__(self, *args, **kw):
55 d1304043 Stavros Sachtouris
        self.call_error = kw.pop('call_error', self.__class__.__name__)
56 54069d1b Stavros Sachtouris
        self.args = args
57 d1304043 Stavros Sachtouris
        self.kwargs = kw
58 54069d1b Stavros Sachtouris
59 54069d1b Stavros Sachtouris
    def __str__(self):
60 d1304043 Stavros Sachtouris
        return '\n--------\n'.join(str_or_utf8(x) for x in self.args)
61 54069d1b Stavros Sachtouris
62 54069d1b Stavros Sachtouris
    def __repr__(self):
63 54069d1b Stavros Sachtouris
        return '%s(%s)' % (self.__class__.__name__,
64 d1304043 Stavros Sachtouris
                           ','.join(str_or_utf8(x) for x in self.args))
65 54069d1b Stavros Sachtouris
66 54069d1b Stavros Sachtouris
    @classmethod
67 54069d1b Stavros Sachtouris
    def from_exception(cls, exc):
68 54069d1b Stavros Sachtouris
        args = None
69 54069d1b Stavros Sachtouris
        try:
70 54069d1b Stavros Sachtouris
            args = tuple(exc.args)
71 6764f588 Stavros Sachtouris
        except (TypeError, AttributeError):
72 54069d1b Stavros Sachtouris
            pass
73 54069d1b Stavros Sachtouris
74 54069d1b Stavros Sachtouris
        if args is None:
75 54069d1b Stavros Sachtouris
            args = (str(exc),)
76 54069d1b Stavros Sachtouris
        self = cls(*args, call_error=exc.__class__.__name__)
77 54069d1b Stavros Sachtouris
        return self
78 54069d1b Stavros Sachtouris
79 54069d1b Stavros Sachtouris
    def to_dict(self):
80 54069d1b Stavros Sachtouris
        return {'call_error': self.call_error,
81 d1304043 Stavros Sachtouris
                'error_args': (self.args, self.kwargs)}
82 54069d1b Stavros Sachtouris
83 54069d1b Stavros Sachtouris
    @classmethod
84 54069d1b Stavros Sachtouris
    def from_dict(cls, dictobj):
85 54069d1b Stavros Sachtouris
        args = None
86 54069d1b Stavros Sachtouris
        try:
87 54069d1b Stavros Sachtouris
            if 'error_args' in dictobj and 'call_error' in dictobj:
88 54069d1b Stavros Sachtouris
                args = dictobj['error_args']
89 54069d1b Stavros Sachtouris
                call_error = dictobj['call_error']
90 6764f588 Stavros Sachtouris
        except TypeError:
91 54069d1b Stavros Sachtouris
            pass
92 54069d1b Stavros Sachtouris
93 54069d1b Stavros Sachtouris
        if args is None:
94 54069d1b Stavros Sachtouris
            args = (str(dictobj),)
95 54069d1b Stavros Sachtouris
            call_error = 'UnknownError'
96 d1304043 Stavros Sachtouris
            kw = {}
97 d1304043 Stavros Sachtouris
        else:
98 d1304043 Stavros Sachtouris
            args, kw = args
99 54069d1b Stavros Sachtouris
100 d1304043 Stavros Sachtouris
        self = cls(*args, call_error=call_error, **kw)
101 54069d1b Stavros Sachtouris
        return self
102 54069d1b Stavros Sachtouris
103 6764f588 Stavros Sachtouris
104 54069d1b Stavros Sachtouris
def register_exceptions(*exceptions):
105 54069d1b Stavros Sachtouris
    for exception in exceptions:
106 54069d1b Stavros Sachtouris
        if not issubclass(exception, CallError):
107 6764f588 Stavros Sachtouris
            m = "Registering '%s': is not a CallError subclass" % (exception,)
108 54069d1b Stavros Sachtouris
            raise ValueError(m)
109 54069d1b Stavros Sachtouris
        CallError.exceptions[exception.__name__] = exception
110 54069d1b Stavros Sachtouris
111 6764f588 Stavros Sachtouris
112 54069d1b Stavros Sachtouris
def register_exception(exc):
113 54069d1b Stavros Sachtouris
    register_exceptions(exc)
114 54069d1b Stavros Sachtouris
    return exc
115 54069d1b Stavros Sachtouris
116 6764f588 Stavros Sachtouris
117 54069d1b Stavros Sachtouris
@register_exception
118 54069d1b Stavros Sachtouris
class CorruptedError(CallError):
119 54069d1b Stavros Sachtouris
    pass
120 54069d1b Stavros Sachtouris
121 6764f588 Stavros Sachtouris
122 54069d1b Stavros Sachtouris
@register_exception
123 54069d1b Stavros Sachtouris
class InvalidDataError(CallError):
124 54069d1b Stavros Sachtouris
    pass
125 54069d1b Stavros Sachtouris
126 6764f588 Stavros Sachtouris
127 54069d1b Stavros Sachtouris
class ReturnButFail(Exception):
128 54069d1b Stavros Sachtouris
    def __init__(self, retval=None):
129 54069d1b Stavros Sachtouris
        self.data = retval