annotate sat/tools/stream.py @ 3942:a92eef737703

plugin XEP-0373: download public keys if they are not found in local storage: public keys were only obtained from PEP notifications, however this wasn't working if the entity was not in our roster. Now if no public key is retrieved from local storage, the public key node is requested, and an error is raised if nothing is found. This allows the use of OX with entities which are not in roster. rel 380
author Goffi <goffi@goffi.org>
date Sat, 15 Oct 2022 20:38:33 +0200
parents e4631d073c8b
children 0dd79c6cc1d2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
3480
7550ae9cfbac Renamed the project from "Salut à Toi" to "Libervia":
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
3 # Libervia: an XMPP client
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3182
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 """ interfaces """
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
3087
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
21 import uuid
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
22 import os
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
23 from zope import interface
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core import exceptions
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core.constants import Const as C
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.core.log import getLogger
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.protocols import basic
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from twisted.internet import interfaces
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 log = getLogger(__name__)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 class IStreamProducer(interface.Interface):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 def startStream(consumer):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 """start producing the stream
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 @return (D): deferred fired when stream is finished
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 """
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
39 pass
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
3087
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
42 class SatFile:
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 """A file-like object to have high level files manipulation"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
44
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 # TODO: manage "with" statement
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
47 def __init__(self, host, client, path, mode="rb", uid=None, size=None, data_cb=None,
3087
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
48 auto_end_signals=True, check_size_with_read=False):
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 """
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 @param host: %(doc_host)s
3087
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
51 @param path(Path, str): path to the file to get or write to
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 @param mode(str): same as for built-in "open" function
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 @param uid(unicode, None): unique id identifing this progressing element
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 This uid will be used with self.host.progressGet
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 will be automaticaly generated if None
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 @param size(None, int): size of the file (when known in advance)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 @param data_cb(None, callable): method to call on each data read/write
3087
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
58 can be used to do processing like calculating hash.
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
59 if data_cb return a non None value, it will be used instead of the
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
60 data read/to write
2866
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
61 @param auto_end_signals(bool): if True, progressFinished and progressError signals
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
62 are automatically sent.
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
63 if False, you'll have to call self.progressFinished and self.progressError
8ce5748bfe97 plugin XEP-0363: updated to namespace "urn:xmpp:http:upload:0", handle headers
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
64 yourself.
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 progressStarted signal is always sent automatically
3924
e4631d073c8b tools (stream): log error when a SatFile is closed:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
66 @param check_size_with_read(bool): if True, size will be checked using number of
3087
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
67 bytes read or written. This is useful when data_cb modifiy len of file.
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.host = host
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self.profile = client.profile
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
71 self.uid = uid or str(uuid.uuid4())
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self._file = open(path, mode)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self.size = size
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 self.data_cb = data_cb
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self.auto_end_signals = auto_end_signals
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 metadata = self.getProgressMetadata()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
77 self.host.registerProgressCb(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
78 self.uid, self.getProgress, metadata, profile=client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
79 )
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 self.host.bridge.progressStarted(self.uid, metadata, client.profile)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
3087
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
82 self._transfer_count = 0 if check_size_with_read else None
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
83
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
84 @property
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
85 def check_size_with_read(self):
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
86 return self._transfer_count is not None
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
87
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
88 @check_size_with_read.setter
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
89 def check_size_with_read(self, value):
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
90 if value and self._transfer_count is None:
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
91 self._transfer_count = 0
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
92 else:
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
93 self._transfer_count = None
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
94
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 def checkSize(self):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 """Check that current size correspond to given size
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 must be used when the transfer is supposed to be finished
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 @return (bool): True if the position is the same as given size
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 @raise exceptions.NotFound: size has not be specified
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 """
3087
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
102 if self.check_size_with_read:
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
103 position = self._transfer_count
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
104 else:
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
105 position = self._file.tell()
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 if self.size is None:
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 raise exceptions.NotFound
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 return position == self.size
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 def close(self, progress_metadata=None, error=None):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 """Close the current file
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
112
3087
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
113 @param progress_metadata(None, dict): metadata to send with _onProgressFinished
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
114 message
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
115 @param error(None, unicode): set to an error message if progress was not
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
116 successful
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 mutually exclusive with progress_metadata
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 error can happen even if error is None, if current size differ from given size
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 """
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 if self._file.closed:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
121 return # avoid double close (which is allowed) error
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 if error is None:
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 try:
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 size_ok = self.checkSize()
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 except exceptions.NotFound:
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 size_ok = True
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 if not size_ok:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2866
diff changeset
128 error = "declared and actual size mismatch"
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 log.warning(error)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 progress_metadata = None
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
131
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 self._file.close()
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
133
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 if self.auto_end_signals:
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 if error is None:
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 self.progressFinished(progress_metadata)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 else:
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 assert progress_metadata is None
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 self.progressError(error)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
140
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 self.host.removeProgressCb(self.uid, self.profile)
3924
e4631d073c8b tools (stream): log error when a SatFile is closed:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
142 if error is not None:
e4631d073c8b tools (stream): log error when a SatFile is closed:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
143 log.error(f"file {self._file} closed with an error: {error}")
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
144
3087
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
145 @property
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
146 def closed(self):
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
147 return self._file.closed
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
148
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 def progressFinished(self, metadata=None):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 if metadata is None:
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 metadata = {}
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 self.host.bridge.progressFinished(self.uid, metadata, self.profile)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
153
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 def progressError(self, error):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 self.host.bridge.progressError(self.uid, error, self.profile)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
156
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 def flush(self):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 self._file.flush()
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
159
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 def write(self, buf):
3087
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
161 if self.data_cb is not None:
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
162 ret = self.data_cb(buf)
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
163 if ret is not None:
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
164 buf = ret
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
165 if self._transfer_count is not None:
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
166 self._transfer_count += len(buf)
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 self._file.write(buf)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
168
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 def read(self, size=-1):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 read = self._file.read(size)
3087
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
171 if self.data_cb is not None:
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
172 ret = self.data_cb(read)
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
173 if ret is not None:
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
174 read = ret
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
175 if self._transfer_count is not None:
a51f7fce1e2c tools (stream): data modification on SatFile:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
176 self._transfer_count += len(read)
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 return read
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
178
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 def seek(self, offset, whence=os.SEEK_SET):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 self._file.seek(offset, whence)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
181
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 def tell(self):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 return self._file.tell()
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
184
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 def mode(self):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 return self._file.mode()
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
187
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 def getProgressMetadata(self):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 """Return progression metadata as given to progressStarted
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
190
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 @return (dict): metadata (check bridge for documentation)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
193 metadata = {"type": C.META_TYPE_FILE}
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
194
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 mode = self._file.mode
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
196 if "+" in mode:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
197 pass # we have no direction in read/write modes
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
198 elif mode in ("r", "rb"):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
199 metadata["direction"] = "out"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
200 elif mode in ("w", "wb"):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
201 metadata["direction"] = "in"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
202 elif "U" in mode:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
203 metadata["direction"] = "out"
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 else:
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 raise exceptions.InternalError
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
206
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
207 metadata["name"] = self._file.name
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
208
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 return metadata
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
210
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 def getProgress(self, progress_id, profile):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
212 ret = {"position": self._file.tell()}
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 if self.size:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
214 ret["size"] = self.size
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 return ret
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
216
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
217
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 @interface.implementer(IStreamProducer)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 @interface.implementer(interfaces.IConsumer)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 class FileStreamObject(basic.FileSender):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 def __init__(self, host, client, path, **kwargs):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 """
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
223
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 A SatFile will be created and put in self.file_obj
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 @param path(unicode): path to the file
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 @param **kwargs: kw arguments to pass to SatFile
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 """
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 self.file_obj = SatFile(host, client, path, **kwargs)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
229
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 def registerProducer(self, producer, streaming):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 pass
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
232
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 def startStream(self, consumer):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 return self.beginFileTransfer(self.file_obj, consumer)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
235
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 def write(self, data):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 self.file_obj.write(data)
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
238
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 def close(self, *args, **kwargs):
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 self.file_obj.close(*args, **kwargs)