tools (strem): fix mode + add pre_close_cb callback:

rel 378
This commit is contained in:
Goffi 2022-10-31 04:04:32 +01:00
commit a53ab322f3

View file

@ -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 @@ class SatFile:
# 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 @@ class SatFile:
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 @@ class SatFile:
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 @@ class SatFile:
"""
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 @@ class SatFile:
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