gajim-plural/gajim/common/file_props.py

173 lines
5.2 KiB
Python
Raw Normal View History

2012-06-14 18:27:23 +02:00
"""
2018-06-21 01:46:16 +02:00
This module is in charge of taking care of all the information related to
2012-06-14 18:27:23 +02:00
individual files. Files are identified by the account name and its sid.
>>> print(FilesProp.getFileProp('jabberid', '10'))
2012-06-14 18:27:23 +02:00
None
>>> fp = FilesProp()
Traceback (most recent call last):
...
Exception: this class should not be instatiated
>>> print(FilesProp.getAllFileProp())
2012-06-14 18:27:23 +02:00
[]
>>> fp = FilesProp.getNewFileProp('jabberid', '10')
>>> fp2 = FilesProp.getFileProp('jabberid', '10')
>>> fp == fp2
True
"""
from typing import Any # pylint: disable=unused-import
from typing import ClassVar # pylint: disable=unused-import
from typing import Dict # pylint: disable=unused-import
from typing import Tuple # pylint: disable=unused-import
2012-06-14 18:27:23 +02:00
class FilesProp:
_files_props = {} # type: ClassVar[Dict[Tuple[str, str], Any]]
2012-06-14 18:27:23 +02:00
def __init__(self):
2018-06-21 01:46:16 +02:00
raise Exception('this class should not be instantiated')
2012-06-14 18:27:23 +02:00
@classmethod
def getNewFileProp(cls, account, sid):
fp = FileProp(account, sid)
cls.setFileProp(fp, account, sid)
return fp
@classmethod
def getFileProp(cls, account, sid):
if (account, sid) in cls._files_props.keys():
return cls._files_props[account, sid]
@classmethod
def getFilePropByAccount(cls, account):
# Returns a list of file_props in one account
file_props = []
for account_, sid in cls._files_props:
if account_ == account:
2012-06-14 18:27:23 +02:00
file_props.append(cls._files_props[account, sid])
return file_props
@classmethod
def getFilePropByType(cls, type_, sid):
# This method should be deleted. Getting fileprop by type and sid is not
# unique enough. More than one fileprop might have the same type and sid
files_prop = cls.getAllFileProp()
for fp in files_prop:
if fp.type_ == type_ and fp.sid == sid:
return fp
@classmethod
def getFilePropBySid(cls, sid):
# This method should be deleted. It is kept to make things compatible
# This method should be replaced and instead get the file_props by
# account and sid
files_prop = cls.getAllFileProp()
for fp in files_prop:
if fp.sid == sid:
return fp
@classmethod
def getFilePropByTransportSid(cls, account, sid):
files_prop = cls.getAllFileProp()
for fp in files_prop:
if fp.account == account and fp.transport_sid == sid:
return fp
2012-06-14 18:27:23 +02:00
@classmethod
def getAllFileProp(cls):
return list(cls._files_props.values())
2012-06-14 18:27:23 +02:00
@classmethod
def setFileProp(cls, fp, account, sid):
cls._files_props[account, sid] = fp
@classmethod
def deleteFileProp(cls, file_prop):
files_props = cls._files_props
a = s = None
for account, sid in files_props:
fp = files_props[account, sid]
if fp is file_prop:
a = account
s = sid
2018-09-18 10:26:27 +02:00
if a is not None and s is not None:
2012-06-14 18:27:23 +02:00
del files_props[a, s]
class FileProp:
2012-06-14 18:27:23 +02:00
def __init__(self, account, sid):
2018-06-21 01:46:16 +02:00
# Do not instantiate this class directly. Call FilesProp.getNeFileProp
2012-06-14 18:27:23 +02:00
# instead
self.streamhosts = []
self.transfered_size = []
self.started = False
self.completed = False
self.paused = False
self.stalled = False
self.connected = False
self.stopped = False
self.is_a_proxy = False
self.proxyhost = None
self.proxy_sender = None
self.proxy_receiver = None
self.streamhost_used = None
# method callback called in case of transfer failure
self.failure_cb = None
# method callback called when disconnecting
self.disconnect_cb = None
self.continue_cb = None
self.sha_str = None
# transfer type: 's' for sending and 'r' for receiving
self.type_ = None
self.error = None
2012-10-13 23:32:19 +02:00
# Elapsed time of the file transfer
self.elapsed_time = 0
2012-06-14 18:27:23 +02:00
self.last_time = None
self.received_len = None
# full file path
self.file_name = None
self.name = None
self.date = None
2012-08-28 15:46:52 +02:00
self.desc = None
2012-06-14 18:27:23 +02:00
self.offset = None
self.sender = None
self.receiver = None
self.tt_account = None
self.size = None
self._sid = sid
self.transport_sid = None
2012-06-14 18:27:23 +02:00
self.account = account
self.mime_type = None
self.algo = None
self.direction = None
self.syn_id = None
self.seq = None
self.hash_ = None
self.fd = None
self.startexmpp = None
# Type of the session, if it is 'jingle' or 'si'
2012-06-14 18:27:23 +02:00
self.session_type = None
2012-06-14 18:45:11 +02:00
self.request_id = None
2012-06-20 23:58:23 +02:00
self.proxyhosts = None
self.dstaddr = None
2012-06-14 18:27:23 +02:00
def getsid(self):
# Getter of the property sid
return self._sid
def setsid(self, value):
# The sid value will change
# we need to change the in _files_props key as well
del FilesProp._files_props[self.account, self._sid]
self._sid = value
FilesProp._files_props[self.account, self._sid] = self
sid = property(getsid, setsid)
if __name__ == "__main__":
import doctest
doctest.testmod()