Mercurial > libervia-backend
comparison src/plugins/plugin_misc_file.py @ 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 |
parents | |
children | ebf97c1ac14a |
comparison
equal
deleted
inserted
replaced
1524:7b0fcefd52d4 | 1525:49d33cb48207 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin for file tansfer | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from sat.core.i18n import _ | |
21 from sat.core.constants import Const as C | |
22 from sat.core.log import getLogger | |
23 log = getLogger(__name__) | |
24 import os | |
25 from twisted.internet import defer | |
26 import uuid | |
27 | |
28 | |
29 PLUGIN_INFO = { | |
30 "name": "File Tansfer", | |
31 "import_name": "FILE", | |
32 "type": C.PLUG_TYPE_MISC, | |
33 "main": "FilePlugin", | |
34 "handler": "no", | |
35 "description": _("""File Tansfer Management: | |
36 This plugin manage the various ways of sending a file, and choose the best one.""") | |
37 } | |
38 | |
39 | |
40 class SatFile(file): | |
41 """A file-like object to have high level files manipulation""" | |
42 # TODO: manage "with" statement | |
43 | |
44 def __init__(self, host, path, mode='r', uid=None, size=None, profile=C.PROF_KEY_NONE): | |
45 """ | |
46 @param host: %(doc_host)s | |
47 @param path(str): path of the file to get | |
48 @param mode(str): same as for built-in "open" function | |
49 @param uid(unicode, None): unique id identifing this progressing element | |
50 will be automaticaly generated if None | |
51 @param size(None, int): size of the file | |
52 """ | |
53 self.host = host | |
54 self.uid = uid or unicode(uuid.uuid4()) | |
55 self._file = open(path, mode) | |
56 self.size = None | |
57 self.profile = profile | |
58 self.eof = defer.Deferred() | |
59 self.host.registerProgressCb(self.uid, self.getProgress, profile) | |
60 self.host.bridge.progressStarted(self.uid, self.profile) | |
61 self.eof.addCallback(lambda ignore: self.host.bridge.progressFinished(self.uid, self.profile)) | |
62 self.eof.addErrback(lambda failure: self.host.bridge.progressError(self.uid, unicode(failure), self.profile)) | |
63 | |
64 def close(self): | |
65 self._file.close() | |
66 self.host.removeProgressCb(self.uid, self.profile) | |
67 | |
68 def read(self, size=-1): | |
69 read = self._file.read(size) | |
70 if not read: | |
71 self.eof.callback(None) | |
72 return read | |
73 | |
74 def seek(self, offset, whence=os.SEEK_SET): | |
75 self._file.seek(offset, whence) | |
76 | |
77 def tell(self): | |
78 return self._file.tell() | |
79 | |
80 def getProgress(self, progress_id, data, profile): | |
81 return {'position': self._file.tell(), 'size': self.size or 0} | |
82 | |
83 | |
84 class FilePlugin(object): | |
85 File=SatFile | |
86 | |
87 def __init__(self, host): | |
88 log.info(_("plugin File initialization")) | |
89 self.host = host | |
90 | |
91 |