Mercurial > libervia-backend
changeset 1525:49d33cb48207
plugin file: first draft:
This plugin will be used to manage file transfers, and choose the best available method.
It embed a class dedicated to files: SatFile, which act as a file objects but manage some high level stuffs (like progression).
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 25 Sep 2015 19:19:12 +0200 (2015-09-25) |
parents | 7b0fcefd52d4 |
children | bb451fd1cea3 |
files | src/plugins/plugin_misc_file.py |
diffstat | 1 files changed, 91 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/plugins/plugin_misc_file.py Fri Sep 25 19:19:12 2015 +0200 @@ -0,0 +1,91 @@ +#!/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 + +