Example #1
0
 def get_storage_s(unsynced_storages, storage_path,
                     name='main', file_format='pickle', TTL=None):
     # storage identifier
     filename = os.path.join(storage_path, name)
     
     # get storage from memory
     storage = unsynced_storages.get(filename)
     if storage is None:
         # thread safe disk loading
         with XBMCMixin._lock:
             # second attempt to get from memory (created by another thread?)
             storage = unsynced_storages.get(filename)
             # load from disk
             if storage is None:
                 if TTL:
                     TTL = timedelta(minutes=TTL)
                 try:
                     # load storage from disk
                     log.debug('Loading storage "%s" from disk', name)
                     storage = TimedStorage(filename, file_format, TTL)
                 except ValueError:
                     # storage file is corrupted, create new one
                     log.error('storage "%s" is corrupted', filename)
                     os.remove(filename)
                     storage = TimedStorage(filename, file_format, TTL)
                 unsynced_storages[filename] = storage
     return storage
    def _connect(self):
        log.debug("Opening Sqlite table %r in %s" % (self.tablename, self.filename))
        filename = encode_fs(self.filename)
        if self.flag == 'n':
            if os.path.exists(filename):
                os.remove(filename)
        dirname = os.path.dirname(filename)
        if dirname and not os.path.exists(dirname):
            raise RuntimeError('Error! The directory does not exist, %s' % self.filename)

        if self.autocommit:
            self.conn = sqlite3.connect(self.filename, isolation_level=None)
        else:
            self.conn = sqlite3.connect(self.filename)
        try:
            self._execute(self.CREATE_TABLE % self.tablename)
            self._execute(self.CREATE_INDEX % self.tablename)
            if self.flag == 'w':
                self.clear()
            elif self.autopurge and self.ttl:
                self.purge()
            elif self.cached:
                self._load()
        except sqlite3.DatabaseError:
            self.close()
            raise
Example #3
0
def cpb_page(cat, query, sort, page):

    log.debug("THE CAT = " + cat + " & THE Q = " + query)
    if not "cpasbien" in query:
        url_adr = BASE_URL + "/" + cat + "/" + query.replace(" ", "+") + "/page-" + str(page) + "," + sort
    else:
        url_adr = BASE_URL + "/view_cat.php?categorie=" + cat + "&page=" + str(page) + "&trie=" + sort[5:]
    log.debug("THE URL = " + url_adr)
    html_data = url_get(url_adr)

    soup = BeautifulSoup(html_data, "html5lib")
    name_nodes = soup.findAll("td", "torrent-aff")

    for name_node in name_nodes:
        title = name_node.find("a").text
        html_uri = name_node.find("a")["href"]
        torrent_basename = os.path.basename(html_uri)
        uri_addr = BASE_URL + "/_torrents/" + torrent_basename.replace(".html", ".torrent")
        img_addr = BASE_URL + "/_pictures/" + torrent_basename.replace(".html", ".jpg")
        yield {
            "label": title,
            "path": plugin.url_for("play", uri=uri_addr),
            "is_playable": True,
            "thumbnail": img_addr,
            "properties": {"fanart_image": img_addr},
        }
    yield {
        "label": ">> Next page",
        "path": plugin.url_for("cpb_page", cat=cat, query=query, sort=sort, page=int(page) + 1),
        "is_playable": False,
    }
 def close(self):
     log.debug("Closing %s" % self)
     if self.conn:
         if self.autocommit:
             self.commit()
         self.conn.close()
         self.conn = None
Example #5
0
    def finish(self, items=None, sort_methods=None, succeeded=True,
               update_listing=False, cache_to_disc=True, view_mode=None):
        '''Adds the provided items to the XBMC interface.

        :param items: an iterable of items where each item is either a
            dictionary with keys/values suitable for passing to
            :meth:`xbmcswift2.ListItem.from_dict` or an instance of
            :class:`xbmcswift2.ListItem`.
        :param sort_methods: a list of valid XBMC sort_methods. Each item in
                             the list can either be a sort method or a tuple of
                             ``sort_method, label2_mask``. See
                             :meth:`add_sort_method` for
                             more detail concerning valid sort_methods.

                             Example call with sort_methods::

                                sort_methods = ['label', 'title', ('date', '%D')]
                                plugin.finish(items, sort_methods=sort_methods)

        :param view_mode: can either be an integer (or parseable integer
            string) corresponding to a view_mode or the name of a type of view.
            Currrently the only view type supported is 'thumbnail'.
        :returns: a list of all ListItems added to the XBMC interface.
        '''
        # If we have any items, add them. Items are optional here.
        if items:
            self.add_items(items)
        if sort_methods:
            for sort_method in sort_methods:
                if not isinstance(sort_method, basestring) and hasattr(sort_method, '__len__'):
                    self.add_sort_method(*sort_method)
                else:
                    self.add_sort_method(sort_method)

        # Attempt to set a view_mode if given
        if view_mode is not None:
            # First check if we were given an integer or parseable integer
            try:
                view_mode_id = int(view_mode)
            except ValueError:
                # Attempt to lookup a view mode
                view_mode_id = self.get_view_mode_id(view_mode)

            if view_mode_id is not None:
                self.set_view_mode(view_mode_id)

        # Finalize the directory items
        self.end_of_directory(succeeded, update_listing, cache_to_disc)

        # Close any open storages which will persist them to disk
        if hasattr(self, '_unsynced_storages'):
            for storage in self._unsynced_storages.values():
                log.debug('Saving a %s storage to disk at "%s"',
                          storage.file_format, storage.filename)
                storage.close()

        # Return the cached list of all the list items that were added
        return self.added_items
Example #6
0
 def __init__(self, filename, flag='c', mode=None, file_format='pickle'):
     self.flag = flag                    # r=readonly, c=create, or n=new
     self.mode = mode                    # None or an octal triple like 0644
     self.file_format = file_format      # 'csv', 'json', or 'pickle'
     self.filename = filename
     if flag != 'n' and os.access(filename, os.R_OK):
         log.debug('Reading %s storage from disk at "%s"',
                   self.file_format, self.filename)
         fileobj = open(filename, 'rb' if file_format == 'pickle' else 'r')
         with fileobj:
             self.load(fileobj)
Example #7
0
 def _get_cache(self, cache_type, cache_name, **kwargs):
     if not hasattr(self, '_unsynced_caches'):
         self._unsynced_caches = {}
     filename = os.path.join(self.cache_path, cache_name)
     try:
         cache = self._unsynced_caches[filename]
         log.debug('Used live cache "%s" located at "%s"' % (cache_name, filename))
     except KeyError:
         cache = cache_type(filename, **kwargs)
         self._unsynced_caches[filename] = cache
         log.debug('Used cold cache "%s" located at "%s"' % (cache_name, filename))
     return cache
 def commit(self):
     if self.cached and self.cache:
         self.cached = False
         upd_dict = dict((k, v) for k, v in self.cache.iteritems()
                         if k not in self.original or not self.original[k] == v)
         if upd_dict:
             log.debug("Updated storage keys: %s" % ", ".join(upd_dict.keys()))
             self.update(upd_dict)
         self.original = copy.deepcopy(self.cache)
         self.cached = True
     if self.conn:
         self.conn.commit()
Example #9
0
 def commit(self):
     if self.cached and self.cache:
         self.cached = False
         upd_dict = dict((k, v) for k, v in self.cache.iteritems()
                         if k not in self.original or not self.original[k] == v)
         if upd_dict:
             log.debug("Updated storage keys: %s" % ", ".join(upd_dict.keys()))
             self.update(upd_dict)
         self.original = copy.deepcopy(self.cache)
         self.cached = True
     if self.conn:
         self.conn.commit()
    def get_storage(self, name='main', file_format='pickle', TTL=None):
        '''Returns a storage for the given name. The returned storage is a
        fully functioning python dictionary and is designed to be used that
        way. It is usually not necessary for the caller to load or save the
        storage manually. If the storage does not already exist, it will be
        created.

        .. seealso:: :class:`xbmcswift2.TimedStorage` for more details.

        :param name: The name  of the storage to retrieve.
        :param file_format: Choices are 'pickle', 'csv', and 'json'. Pickle is
                            recommended as it supports python objects.

                            .. note:: If a storage already exists for the given
                                      name, the file_format parameter is
                                      ignored. The format will be determined by
                                      the existing storage file.
        :param TTL: The time to live for storage items specified in minutes or None
                    for no expiration. Since storage items aren't expired until a
                    storage is loaded form disk, it is possible to call
                    get_storage() with a different TTL than when the storage was
                    created. The currently specified TTL is always honored.
        '''

        if not hasattr(self, '_unsynced_storages'):
            self._unsynced_storages = {}
        filename = os.path.join(self.storage_path, name)
        try:
            storage = self._unsynced_storages[filename]
            log.debug('Loaded storage "%s" from memory', name)
        except KeyError:
            if TTL:
                TTL = timedelta(minutes=TTL)

            try:
                storage = TimedStorage(filename, file_format, TTL)
            except ValueError:
                # Thrown when the storage file is corrupted and can't be read.
                # Prompt user to delete storage.
                choices = ['Clear storage', 'Cancel']
                ret = xbmcgui.Dialog().select('A storage file is corrupted. It'
                                              ' is recommended to clear it.',
                                              choices)
                if ret == 0:
                    os.remove(filename)
                    storage = TimedStorage(filename, file_format, TTL)
                else:
                    raise Exception('Corrupted storage file at %s' % filename)

            self._unsynced_storages[filename] = storage
            log.debug('Loaded storage "%s" from disk', name)
        return storage
Example #11
0
            def wrapper(*args, **kwargs):
                key = (function.__name__, kwd_mark,) + args
                if kwargs:
                    key += (kwd_mark,) + tuple(sorted(kwargs.items()))

                try:
                    result = cache[key]
                    #log.debug('Cache hit for key "%s"' % (key, ))
                    log.debug('Cache hit for function "%s" with args "%s" and kwargs "%s"' % (function.__name__, args, kwargs))
                except KeyError:
                    log.debug('Cache miss for function "%s" with args "%s" and kwargs "%s"' % (function.__name__, args, kwargs))
                    result = function(*args, **kwargs)
                    cache[key] = result
                cache.sync()
                return result
Example #12
0
    def finish(self, items=None, sort_methods=None, succeeded=True,
               update_listing=False, cache_to_disc=True, view_mode=None):
        '''Adds the provided items to the XBMC interface. Each item in
        the provided list should either be an instance of
        xbmcswift2.ListItem or a dictionary that will be passed to
        xbmcswift2.ListItem.from_dict().

        :param items: an iterable of items where each item is either a
            dictionary with keys/values suitable for passing to
            :meth:`xbmcswift2.ListItem.from_dict` or an instance of
            :class:`xbmcswift2.ListItem`.
        :param sort_methods: a list of valid XBMC sort_methods. See
            :attr:`xbmcswift2.SortMethod`.
        :param view_mode: can either be an integer (or parseable integer
            string) corresponding to a view_mode or the name of a type of view.
            Currrently the only view type supported is 'thumbnail'.
        :returns: a list of all ListItems added to the XBMC interface.
        '''
        # If we have any items, add them. Items are optional here.
        if items:
            self.add_items(items)
        if sort_methods:
            for sort_method in sort_methods:
                xbmcplugin.addSortMethod(self.handle, sort_method)

        # Attempt to set a view_mode if given
        if view_mode is not None:
            # First check if we were given an integer or parseable integer
            try:
                view_mode_id = int(view_mode)
            except ValueError:
                # Attempt to lookup a view mode
                view_mode_id = self.get_view_mode_id(view_mode)

            if view_mode_id is not None:
                self.set_view_mode(view_mode_id)

        # Finalize the directory items
        self.end_of_directory(succeeded, update_listing, cache_to_disc)

        # Close any open caches which will persist them to disk
        if hasattr(self, '_unsynced_caches'):
            for cache in self._unsynced_caches.values():
                log.debug('Saving a %s cache to disk at "%s"' % (cache.file_format, cache.filename))
                cache.close()

        # Return the cached list of all the list items that were added
        return self.added_items
            def wrapper(*args, **kwargs):
                key = (function.__name__, kwd_mark,) + args
                if kwargs:
                    key += (kwd_mark,) + tuple(sorted(kwargs.items()))

                try:
                    result = storage[key]
                    log.debug('Storage hit for function "%s" with args "%s" '
                              'and kwargs "%s"', function.__name__, args,
                              kwargs)
                except KeyError:
                    log.debug('Storage miss for function "%s" with args "%s" '
                              'and kwargs "%s"', function.__name__, args,
                              kwargs)
                    result = function(*args, **kwargs)
                    storage[key] = result
                    storage.sync()
                return result
    def get_storage(self, name='main', ttl=None, tablename=None, autocommit=True, cached=False):
        """Returns a storage for the given name. The returned storage is a
        fully functioning python dictionary and is designed to be used that
        way. It is usually not necessary for the caller to load or save the
        storage manually. If the storage does not already exist, it will be
        created.

        .. seealso:: :class:`xbmcswift2.TimedStorage` for more details.

        :param name: The name  of the storage to retrieve.
        :param file_format: Choices are 'pickle', 'csv', and 'json'. Pickle is
                            recommended as it supports python objects.

                            .. note:: If a storage already exists for the given
                                      name, the file_format parameter is
                                      ignored. The format will be determined by
                                      the existing storage file.
        :param ttl: The time to live for storage items specified in minutes or None
                    for no expiration. Since storage items aren't expired until a
                    storage is loaded form disk, it is possible to call
                    get_storage() with a different TTL than when the storage was
                    created. The currently specified TTL is always honored.
        """

        import sqlite3
        if not hasattr(self, '_unsynced_storages'):
            self._unsynced_storages = {}
        filename = os.path.join(self.storage_path, name)
        tablename = tablename or os.path.basename(name).replace('.', '_')
        try:
            storage = self._unsynced_storages[filename]
            log.debug('Loaded storage "%s" from memory', name)
        except KeyError:
            if ttl:
                ttl *= 60

            storage = Storage(filename, ttl=ttl, tablename=tablename, autocommit=autocommit,
                              cached=cached, autopurge=True)
            self._unsynced_storages[filename] = storage
            log.debug('Loaded storage "%s" from disk', name)
        return storage
Example #15
0
            def wrapper(*args, **kwargs):
                # TODO test this method
                storage = XBMCMixin.get_storage_s(unsynced_storages, storage_path, cachename, file_format='pickle', TTL=TTL)
                
                kwd_mark = 'f35c2d973e1bbbc61ca60fc6d7ae4eb3'
                key = (function.__name__, kwd_mark,) + args
                if kwargs:
                    key += (kwd_mark,) + tuple(sorted(kwargs.items()))

                try:
                    result = storage[key]
                    log.debug('Storage hit for function "%s" with args "%s" '
                              'and kwargs "%s"', function.__name__, args,
                              kwargs)
                except KeyError:
                    log.debug('Storage miss for function "%s" with args "%s" '
                              'and kwargs "%s"', function.__name__, args,
                              kwargs)
                    result = function(*args, **kwargs)
                    if result:
                        storage[key] = result
                        storage.sync()
                return result