<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import partial
from promise import Deferred
from contentbits.factory import CollectionFactory


class Manager(object):

    class CollectionSaveHelper(object):

        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.id,
                        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.id, next(self.old_collection).id)\
                        .done(partial(self._del_items))\
                        .fail(self.deferred.reject)
            except StopIteration:
                self.deferred.resolve(self.new_collection.id)

    def __init__(self, storage=None, collection_factory=None):
        self._storage = None
        self._collection_factory = None
        self.set_storage(storage)
        if collection_factory is None:
            collection_factory = CollectionFactory()
        self.set_collection_factory(collection_factory)

    def set_storage(self, storage):
        self._storage = storage
        return self

    def set_collection_factory(self, factory):
        self._collection_factory = factory
        return self

    def save_collection(self, collection):
        return Manager.CollectionSaveHelper(self, collection).save()
<commit_msg>Added properties to read collection_factory and storage instances from manager instance<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import partial
from promise import Deferred
from contentbits.factory import CollectionFactory


class Manager(object):

    class CollectionSaveHelper(object):

        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.id,
                        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.id, next(self.old_collection).id)\
                        .done(partial(self._del_items))\
                        .fail(self.deferred.reject)
            except StopIteration:
                self.deferred.resolve(self.new_collection.id)

    def __init__(self, storage=None, collection_factory=None):
        self._storage = None
        self._collection_factory = None
        self.set_storage(storage)
        if collection_factory is None:
            collection_factory = CollectionFactory()
        self.set_collection_factory(collection_factory)

    @property
    def storage(self):
        return self._storage

    def set_storage(self, storage):
        self._storage = storage
        return self

    def set_collection_factory(self, factory):
        self._collection_factory = factory
        return self

    @property
    def collection_factory(self):
        return self._collection_factory

    def save_collection(self, collection):
        return Manager.CollectionSaveHelper(self, collection).save()

{'fraction_non_alphanumeric': 0.07987711213517665, 'fraction_numerical': 0.00030721966205837174, 'mean_word_length': 2.029781293624942, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5418999', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1590, 'n_words': 328}