Mercurial > libervia-backend
annotate src/plugins/plugin_misc_file.py @ 1568:1f7a34d499e0
plugins XEP-0234, file: use of SatFile for writing too
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 08 Nov 2015 14:44:33 +0100 |
parents | ebf97c1ac14a |
children | babd97d80049 |
rev | line source |
---|---|
1525 | 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 | |
1553 | 40 class SatFile(object): |
1525 | 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 | |
1568
1f7a34d499e0
plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents:
1553
diff
changeset
|
68 def flush(self): |
1f7a34d499e0
plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents:
1553
diff
changeset
|
69 self._file.flush() |
1f7a34d499e0
plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents:
1553
diff
changeset
|
70 |
1f7a34d499e0
plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents:
1553
diff
changeset
|
71 def write(self, buf): |
1f7a34d499e0
plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents:
1553
diff
changeset
|
72 self._file.write(buf) |
1f7a34d499e0
plugins XEP-0234, file: use of SatFile for writing too
Goffi <goffi@goffi.org>
parents:
1553
diff
changeset
|
73 |
1525 | 74 def read(self, size=-1): |
75 read = self._file.read(size) | |
76 if not read: | |
77 self.eof.callback(None) | |
78 return read | |
79 | |
80 def seek(self, offset, whence=os.SEEK_SET): | |
81 self._file.seek(offset, whence) | |
82 | |
83 def tell(self): | |
84 return self._file.tell() | |
85 | |
86 def getProgress(self, progress_id, data, profile): | |
87 return {'position': self._file.tell(), 'size': self.size or 0} | |
88 | |
89 | |
90 class FilePlugin(object): | |
91 File=SatFile | |
92 | |
93 def __init__(self, host): | |
94 log.info(_("plugin File initialization")) | |
95 self.host = host | |
96 | |
97 |