typeing
This commit is contained in:
parent
bc35421760
commit
daee891825
6 changed files with 99 additions and 113 deletions
|
@ -1,7 +1,7 @@
|
|||
# -*- mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*-
|
||||
from ctypes import *
|
||||
from datetime import datetime
|
||||
from typing import Union, Callable
|
||||
from typing import Union, Callable, Union
|
||||
|
||||
try:
|
||||
from wrapper.libtox import LibToxCore
|
||||
|
@ -258,7 +258,7 @@ class Tox:
|
|||
"""
|
||||
return int(Tox.libtoxcore.tox_get_savedata_size(self._tox_pointer))
|
||||
|
||||
def get_savedata(self, savedata=None) -> str:
|
||||
def get_savedata(self, savedata=None) -> bytes:
|
||||
"""
|
||||
Store all information associated with the tox instance to a byte array.
|
||||
|
||||
|
@ -348,6 +348,7 @@ class Tox:
|
|||
'address, or the IP address passed was invalid.')
|
||||
if tox_err_bootstrap == TOX_ERR_BOOTSTRAP['BAD_PORT']:
|
||||
raise ArgumentError('The port passed was invalid. The valid port range is (1, 65535).')
|
||||
raise ToxError('The function did not return OK')
|
||||
|
||||
def self_get_connection_status(self) -> int:
|
||||
"""
|
||||
|
@ -420,10 +421,10 @@ class Tox:
|
|||
|
||||
# Internal client information (Tox address/id)
|
||||
|
||||
def self_get_toxid(self, address=None) -> str:
|
||||
def self_get_toxid(self, address: Union[bytes, None]=None) -> str:
|
||||
return self.self_get_address(address)
|
||||
|
||||
def self_get_address(self, address=None) -> str:
|
||||
def self_get_address(self, address: Union[bytes, None]=None) -> str:
|
||||
"""
|
||||
Writes the Tox friend address of the client to a byte array. The address is not in human-readable format. If a
|
||||
client wants to display the address, formatting is required.
|
||||
|
@ -456,7 +457,7 @@ class Tox:
|
|||
"""
|
||||
return int(Tox.libtoxcore.tox_self_get_nospam(self._tox_pointer))
|
||||
|
||||
def self_get_public_key(self, public_key: str=None) -> str:
|
||||
def self_get_public_key(self, public_key: Union[bytes, None] = None) -> str:
|
||||
"""
|
||||
Copy the Tox Public Key (long term) from the Tox object.
|
||||
|
||||
|
@ -470,7 +471,7 @@ class Tox:
|
|||
Tox.libtoxcore.tox_self_get_public_key(self._tox_pointer, public_key)
|
||||
return bin_to_string(public_key, TOX_PUBLIC_KEY_SIZE)
|
||||
|
||||
def self_get_secret_key(self, secret_key=None) -> str:
|
||||
def self_get_secret_key(self, secret_key: Union[bytes, None]=None) -> str:
|
||||
"""
|
||||
Copy the Tox Secret Key from the Tox object.
|
||||
|
||||
|
@ -505,12 +506,12 @@ class Tox:
|
|||
byref(tox_err_set_info))
|
||||
tox_err_set_info = tox_err_set_info.value
|
||||
if tox_err_set_info == TOX_ERR_SET_INFO['OK']:
|
||||
return True # was bool(result)
|
||||
return bool(result)
|
||||
elif tox_err_set_info == TOX_ERR_SET_INFO['NULL']:
|
||||
raise ArgumentError('One of the arguments to the function was NULL when it was not expected.')
|
||||
elif tox_err_set_info == TOX_ERR_SET_INFO['TOO_LONG']:
|
||||
raise ArgumentError('Information length exceeded maximum permissible size.')
|
||||
return False # was
|
||||
raise ToxError('The function did not return OK')
|
||||
|
||||
def self_get_name_size(self) -> int:
|
||||
"""
|
||||
|
@ -523,7 +524,7 @@ class Tox:
|
|||
retval = Tox.libtoxcore.tox_self_get_name_size(self._tox_pointer)
|
||||
return int(retval)
|
||||
|
||||
def self_get_name(self, name: str=None) -> str:
|
||||
def self_get_name(self, name: Union[bytes,None]=None) -> str:
|
||||
"""
|
||||
Write the nickname set by tox_self_set_name to a byte array.
|
||||
|
||||
|
@ -580,7 +581,7 @@ class Tox:
|
|||
"""
|
||||
return Tox.libtoxcore.tox_self_get_status_message_size(self._tox_pointer)
|
||||
|
||||
def self_get_status_message(self, status_message: str=None) -> str:
|
||||
def self_get_status_message(self, status_message: Union[bytes,None]=None) -> str:
|
||||
"""
|
||||
Write the status message set by tox_self_set_status_message to a byte array.
|
||||
|
||||
|
@ -1877,17 +1878,7 @@ class Tox:
|
|||
nick = bytes(nick, 'utf-8')
|
||||
if type(group_name) != bytes:
|
||||
group_name = bytes(group_name, 'utf-8')
|
||||
if False: # API change
|
||||
peer_info = self.group_self_peer_info_new()
|
||||
peer_info.contents.nick = c_char_p(nick)
|
||||
peer_info.contents.nick_length = len(nick)
|
||||
peer_info.contents.user_status = status
|
||||
result = Tox.libtoxcore.tox_group_new(self._tox_pointer,
|
||||
privacy_state,
|
||||
group_name,
|
||||
c_size_t(len(group_name)),
|
||||
peer_info, byref(error))
|
||||
else:
|
||||
if True:
|
||||
cgroup_name = c_char_p(group_name)
|
||||
result = Tox.libtoxcore.tox_group_new(self._tox_pointer,
|
||||
privacy_state,
|
||||
|
@ -2203,7 +2194,8 @@ class Tox:
|
|||
error = c_int()
|
||||
key = create_string_buffer(TOX_GROUP_PEER_PUBLIC_KEY_SIZE)
|
||||
LOG_DEBUG(f"tox.group_self_get_public_key")
|
||||
result = Tox.libtoxcore.tox_group_self_get_public_key(self._tox_pointer, c_uint32(group_number),
|
||||
result = Tox.libtoxcore.tox_group_self_get_public_key(self._tox_pointer,
|
||||
c_uint32(group_number),
|
||||
key, byref(error))
|
||||
if error.value:
|
||||
LOG_ERROR(f"tox.group_self_get_public_key {TOX_ERR_FRIEND_GET_PUBLIC_KEY[error.value]}")
|
||||
|
@ -2452,7 +2444,9 @@ class Tox:
|
|||
size = self.group_get_topic_size(group_number)
|
||||
topic = create_string_buffer(size)
|
||||
LOG_DEBUG(f"tox.group_get_topic")
|
||||
result = Tox.libtoxcore.tox_group_get_topic(self._tox_pointer, c_uint32(group_number), topic, byref(error))
|
||||
result = Tox.libtoxcore.tox_group_get_topic(self._tox_pointer,
|
||||
c_uint32(group_number),
|
||||
topic, byref(error))
|
||||
if error.value:
|
||||
LOG_ERROR(f" err={error.value}")
|
||||
raise ToxError(f" err={error.value}")
|
||||
|
@ -2488,7 +2482,8 @@ class Tox:
|
|||
size = self.group_get_name_size(group_number)
|
||||
name = create_string_buffer(size)
|
||||
LOG_DEBUG(f"tox.group_get_name")
|
||||
result = Tox.libtoxcore.tox_group_get_name(self._tox_pointer, c_uint32(group_number),
|
||||
result = Tox.libtoxcore.tox_group_get_name(self._tox_pointer,
|
||||
c_uint32(group_number),
|
||||
name, byref(error))
|
||||
if error.value:
|
||||
LOG_ERROR(f"group_get_name err={error.value}")
|
||||
|
|
|
@ -59,8 +59,15 @@ TOX_ERR_SET_INFO = {
|
|||
'OK': 0,
|
||||
'NULL': 1,
|
||||
'TOO_LONG': 2,
|
||||
# The function returned successfully.
|
||||
'TOX_ERR_SET_INFO_OK': 0,
|
||||
# One of the arguments to the function was NULL when it was not expected.
|
||||
'TOX_ERR_SET_INFO_NULL': 1,
|
||||
# Information length exceeded maximum permissible size.
|
||||
'TOX_ERR_SET_INFO_TOO_LONG': 2,
|
||||
}
|
||||
|
||||
|
||||
TOX_ERR_FRIEND_ADD = {
|
||||
'OK': 0,
|
||||
'NULL': 1,
|
||||
|
|
|
@ -60,7 +60,7 @@ class ToxEncryptSave:
|
|||
|
||||
:return: output array
|
||||
"""
|
||||
out = create_string_buffer(len(data) - TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
|
||||
out = create_string_buffer(len(data) - enum.TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
|
||||
tox_err_decryption = c_int()
|
||||
if type(password) != bytes:
|
||||
password = bytes(password, 'utf-8')
|
||||
|
|
|
@ -17,9 +17,7 @@ import threading
|
|||
import random
|
||||
from ctypes import *
|
||||
import argparse
|
||||
|
||||
from time import sleep
|
||||
from os.path import exists
|
||||
import time
|
||||
|
||||
# LOG=util.log
|
||||
global LOG
|
||||
|
@ -36,12 +34,11 @@ import wrapper
|
|||
import wrapper.toxcore_enums_and_consts as enums
|
||||
from wrapper.tox import Tox, UINT32_MAX, ToxError
|
||||
from wrapper.toxcore_enums_and_consts import TOX_CONNECTION, TOX_USER_STATUS, \
|
||||
TOX_MESSAGE_TYPE, TOX_PUBLIC_KEY_SIZE, TOX_FILE_CONTROL
|
||||
TOX_MESSAGE_TYPE, TOX_PUBLIC_KEY_SIZE, TOX_FILE_CONTROL, TOX_FILE_KIND
|
||||
|
||||
import wrapper_tests.support_testing as ts
|
||||
from wrapper_tests.support_testing import oMainArgparser
|
||||
|
||||
import time
|
||||
def sleep(fSec):
|
||||
if 'QtCore' in globals():
|
||||
if fSec > .000001: QtCore.QThread.msleep(fSec)
|
||||
|
@ -79,26 +76,26 @@ else:
|
|||
super(AV, self).__init__(core)
|
||||
self.core = self.get_tox()
|
||||
|
||||
def on_call(self, fid, audio_enabled, video_enabled):
|
||||
def on_call(self, fid, audio_enabled, video_enabled) -> None:
|
||||
LOG.info("Incoming %s call from %d:%s ..." % (
|
||||
"video" if video_enabled else "audio", fid,
|
||||
self.core.friend_get_name(fid)))
|
||||
bret = self.answer(fid, 48, 64)
|
||||
LOG.info(f"Answered, in call... {bret!s}")
|
||||
LOG.info(f"Answered, in call... {bret}")
|
||||
|
||||
def on_call_state(self, fid, state):
|
||||
def on_call_state(self, fid, state) -> None:
|
||||
LOG.info('call state:fn=%d, state=%d' % (fid, state))
|
||||
|
||||
def on_audio_bit_rate(self, fid, audio_bit_rate):
|
||||
def on_audio_bit_rate(self, fid, audio_bit_rate) -> None:
|
||||
LOG.info('audio bit rate status: fn=%d, abr=%d' %
|
||||
(fid, audio_bit_rate))
|
||||
|
||||
def on_video_bit_rate(self, fid, video_bit_rate):
|
||||
def on_video_bit_rate(self, fid, video_bit_rate) -> None:
|
||||
LOG.info('video bit rate status: fn=%d, vbr=%d' %
|
||||
(fid, video_bit_rate))
|
||||
|
||||
def on_audio_receive_frame(self, fid, pcm, sample_count,
|
||||
channels, sampling_rate):
|
||||
channels, sampling_rate) -> None:
|
||||
# LOG.info('audio frame: %d, %d, %d, %d' %
|
||||
# (fid, sample_count, channels, sampling_rate))
|
||||
# LOG.info('pcm len:%d, %s' % (len(pcm), str(type(pcm))))
|
||||
|
@ -109,7 +106,7 @@ else:
|
|||
if bret is False:
|
||||
LOG.error('on_audio_receive_frame error.')
|
||||
|
||||
def on_video_receive_frame(self, fid, width, height, frame, u, v):
|
||||
def on_video_receive_frame(self, fid, width, height, frame, u, v) -> None:
|
||||
LOG.info('video frame: %d, %d, %d, ' % (fid, width, height))
|
||||
sys.stdout.write('*')
|
||||
sys.stdout.flush()
|
||||
|
@ -117,7 +114,7 @@ else:
|
|||
if bret is False:
|
||||
LOG.error('on_video_receive_frame error.')
|
||||
|
||||
def witerate(self):
|
||||
def witerate(self) -> None:
|
||||
self.iterate()
|
||||
|
||||
|
||||
|
@ -140,7 +137,7 @@ class EchoBot():
|
|||
self.av = None
|
||||
self.on_connection_status = None
|
||||
|
||||
def start(self):
|
||||
def start(self) -> None:
|
||||
self.connect()
|
||||
if bHAVE_AV:
|
||||
# RuntimeError: Attempted to create a second session for the same Tox instance.
|
||||
|
@ -150,7 +147,7 @@ class EchoBot():
|
|||
public_key,
|
||||
message_data,
|
||||
message_data_size,
|
||||
*largs):
|
||||
*largs) -> None:
|
||||
key = ''.join(chr(x) for x in public_key[:TOX_PUBLIC_KEY_SIZE])
|
||||
sPk = wrapper.tox.bin_to_string(key, TOX_PUBLIC_KEY_SIZE)
|
||||
sMd = str(message_data, 'UTF-8')
|
||||
|
@ -164,14 +161,14 @@ class EchoBot():
|
|||
iMessageType,
|
||||
message_data,
|
||||
message_data_size,
|
||||
*largs):
|
||||
*largs) -> None:
|
||||
sMd = str(message_data, 'UTF-8')
|
||||
LOG_debug(f"on_friend_message {iFriendNum}" +' ' +sMd)
|
||||
self.on_friend_message(iFriendNum, iMessageType, sMd)
|
||||
LOG.info('setting bobs_on_friend_message')
|
||||
self._tox.callback_friend_message(bobs_on_friend_message)
|
||||
|
||||
def bobs_on_file_chunk_request(iTox, fid, filenumber, position, length, *largs):
|
||||
def bobs_on_file_chunk_request(iTox, fid, filenumber, position, length, *largs) -> None:
|
||||
if length == 0:
|
||||
return
|
||||
|
||||
|
@ -180,7 +177,7 @@ class EchoBot():
|
|||
self._tox.callback_file_chunk_request(bobs_on_file_chunk_request)
|
||||
|
||||
def bobs_on_file_recv(iTox, fid, filenumber, kind, size, filename, *largs):
|
||||
LOG_info(f"on_file_recv {fid!s} {filenumber!s} {kind!s} {size!s} {filename}")
|
||||
LOG_info(f"on_file_recv {fid} {filenumber} {kind} {size} {filename}")
|
||||
if size == 0:
|
||||
return
|
||||
self.files[(fid, filenumber)] = {
|
||||
|
@ -191,7 +188,7 @@ class EchoBot():
|
|||
self._tox.file_control(fid, filenumber, TOX_FILE_CONTROL['RESUME'])
|
||||
|
||||
|
||||
def connect(self):
|
||||
def connect(self) -> None:
|
||||
if not self.on_connection_status:
|
||||
def on_connection_status(iTox, iCon, *largs):
|
||||
LOG_info('ON_CONNECTION_STATUS - CONNECTED ' + repr(iCon))
|
||||
|
@ -233,7 +230,7 @@ class EchoBot():
|
|||
except Exception as e:
|
||||
LOG.warn('error relay to ' + lElt[0])
|
||||
|
||||
def loop(self):
|
||||
def loop(self) -> None:
|
||||
if not self.av:
|
||||
self.start()
|
||||
checked = False
|
||||
|
@ -266,30 +263,30 @@ class EchoBot():
|
|||
|
||||
LOG.info('Ending loop.')
|
||||
|
||||
def iterate(self, n=100):
|
||||
def iterate(self, n=100) -> None:
|
||||
interval = self._tox.iteration_interval()
|
||||
for i in range(n):
|
||||
self._tox.iterate()
|
||||
sleep(interval / 1000.0)
|
||||
self._tox.iterate()
|
||||
|
||||
def on_friend_request(self, pk, message):
|
||||
def on_friend_request(self, pk, message) -> None:
|
||||
LOG.debug('Friend request from %s: %s' % (pk, message))
|
||||
self._tox.friend_add_norequest(pk)
|
||||
LOG.info('on_friend_request Accepted.')
|
||||
save_to_file(self._tox, sDATA_FILE)
|
||||
|
||||
def on_friend_message(self, friendId, type, message):
|
||||
def on_friend_message(self, friendId, type, message) -> None:
|
||||
name = self._tox.friend_get_name(friendId)
|
||||
LOG.debug('%s: %s' % (name, message))
|
||||
yMessage = bytes(message, 'UTF-8')
|
||||
self._tox.friend_send_message(friendId, TOX_MESSAGE_TYPE['NORMAL'], yMessage)
|
||||
LOG.info('EchoBot sent: %s' % message)
|
||||
|
||||
def on_file_recv_chunk(self, fid, filenumber, position, data):
|
||||
def on_file_recv_chunk(self, fid, filenumber, position, data) -> None:
|
||||
filename = self.files[(fid, filenumber)]['filename']
|
||||
size = self.files[(fid, filenumber)]['size']
|
||||
LOG.debug(f"on_file_recv_chunk {fid!s} {filenumber!s} {filename} {position/float(size)*100!s}")
|
||||
LOG.debug(f"on_file_recv_chunk {fid} {filenumber} {filename} {position/float(size)*100}")
|
||||
|
||||
if data is None:
|
||||
msg = "I got '{}', sending it back right away!".format(filename)
|
||||
|
@ -298,7 +295,7 @@ class EchoBot():
|
|||
self.files[(fid, 0)] = self.files[(fid, filenumber)]
|
||||
|
||||
length = self.files[(fid, filenumber)]['size']
|
||||
self.file_send(fid, 0, length, filename, filename)
|
||||
self._tox.file_send(fid, TOX_FILE_KIND['DATA'], length, filename)
|
||||
|
||||
del self.files[(fid, filenumber)]
|
||||
return
|
||||
|
@ -330,7 +327,7 @@ class BaseThread(threading.Thread):
|
|||
self._stop_thread = False
|
||||
self.name = name
|
||||
|
||||
def stop_thread(self, timeout=-1):
|
||||
def stop_thread(self, timeout=-1) -> None:
|
||||
self._stop_thread = True
|
||||
if timeout < 0:
|
||||
timeout = ts.iTHREAD_TIMEOUT
|
||||
|
@ -348,7 +345,7 @@ class ToxIterateThread(BaseThread):
|
|||
super().__init__(name='ToxIterateThread')
|
||||
self._tox = tox
|
||||
|
||||
def run(self):
|
||||
def run(self) -> None:
|
||||
while not self._stop_thread:
|
||||
self._tox.iterate()
|
||||
sleep(self._tox.iteration_interval() / 1000)
|
||||
|
@ -377,7 +374,7 @@ def oArgparse(lArgv):
|
|||
|
||||
return oArgs
|
||||
|
||||
def iMain(oArgs):
|
||||
def iMain(oArgs) -> int:
|
||||
global sDATA_FILE
|
||||
# oTOX_OPTIONS = ToxOptions()
|
||||
global oTOX_OPTIONS
|
||||
|
@ -429,38 +426,14 @@ def iMain(oArgs):
|
|||
iRet = 1
|
||||
return iRet
|
||||
|
||||
def main(largs=None):
|
||||
if largs is None: largs = []
|
||||
oArgs = oArgparse(largs)
|
||||
global oTOX_OARGS
|
||||
oTOX_OARGS = oArgs
|
||||
print(oArgs)
|
||||
|
||||
if coloredlogs:
|
||||
logger = logging.getLogger()
|
||||
# https://pypi.org/project/coloredlogs/
|
||||
coloredlogs.install(level=oArgs.loglevel,
|
||||
logger=logger,
|
||||
# %(asctime)s,%(msecs)03d %(hostname)s [%(process)d]
|
||||
fmt='%(name)s %(levelname)s %(message)s'
|
||||
)
|
||||
else:
|
||||
logging.basicConfig(level=oArgs.loglevel) # logging.INFO
|
||||
|
||||
return iMain(oArgs)
|
||||
|
||||
|
||||
def main(lArgs=None):
|
||||
global oTOX_OARGS
|
||||
def main(lArgs=None) -> int:
|
||||
global oTOX_OARGS
|
||||
global bIS_LOCAL
|
||||
if lArgs is None: lArgs = []
|
||||
oArgs = oArgparse(lArgs)
|
||||
global bIS_LOCAL
|
||||
bIS_LOCAL = oArgs.network in ['newlocal', 'localnew', 'local']
|
||||
oTOX_OARGS = oArgs
|
||||
setattr(oTOX_OARGS, 'bIS_LOCAL', bIS_LOCAL)
|
||||
bIS_LOCAL = True
|
||||
setattr(oTOX_OARGS, 'bIS_LOCAL', bIS_LOCAL)
|
||||
# oTOX_OPTIONS = ToxOptions()
|
||||
global oTOX_OPTIONS
|
||||
oTOX_OPTIONS = ts.oToxygenToxOptions(oArgs)
|
||||
if coloredlogs:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue