changeset 3968:0dd79c6cc1d2

tools (strem): fix `mode` + add `pre_close_cb` callback: rel 378
author Goffi <goffi@goffi.org>
date Mon, 31 Oct 2022 04:04:32 +0100
parents f461f11ea176
children 8e7d5796fb23
files sat/tools/stream.py
diffstat 1 files changed, 25 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/sat/tools/stream.py	Fri Oct 28 18:50:06 2022 +0200
+++ b/sat/tools/stream.py	Mon Oct 31 04:04:32 2022 +0100
@@ -18,15 +18,21 @@
 
 """ interfaces """
 
+from argparse import OPTIONAL
+from pathlib import Path
+from typing import Callable, Optional, Union
 import uuid
 import os
 from zope import interface
 from sat.core import exceptions
 from sat.core.constants import Const as C
+from sat.core.core_types import SatXMPPEntity
 from sat.core.log import getLogger
 from twisted.protocols import basic
 from twisted.internet import interfaces
 
+from sat.core.sat_main import SAT
+
 log = getLogger(__name__)
 
 
@@ -44,8 +50,19 @@
 
     # TODO: manage "with" statement
 
-    def __init__(self, host, client, path, mode="rb", uid=None, size=None, data_cb=None,
-                 auto_end_signals=True, check_size_with_read=False):
+    def __init__(
+        self,
+        host: SAT,
+        client: SatXMPPEntity,
+        path: Union[str, Path],
+        mode: str = "rb",
+        uid: Optional[str] = None,
+        size: Optional[int] = None,
+        data_cb: Optional[Callable] = None,
+        auto_end_signals: bool = True,
+        check_size_with_read: bool = False,
+        pre_close_cb: Optional[Callable]=None
+    ) -> None:
         """
         @param host: %(doc_host)s
         @param path(Path, str): path to the file to get or write to
@@ -65,6 +82,7 @@
             progressStarted signal is always sent automatically
         @param check_size_with_read(bool): if True, size will be checked using number of
             bytes read or written. This is useful when data_cb modifiy len of file.
+        @param pre_close_cb:
         """
         self.host = host
         self.profile = client.profile
@@ -73,6 +91,7 @@
         self.size = size
         self.data_cb = data_cb
         self.auto_end_signals = auto_end_signals
+        self.pre_close_cb = pre_close_cb
         metadata = self.getProgressMetadata()
         self.host.registerProgressCb(
             self.uid, self.getProgress, metadata, profile=client.profile
@@ -119,6 +138,8 @@
         """
         if self._file.closed:
             return  # avoid double close (which is allowed) error
+        if self.pre_close_cb is not None:
+            self.pre_close_cb()
         if error is None:
             try:
                 size_ok = self.checkSize()
@@ -182,8 +203,9 @@
     def tell(self):
         return self._file.tell()
 
+    @property
     def mode(self):
-        return self._file.mode()
+        return self._file.mode
 
     def getProgressMetadata(self):
         """Return progression metadata as given to progressStarted