view src/plugins/plugin_misc_file.py @ 1533:d749922300d0

core: removed forgotten breakpoint
author Goffi <goffi@goffi.org>
date Tue, 29 Sep 2015 17:54:21 +0200
parents 49d33cb48207
children ebf97c1ac14a
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

# SAT plugin for file tansfer
# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core.log import getLogger
log = getLogger(__name__)
import os
from twisted.internet import defer
import uuid


PLUGIN_INFO = {
    "name": "File Tansfer",
    "import_name": "FILE",
    "type": C.PLUG_TYPE_MISC,
    "main": "FilePlugin",
    "handler": "no",
    "description": _("""File Tansfer Management:
This plugin manage the various ways of sending a file, and choose the best one.""")
}


class SatFile(file):
    """A file-like object to have high level files manipulation"""
    # TODO: manage "with" statement

    def __init__(self, host, path, mode='r', uid=None, size=None, profile=C.PROF_KEY_NONE):
        """
        @param host: %(doc_host)s
        @param path(str): path of the file to get
        @param mode(str): same as for built-in "open" function
        @param uid(unicode, None): unique id identifing this progressing element
            will be automaticaly generated if None
        @param size(None, int): size of the file
        """
        self.host = host
        self.uid = uid or unicode(uuid.uuid4())
        self._file = open(path, mode)
        self.size = None
        self.profile = profile
        self.eof = defer.Deferred()
        self.host.registerProgressCb(self.uid, self.getProgress, profile)
        self.host.bridge.progressStarted(self.uid, self.profile)
        self.eof.addCallback(lambda ignore: self.host.bridge.progressFinished(self.uid, self.profile))
        self.eof.addErrback(lambda failure: self.host.bridge.progressError(self.uid, unicode(failure), self.profile))

    def close(self):
        self._file.close()
        self.host.removeProgressCb(self.uid, self.profile)

    def read(self, size=-1):
        read = self._file.read(size)
        if not read:
            self.eof.callback(None)
        return read

    def seek(self, offset, whence=os.SEEK_SET):
        self._file.seek(offset, whence)

    def tell(self):
        return self._file.tell()

    def getProgress(self, progress_id, data, profile):
        return {'position': self._file.tell(), 'size': self.size or 0}


class FilePlugin(object):
    File=SatFile

    def __init__(self, host):
        log.info(_("plugin File initialization"))
        self.host = host