Пример #1
0
    class CollectionSaveHelper(six.Iterator):

        def __init__(self, manager, new_collection):
            self.deferred = Deferred()
            self._manager = manager
            self.new_collection = new_collection
            self.old_collection = None
            self.items = iter(new_collection)

        def __next__(self):
            return next(self.items)

        def save(self):
            # read current state of collection (it`s items)
            # if collection exists - go to next step
            # otherwise create new collection
            self._manager.storage.read_collection(self.new_collection.id)\
                    .done(partial(self._collection_found))\
                    .fail(partial(self._collection_not_found))

            return self.deferred.promise()

        def _collection_found(self, old_collection):
            self.old_collection = self._manager.collection_factory.discover(old_collection)
            self._add_items()

        def _collection_not_found(self, exception):
            self._manager.storage.create_collection(self.new_collection.id)\
                    .done(partial(self._collection_created))\
                    .fail(self.deferred.reject)

        def _collection_created(self, collection_id):
            self.new_collection.id = collection_id
            # lie that old collection was just empty
            self._collection_found([])

        def _add_items(self):
            try:
                self._manager.add_item_to_collection(self.new_collection,
                        next(self)).done(self._item_added)\
                        .fail(self.deferred.reject)
            except StopIteration:
                self._del_items()

        def _item_added(self, item_id):
            try:
                del self.old_collection[item_id]
            except:
                pass
            self._add_items()

        def _del_items(self, deleted_item_id=None):
            try:
                self._manager.remove_item_from_collection(
                        self.new_collection, next(self.old_collection))\
                        .done(partial(self._del_items))\
                        .fail(self.deferred.reject)
            except StopIteration:
                self.deferred.resolve(self.new_collection.id)
Пример #2
0
 def _wrapper(self, callback, message):
     """
     Calls given command with given data
     """
     tmp = self._parse_msg(message)
     d = Deferred()
     Deferred(partial(callback, *tmp['args'], **tmp['kwargs']))\
             .then(partial(self._serialize, d.resolve),
                     partial(self._serialize, d.reject))
     return d.promise()
Пример #3
0
    def read_collection(self, collection_id):
        deferred = Deferred()

        def _done(collection):
            c = self._collection_factory.discover(collection)
            c.id = collection_id
            deferred.resolve(c)

        self._storage.read_collection(collection_id)\
                .done(_done).fail(deferred.reject)
        return deferred.promise()
Пример #4
0
 def call(self, command, args=None, kwargs=None):
     """
     Calls given command with given attributes.
     Function should be given as a string.
     """
     self.connect()
     d = Deferred()
     Deferred(partial(self._adapter.send, str(command),
                      self._prepare_msg(args, kwargs)))\
         .then(partial(self._unserialize, d.resolve),
                 partial(self._unserialize, d.reject))
     return d.promise()
Пример #5
0
 def wrapper(*args, **kwargs):
     d = Deferred()
     try:
         d.resolve(func(*args, **kwargs))
     except Exception as e:
         d.reject(exception=e)
     return d.promise()
Пример #6
0
 def wrapper(*args, **kwargs):
     d = Deferred()
     kwargs['deferred'] = d
     func(*args, **kwargs)
     return d.promise()
Пример #7
0
 def __init__(self, manager, new_collection):
     self.deferred = Deferred()
     self._manager = manager
     self.new_collection = new_collection
     self.old_collection = None
     self.items = iter(new_collection)