Exemplo n.º 1
0
    def __init__(self, manager, fid, name, size, filestatus, media, added,
                 fileorder, url, pluginname, hash, status, error, package,
                 owner):
        super(File, self).__init__()

        self.manager = manager
        self.pyload = manager.pyload

        self.fid = int(fid)
        self._name = purge.name(name)
        self._size = int(size)
        self.filestatus = filestatus
        self.media = media
        self.added = added
        self.fileorder = fileorder
        self.url = url
        self.pluginname = pluginname
        self.hash = hash
        self.status = status
        self.error = error
        self.owner = owner
        self.packageid = package
        # database information ends here

        self.lock = RWLock()

        self.plugin = None

        self.wait_until = 0  # time.time() + time to wait

        # status attributes
        self.abort = False
        self.reconnected = False
        self.statusname = None
Exemplo n.º 2
0
    def __init__(
        self, manager, fid, name, size, filestatus, media, added,
            fileorder, url, pluginname, hash, status, error, package, owner):
        super(File, self).__init__()

        self.manager = manager
        self.pyload = manager.pyload

        self.fid = int(fid)
        self._name = purge.name(name)
        self._size = int(size)
        self.filestatus = filestatus
        self.media = media
        self.added = added
        self.fileorder = fileorder
        self.url = url
        self.pluginname = pluginname
        self.hash = hash
        self.status = status
        self.error = error
        self.owner = owner
        self.packageid = package
        # database information ends here

        self.lock = RWLock()

        self.plugin = None

        self.wait_until = 0  # time.time() + time to wait

        # status attributes
        self.abort = False
        self.reconnected = False
        self.statusname = None
Exemplo n.º 3
0
    def setup(self):
        # won't start download when true
        self.pause = True

        # each thread is in exactly one category
        self.free = []
        # a thread that in working must have a file as active attribute
        self.downloading = []
        # holds the decrypter threads
        self.decrypting = []

        # indicates when reconnect has occurred
        self.reconnecting = Event()

        self.lock = RWLock()
Exemplo n.º 4
0
    def __init__(self, core):
        """
        Constructor.
        """
        BaseManager.__init__(self, core)

        # translations
        self.status_msg = [
            self._("none"),
            self._("offline"),
            self._("online"),
            self._("queued"),
            self._("paused"),
            self._("finished"),
            self._("skipped"),
            self._("failed"),
            self._("starting"),
            self._("waiting"),
            self._("downloading"),
            self._("temp. offline"),
            self._("aborted"),
            self._("not possible"),
            self._("missing"),
            self._("file mismatch"),
            self._("occupied"),
            self._("decrypting"),
            self._("processing"),
            self._("custom"),
            self._("unknown")
        ]

        self.files = {}  # holds instances for files
        self.packages = {}  # same for packages

        self.job_cache = {}

        # locking the caches, db is already locked implicit
        self.lock = RWLock()
        # self.lock._Verbose__verbose = True

        self.downloadstats = {}  # cached dl stats
        self.queuestats = {}  # cached queue stats

        self.db = self.pyload_core.db
Exemplo n.º 5
0
    def setup(self):
        # translations
        self.status_msg = [
            self._('none'),
            self._('offline'),
            self._('online'),
            self._('queued'),
            self._('paused'),
            self._('finished'),
            self._('skipped'),
            self._('failed'),
            self._('starting'),
            self._('waiting'),
            self._('downloading'),
            self._('temp. offline'),
            self._('aborted'),
            self._('not possible'),
            self._('missing'),
            self._('file mismatch'),
            self._('occupied'),
            self._('decrypting'),
            self._('processing'),
            self._('custom'),
            self._('unknown')
        ]

        self.files = {}  # holds instances for files
        self.packages = {}  # same for packages

        self.job_cache = {}

        # locking the caches, db is already locked implicit
        self.lock = RWLock()
        # self.lock._Verbose__verbose = True

        self.downloadstats = {}  # cached dl stats
        self.queuestats = {}  # cached queue stats

        self.db = self.pyload.db
Exemplo n.º 6
0
class File(BaseObject):
    """Represents a file object at runtime."""
    __slots__ = [
        '_name', '_size', 'abort', 'added', 'error', 'fid', 'fileorder',
        'filestatus', 'hash', 'lock', 'manager', 'media', 'owner', 'packageid',
        'plugin', 'pluginclass', 'pluginname', 'pyload', 'reconnected',
        'status', 'statusname', 'url', 'wait_until'
    ]

    @staticmethod
    def from_info_data(m, info):
        file = File(m, info.fid, info.name, info.size, info.status, info.media,
                    info.added, info.fileorder, '', '', '', DownloadStatus.NA,
                    '', info.package, info.owner)
        if info.download:
            file.url = info.download.url
            file.pluginname = info.download.plugin
            file.hash = info.download.hash
            file.status = info.download.status
            file.error = info.download.error

        file.init_plugin()
        return file

    def __init__(self, manager, fid, name, size, filestatus, media, added,
                 fileorder, url, pluginname, hash, status, error, package,
                 owner):
        super(File, self).__init__()

        self.manager = manager
        self.pyload = manager.pyload

        self.fid = int(fid)
        self._name = purge.name(name)
        self._size = int(size)
        self.filestatus = filestatus
        self.media = media
        self.added = added
        self.fileorder = fileorder
        self.url = url
        self.pluginname = pluginname
        self.hash = hash
        self.status = status
        self.error = error
        self.owner = owner
        self.packageid = package
        # database information ends here

        self.lock = RWLock()

        self.plugin = None

        self.wait_until = 0  # time.time() + time to wait

        # status attributes
        self.abort = False
        self.reconnected = False
        self.statusname = None

    def get_size(self):
        """Get size of download."""
        if self.plugin is not None and self.plugin.dl is not None \
                and self.plugin.dl.size is not None:
            self.set_size(self.plugin.dl.size)
        return self._size

    # NOTE: convert size to int
    def set_size(self, value):
        self._size = int(value)

    size = property(get_size, set_size)

    def get_name(self):
        try:
            if self.plugin.req.name:
                return self.plugin.req.name
            else:
                return self._name
        except Exception:
            return self._name

    def set_name(self, name):
        """Only set unicode or utf8 strings as name."""
        name = purge.name(name)

        # media type is updated if needed
        if self._name != name:
            self.media = guess_type(name)
            self._name = name

    name = property(get_name, set_name)

    def __repr__(self):
        return '<File {0}: {1}@{2}>'.format(self.fid, self.name,
                                            self.pluginname)

    @lock
    def init_plugin(self):
        """Inits plugin instance."""
        if not self.plugin:
            self.pluginclass = self.pyload.pgm.get_plugin_class(
                'hoster', self.pluginname)
            self.plugin = self.pluginclass(self)

    @lock(shared=True)
    def has_plugin(self):
        """Thread safe way to determine this file has initialized plugin
        attribute."""
        return self.plugin is not None

    def package(self):
        """Return package instance."""
        return self.manager.get_package(self.packageid)

    def set_status(self, status):
        self.status = statusmap[status]
        # needs to sync so status is written to database
        self.sync()

    def set_custom_status(self, msg, status='processing'):
        self.statusname = msg
        self.set_status(status)

    def get_status_name(self):
        if self.status not in (15, 16) or not self.statusname:
            return self.manager.status_msg[self.status]
        else:
            return self.statusname

    def has_status(self, status):
        return statusmap[status] == self.status

    def sync(self):
        """Sync File instance with database."""
        self.manager.update_file(self)

    @lock
    def release(self):
        """Sync and remove from cache."""
        if self.plugin is not None:
            self.plugin.clean()
            self.plugin = None

        self.manager.release_file(self.fid)

    def to_info_data(self):
        return FileInfo(
            self.fid, self.get_name(), self.packageid, self.owner, self.size,
            self.filestatus, self.media, self.added, self.fileorder,
            DownloadInfo(self.url, self.pluginname, self.hash, self.status,
                         self.get_status_name(), self.error))

    def get_path(self):
        raise NotImplementedError

    def move(self, pid):
        raise NotImplementedError

    # TODO: Recheck
    def abort_download(self):
        """Abort file if possible."""
        while self.fid in self.pyload.tsm.processing_ids():
            if self.plugin and self.plugin.req:
                self.lock.acquire(shared=True)
                try:
                    self.abort = True
                    self.plugin.req.abort()
                    if self.plugin.dl:
                        self.plugin.dl.abort()
                finally:
                    self.lock.release()

            time.sleep(0.5)

        self.abort = False
        self.set_status('aborted')
        self.release()

    def finish_if_done(self):
        """Set status to finish and release file if every thread is finished
        with it."""
        # TODO: this is wrong now, it should check if addons are using it
        if self.fid in self.pyload.tsm.processing_ids():
            return False

        self.set_status('finished')
        self.release()
        self.manager.check_all_links_finished()
        return True

    def check_if_processed(self):
        self.manager.check_all_links_processed(self.fid)

    @trycatch(0)
    def get_speed(self):
        """Calculates speed."""
        return self.plugin.dl.speed

    @trycatch(0)
    def get_eta(self):
        """Gets estimated time of arrival / or waiting time."""
        if self.status == DownloadStatus.Waiting:
            return self.wait_until - time.time()

        return self.get_bytes_left() // self.get_speed()

    @trycatch(0)
    def get_bytes_arrived(self):
        """Gets bytes arrived."""
        return self.plugin.dl.arrived

    @trycatch(0)
    def get_bytes_left(self):
        """Gets bytes left."""
        return self.plugin.dl.size - self.plugin.dl.arrived

    @trycatch(0)
    def get_flags(self):
        return self.plugin.dl.flags

    def get_progress_info(self):
        return ProgressInfo(
            self.pluginname, self.name, self.get_status_name(), self.get_eta(),
            self.get_bytes_arrived(), self.size, self.owner,
            ProgressType.Download,
            DownloadProgress(self.fid, self.packageid, self.get_speed(),
                             self.get_flags(), self.status))
Exemplo n.º 7
0
class File(BaseObject):
    """Represents a file object at runtime."""
    __slots__ = ['_name', '_size', 'abort', 'added', 'error', 'fid',
                 'fileorder', 'filestatus', 'hash', 'lock', 'manager', 'media',
                 'owner', 'packageid', 'plugin', 'pluginclass', 'pluginname',
                 'pyload', 'reconnected', 'status', 'statusname', 'url',
                 'wait_until']

    @staticmethod
    def from_info_data(m, info):
        file = File(m, info.fid, info.name, info.size, info.status, info.media,
                    info.added, info.fileorder, '', '', '', DownloadStatus.NA,
                    '', info.package, info.owner)
        if info.download:
            file.url = info.download.url
            file.pluginname = info.download.plugin
            file.hash = info.download.hash
            file.status = info.download.status
            file.error = info.download.error

        file.init_plugin()
        return file

    def __init__(
        self, manager, fid, name, size, filestatus, media, added,
            fileorder, url, pluginname, hash, status, error, package, owner):
        super(File, self).__init__()

        self.manager = manager
        self.pyload = manager.pyload

        self.fid = int(fid)
        self._name = purge.name(name)
        self._size = int(size)
        self.filestatus = filestatus
        self.media = media
        self.added = added
        self.fileorder = fileorder
        self.url = url
        self.pluginname = pluginname
        self.hash = hash
        self.status = status
        self.error = error
        self.owner = owner
        self.packageid = package
        # database information ends here

        self.lock = RWLock()

        self.plugin = None

        self.wait_until = 0  # time.time() + time to wait

        # status attributes
        self.abort = False
        self.reconnected = False
        self.statusname = None

    def get_size(self):
        """Get size of download."""
        if self.plugin is not None and self.plugin.dl is not None \
                and self.plugin.dl.size is not None:
            self.set_size(self.plugin.dl.size)
        return self._size

    # NOTE: convert size to int
    def set_size(self, value):
        self._size = int(value)

    size = property(get_size, set_size)

    def get_name(self):
        try:
            if self.plugin.req.name:
                return self.plugin.req.name
            else:
                return self._name
        except Exception:
            return self._name

    def set_name(self, name):
        """Only set unicode or utf8 strings as name."""
        name = purge.name(name)

        # media type is updated if needed
        if self._name != name:
            self.media = guess_type(name)
            self._name = name

    name = property(get_name, set_name)

    def __repr__(self):
        return '<File {0}: {1}@{2}>'.format(
            self.fid, self.name, self.pluginname)

    @lock
    def init_plugin(self):
        """Inits plugin instance."""
        if not self.plugin:
            self.pluginclass = self.pyload.pgm.get_plugin_class(
                'hoster', self.pluginname)
            self.plugin = self.pluginclass(self)

    @lock(shared=True)
    def has_plugin(self):
        """Thread safe way to determine this file has initialized plugin
        attribute."""
        return self.plugin is not None

    def package(self):
        """Return package instance."""
        return self.manager.get_package(self.packageid)

    def set_status(self, status):
        self.status = statusmap[status]
        # needs to sync so status is written to database
        self.sync()

    def set_custom_status(self, msg, status='processing'):
        self.statusname = msg
        self.set_status(status)

    def get_status_name(self):
        if self.status not in (15, 16) or not self.statusname:
            return self.manager.status_msg[self.status]
        else:
            return self.statusname

    def has_status(self, status):
        return statusmap[status] == self.status

    def sync(self):
        """Sync File instance with database."""
        self.manager.update_file(self)

    @lock
    def release(self):
        """Sync and remove from cache."""
        if self.plugin is not None:
            self.plugin.clean()
            self.plugin = None

        self.manager.release_file(self.fid)

    def to_info_data(self):
        return FileInfo(self.fid, self.get_name(),
                        self.packageid, self.owner, self.size, self.filestatus,
                        self.media, self.added, self.fileorder,
                        DownloadInfo(
                            self.url, self.pluginname, self.hash, self.status,
                            self.get_status_name(),
                            self.error))

    def get_path(self):
        raise NotImplementedError

    def move(self, pid):
        raise NotImplementedError

    # TODO: Recheck
    def abort_download(self):
        """Abort file if possible."""
        while self.fid in self.pyload.tsm.processing_ids():
            if self.plugin and self.plugin.req:
                self.lock.acquire(shared=True)
                try:
                    self.abort = True
                    self.plugin.req.abort()
                    if self.plugin.dl:
                        self.plugin.dl.abort()
                finally:
                    self.lock.release()

            time.sleep(0.5)

        self.abort = False
        self.set_status('aborted')
        self.release()

    def finish_if_done(self):
        """Set status to finish and release file if every thread is finished
        with it."""
        # TODO: this is wrong now, it should check if addons are using it
        if self.fid in self.pyload.tsm.processing_ids():
            return False

        self.set_status('finished')
        self.release()
        self.manager.check_all_links_finished()
        return True

    def check_if_processed(self):
        self.manager.check_all_links_processed(self.fid)

    @trycatch(0)
    def get_speed(self):
        """Calculates speed."""
        return self.plugin.dl.speed

    @trycatch(0)
    def get_eta(self):
        """Gets estimated time of arrival / or waiting time."""
        if self.status == DownloadStatus.Waiting:
            return self.wait_until - time.time()

        return self.get_bytes_left() // self.get_speed()

    @trycatch(0)
    def get_bytes_arrived(self):
        """Gets bytes arrived."""
        return self.plugin.dl.arrived

    @trycatch(0)
    def get_bytes_left(self):
        """Gets bytes left."""
        return self.plugin.dl.size - self.plugin.dl.arrived

    @trycatch(0)
    def get_flags(self):
        return self.plugin.dl.flags

    def get_progress_info(self):
        return ProgressInfo(
            self.pluginname, self.name, self.get_status_name(),
            self.get_eta(),
            self.get_bytes_arrived(),
            self.size, self.owner, ProgressType.Download,
            DownloadProgress(
                self.fid, self.packageid, self.get_speed(),
                self.get_flags(),
                self.status))