~sirn/fanboi2

ref: 834edf0edc5dd633c0ecea16231b6ed2d728476d fanboi2/fanboi2/tasks/_result_proxy.py -rw-r--r-- 1.3 KiB
834edf0eKridsada Thanabulpong Massive cleanup in preparation for 0.30 (#25) 3 years ago
                                                                                
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
from celery import states

from ..errors import deserialize_error
from ..models import deserialize_model


class ResultProxy(object):
    """A proxy class for :class:`celery.result.AsyncResult` that provide
    results serialization using :func:`fanboi2.errors.deserialize_error` and
    :func:`fanboi2.models.deserialize_model`.

    :param result: A result of :class:`celery.AsyncResult`.
    """

    def __init__(self, result):
        self._result = result
        self._object = None

    def deserialize(self, request):
        """Deserializing the result into Python object."""
        if self._object is None:
            obj, id_, *args = self._result.get()
            if obj == 'failure':
                class_ = deserialize_error(id_)
                if class_ is not None:
                    self._object = class_(*args)
            else:
                dbsession = request.find_service(name='db')
                class_ = deserialize_model(obj)
                if class_ is not None:
                    self._object = dbsession.query(class_).get(id_)
        return self._object

    def success(self):
        """Returns true if result was successfully processed."""
        return self._result.state == states.SUCCESS

    def __getattr__(self, name):
        return self._result.__getattribute__(name)