Exemple #1
0
    def push(self, query, notify_func, cache_received_func=None):
        """Push a new query into the Retriever.

        Internally the query will be wraped into an Order.
        The Order provides a results (list of plyr.Caches) and a query field,
        also there's the time of the Order initialization in timestamp.

        If the query (or one very similar) is currently processed push() will
        return immediately.

        Callbacks can be provided to get the results of the query, both will be
        called on the main thread, for a stress-free threading experience:

            * ``notify_func``: called with the order as soon commit() finished
            * ``cache_received_func``: called, possible several times, as soon a
               single cache is ready. The order and the cache is passed.
               The cache will not be in the results field of the order.
               Do not store the cache, it is temporarily and will be destroyed in
               the background. This is only meant for "render or surrender".

        :returns: the timestamp of the point where the order was created.
        """
        if not any((notify_func, query)):
            return None

        key = make_query_key(query)
        if key in self._query_dict:
            return None

        # The order wraps all the extra data:
        order = Order(
            notify_func=notify_func,
            cache_received_func=cache_received_func,
            query=query,
            timestamp=time.time(),
            results=[]
        )

        # Configure to use the database if possible.
        query.database = self.database
        if cache_received_func:
            query.callback = self._on_query_callback

        # Remember the order.
        self._query_dict[key] = order

        # Distribute it to some thread on the C-side
        MetadataThreads.push(self, order)

        # Users might use this to know if the need to update.
        return order.timestamp
Exemple #2
0
    def __init__(self):
        self._query_dict = {}
        self.database = plyr.Database(g.CACHE_DIR)
        MetadataThreads.__init__(
            self,
            self._on_thread,
            self._on_deliver
        )

        # Config Defaults:
        g.config.add_defaults({
            'metadata': {
                'language': 'en',
                'only_language': False,
                'qsratio': 1.0,
                'fuzzyness': 4,
                'normalization': 'aggressive',
                'force_utf8': False,
                'plugmax': 5,
                'image_min_size': -1,
                'image_max_size': -1,
                'timeout': 5.0,
                'parallel': 4,
                'verbosity': 0
            }
        })

        # Add the provider defaults for all get_types
        defaults = {}
        for get_type in plyr.PROVIDERS.keys():
            defaults[get_type] = ['all']

        # Manually add the special providers:
        g.config.add_defaults({
            'metadata': {
                'providers': defaults
            }
        })