From cf5c5b160893a1b46de1ccc0d59312d14c0a6b69 Mon Sep 17 00:00:00 2001 From: emdee Date: Tue, 18 Oct 2022 00:23:39 +0000 Subject: [PATCH] Fixes --- toxygen/app.py | 18 ++-- toxygen/contacts/contacts_manager.py | 6 +- toxygen/main.py | 18 ++-- toxygen/messenger/messenger.py | 9 +- toxygen/middleware/threads.py | 24 ++---- toxygen/middleware/tox_factory.py | 24 +++--- toxygen/ui/main_screen.py | 124 +++++++++++++++++++-------- toxygen/user_data/profile_manager.py | 14 ++- toxygen/user_data/settings.py | 22 ++--- 9 files changed, 148 insertions(+), 111 deletions(-) diff --git a/toxygen/app.py b/toxygen/app.py index 340bca0..97b9978 100644 --- a/toxygen/app.py +++ b/toxygen/app.py @@ -4,7 +4,7 @@ import sys import traceback from random import shuffle import threading -from time import sleep, time +from time import sleep from gevent import monkey; monkey.patch_all(); del monkey # noqa import gevent @@ -75,7 +75,6 @@ from user_data.backup_service import BackupService import styles.style # TODO: dynamic loading import wrapper_tests.support_testing as ts -from wrapper_tests.tests_wrapper import test_bootstrap_iNmapInfo global LOG import logging @@ -488,7 +487,7 @@ class App: LOG.debug(f"_start_threads init: {te()!r}") # starting threads for tox iterate and toxav iterate - self._main_loop = threads.ToxIterateThread(self._tox, self) + self._main_loop = threads.ToxIterateThread(self._tox) self._main_loop.start() self._av_loop = threads.ToxAVIterateThread(self._tox.AV) @@ -775,8 +774,7 @@ class App: # FixMe: self._log = lambda line: LOG.log(self._oArgs.loglevel, self._ms.status(line)) - self._ms._log = self._log # used in callbacks.py - self.LOG = self._log # backwards + # self._ms._log = self._log # was used in callbacks.py if False: self.status_handler = logging.Handler() @@ -812,6 +810,7 @@ class App: def _init_callbacks(self, ms=None): LOG.debug("_init_callbacks") + # this will block if you are not connected callbacks.init_callbacks(self._tox, self._profile, self._settings, self._plugin_loader, self._contacts_manager, self._calls_manager, @@ -846,10 +845,10 @@ class App: sleep(interval / 1000.0) def _test_tox(self): - self.test_net(iMax=8) + self.test_net() self._ms.log_console() - def test_net(self, oThread=None, iMax=6): + def test_net(self, lElts=None, oThread=None, iMax=4): LOG.debug("test_net " +self._oArgs.network) # bootstrap @@ -905,9 +904,6 @@ class App: LOG.trace(f"Connected status #{i}: {status!r}") self.loop(2) - global iLAST_CONN - iLAST_CONN = time() - def _test_env(self): _settings = self._settings if 'proxy_type' not in _settings or _settings['proxy_type'] == 0 or \ @@ -961,7 +957,7 @@ class App: lElts = self._settings['current_nodes_tcp'] shuffle(lElts) try: - test_bootstrap_iNmapInfo(lElts) + bootstrap_iNodeInfo(lElts) except Exception as e: # json.decoder.JSONDecodeError LOG.error(f"test_tox ' +' : {e}") diff --git a/toxygen/contacts/contacts_manager.py b/toxygen/contacts/contacts_manager.py index 26a3302..04e12d3 100644 --- a/toxygen/contacts/contacts_manager.py +++ b/toxygen/contacts/contacts_manager.py @@ -93,10 +93,10 @@ class ContactsManager(ToxSave): def is_contact_active(self, contact): if not self._active_contact: - LOG.warn("No self._active_contact") +# LOG.debug("No self._active_contact") return False if self._active_contact not in self._contacts: - LOG.debug(f"_active_contact={self._active_contact} len={len(self._contacts)}") + LOG.warn(f"_active_contact={self._active_contact} not in contacts len={len(self._contacts)}") return False if not self._contacts[self._active_contact]: LOG.debug(f"{self._contacts[self._active_contact]} {contact.tox_id}") @@ -241,7 +241,7 @@ class ContactsManager(ToxSave): # AttributeError: 'NoneType' object has no attribute 'number' for (i, contact) in enumerate(self._contacts): if contact is None or not hasattr(contact, 'number'): - LOG.error("Contact {i} is None or not hasattr 'number'") + LOG.error(f"Contact {i} is None or not hasattr 'number'") del self._contacts[i] continue contacts = sorted(self._contacts, key=lambda c: c.number) diff --git a/toxygen/main.py b/toxygen/main.py index 966c9f8..e209469 100644 --- a/toxygen/main.py +++ b/toxygen/main.py @@ -35,19 +35,8 @@ with ts.ignoreStderr(): __maintainer__ = 'Ingvar' __version__ = '0.5.0+' -from PyQt5 import QtCore -import gevent -if 'QtCore' in sys.modules: - def qt_sleep(fSec): - if fSec > .001: - QtCore.QThread.msleep(int(fSec*1000.0)) - QtCore.QCoreApplication.processEvents() - sleep = qt_sleep -elif 'gevent' in sys.modules: - sleep = gevent.sleep -else: - import time - sleep = time.sleep +import time +sleep = time.sleep def reset(): Settings.reset_auto_profile() @@ -230,6 +219,9 @@ def main_parser(): parser.add_argument('--udp_enabled',type=str, default='True', choices=['True','False'], help='En/Disable udp') + parser.add_argument('--trace_enabled',type=str, + default='False', choices=['True','False'], + help='Debugging from toxcore logger_trace') parser.add_argument('--ipv6_enabled',type=str, default=bIpV6, choices=lIpV6Choices, help='En/Disable ipv6') diff --git a/toxygen/messenger/messenger.py b/toxygen/messenger/messenger.py index 9dca1a5..1a494d6 100644 --- a/toxygen/messenger/messenger.py +++ b/toxygen/messenger/messenger.py @@ -97,20 +97,19 @@ class Messenger(tox_save.ToxSave): :param friend_number: number of friend from Qt callback """ + if not text: + return if friend_number is None: friend_number = self._contacts_manager.get_active_number() - if friend_number is None: + if friend_number is None or friend_number < 0: LOG.error(f"No _contacts_manager.get_active_number") return - if not text or friend_number < 0: - return assert_main_thread() friend = self._get_friend_by_number(friend_number) if not friend: LOG.error(f"No self._get_friend_by_number") return - assert friend messages = self._split_message(text.encode('utf-8')) t = util.get_unix_time() for message in messages: @@ -336,8 +335,8 @@ class Messenger(tox_save.ToxSave): self._add_info_message(friend_number, text) def _add_info_message(self, friend_number, text): - assert friend friend = self._get_friend_by_number(friend_number) + assert friend message = InfoMessage(text, util.get_unix_time()) friend.append_message(message) if self._contacts_manager.is_friend_active(friend_number): diff --git a/toxygen/middleware/threads.py b/toxygen/middleware/threads.py index 5e7ab7b..229563d 100644 --- a/toxygen/middleware/threads.py +++ b/toxygen/middleware/threads.py @@ -10,18 +10,6 @@ from wrapper.toxcore_enums_and_consts import TOX_USER_STATUS, TOX_CONNECTION import wrapper_tests.support_testing as ts from utils import util -if 'QtCore' in sys.modules: - def qt_sleep(fSec): - if fSec > .001: - QtCore.QThread.msleep(int(fSec*1000.0)) - QtCore.QCoreApplication.processEvents() - sleep = qt_sleep -elif 'gevent' in sys.modules: - import gevent - sleep = gevent.sleep -else: - import time - sleep = time.sleep import time sleep = time.sleep @@ -63,7 +51,7 @@ class BaseThread(threading.Thread): if not self.is_alive(): break i = i + 1 else: - LOG_WARN(f"BaseThread {self.name} BLOCKED") + LOG_WARN(f"BaseThread {self.name} BLOCKED after {ts.iTHREAD_JOINS}") class BaseQThread(QtCore.QThread): @@ -101,12 +89,14 @@ class InitThread(BaseThread): self._is_first_start = is_first_start def run(self): + # DBUG+ InitThread run: ERROR name 'ts' is not defined + import wrapper_tests.support_testing as ts LOG_DEBUG('InitThread run: ') try: - if self._is_first_start and ts.bAreWeConnected(): - if self._settings['download_nodes_list']: - LOG_INFO('downloading list of nodes') - download_nodes_list(self._settings, oArgs=self._app._args) + if self._is_first_start and ts.bAreWeConnected() and \ + self._settings['download_nodes_list']: + LOG_INFO('downloading list of nodes') + download_nodes_list(self._settings, oArgs=self._app._args) if ts.bAreWeConnected(): LOG_INFO(f"calling test_net nodes") diff --git a/toxygen/middleware/tox_factory.py b/toxygen/middleware/tox_factory.py index 0ab4917..41d0044 100644 --- a/toxygen/middleware/tox_factory.py +++ b/toxygen/middleware/tox_factory.py @@ -22,7 +22,7 @@ def LOG_INFO(a): bVERBOSE = hasattr(__builtins__, 'app') and app.oArgs.loglevel <= 20 if bVERBOSE: print('INFO> '+a) def LOG_DEBUG(a): - bVERBOSE = hasattr(__builtins__, 'app') and app.oArgs.loglevel <= 10-1 + bVERBOSE = hasattr(__builtins__, 'app') and app.oArgs.loglevel <= 10 if bVERBOSE: print('DBUG> '+a) def LOG_TRACE(a): bVERBOSE = hasattr(__builtins__, 'app') and app.oArgs.loglevel < 10 @@ -39,18 +39,21 @@ def tox_log_cb(iTox, level, file, line, func, message, *args): * @param user_data The user data pointer passed to tox_new in options. """ try: - if type(file) == bytes: - file = str(file, 'UTF-8') - if file == 'network.c' and line in [944, 660]: return + file = str(file, 'UTF-8') # root WARNING 3network.c#944:b'send_packet'attempted to send message with network family 10 (probably IPv6) on IPv4 socket - if type(func) == bytes: - func = str(func, 'UTF-8') - if type(message) == bytes: - message = str(message, 'UTF-8') + if file == 'network.c' and line in [944, 660]: return + func = str(func, 'UTF-8') + message = str(message, 'UTF-8') message = f"{file}#{line}:{func} {message}" LOG_LOG(message) except Exception as e: - LOG_ERROR("tox_log_cb {e}") + LOG_ERROR(f"tox_log_cb {e}") + +#tox_log_handler (context=0x24763d0, +# level=LOGGER_LEVEL_TRACE, file=0x7fffe599fb99 "TCP_common.c", line=203, +# func=0x7fffe599fc50 <__func__.2> "read_TCP_packet", +# message=0x7fffba7fabd0 "recv buffer has 0 bytes, but requested 10 bytes", +# userdata=0x0) at /var/local/src/c-toxcore/toxcore/tox.c:78 def tox_factory(data=None, settings=None, args=None, app=None): """ @@ -98,7 +101,8 @@ def tox_factory(data=None, settings=None, args=None, app=None): LOG.debug("wrapper.tox.Tox settings: " +repr(settings)) - if tox_options._options_pointer: + if 'trace_enabled' in settings and settings['trace_enabled'] and \ + tox_options._options_pointer: c_callback = CFUNCTYPE(None, c_void_p, c_int, c_char_p, c_int, c_char_p, c_char_p, c_void_p) tox_options.self_logger_cb = c_callback(tox_log_cb) wrapper.tox.Tox.libtoxcore.tox_options_set_log_callback( diff --git a/toxygen/ui/main_screen.py b/toxygen/ui/main_screen.py index 339ebbd..b7b0c52 100644 --- a/toxygen/ui/main_screen.py +++ b/toxygen/ui/main_screen.py @@ -26,39 +26,75 @@ except Exception as e: LOG.warn(e) PythonConsole = None else: - def hl_format(color, style=''): - """Return a QTextCharFormat with the given attributes. - unused - """ - _color = QColor() - _color.setNamedColor(color) + if True: + # I want to do reverse video but I cant figure how + bg='white' + def hl_format(color, style=''): + """Return a QTextCharFormat with the given attributes. + """ + _color = QColor() + _color.setNamedColor(color) - _format = QTextCharFormat() - _format.setBackground(_color) - if 'bold' in style: - _format.setFontWeight(QFont.Bold) - if 'italic' in style: - _format.setFontItalic(True) + _format = QTextCharFormat() + _format.setForeground(_color) + if 'bold' in style: + _format.setFontWeight(QFont.Bold) + if 'italic' in style: + _format.setFontItalic(True) - _fgcolor = QColor() - _fgcolor.setNamedColor('white') - _format.setForeground(_fgcolor) - return _format + _bgcolor = QColor() + _bgcolor.setNamedColor(bg) + _format.setBackground(_bgcolor) + return _format - aFORMATS = { - 'keyword': hl.format('blue', 'bold'), - 'operator': hl.format('red'), - 'brace': hl.format('darkGray'), - 'defclass': hl.format('black', 'bold'), - 'string': hl.format('magenta'), - 'string2': hl.format('darkMagenta'), - 'comment': hl.format('darkGreen', 'italic'), - 'self': hl.format('black', 'italic'), - 'numbers': hl.format('brown'), - 'inprompt': hl.format('darkBlue', 'bold'), - 'outprompt': hl.format('darkRed', 'bold'), - } + aFORMATS = { + 'keyword': hl_format('blue', 'bold'), + 'operator': hl_format('red'), + 'brace': hl_format('darkGray'), + 'defclass': hl_format('black', 'bold'), + 'string': hl_format('magenta'), + 'string2': hl_format('darkMagenta'), + 'comment': hl_format('darkGreen', 'italic'), + 'self': hl_format('black', 'italic'), + 'numbers': hl_format('brown'), + 'inprompt': hl_format('darkBlue', 'bold'), + 'outprompt': hl_format('darkRed', 'bold'), + } + else: + bg = 'black' + def hl_format(color, style=''): + + """Return a QTextCharFormat with the given attributes. + unused + """ + _color = QColor() + _color.setNamedColor(color) + _format = QTextCharFormat() + _format.setForeground(_color) + if 'bold' in style: + _format.setFontWeight(QFont.Bold) + if 'italic' in style: + _format.setFontItalic(True) + + _bgcolor = QColor() + _bgcolor.setNamedColor(bg) + _format.setBackground(_bgcolor) + return _format + aFORMATS = { + 'keyword': hl_format('blue', 'bold'), + 'operator': hl_format('red'), + 'brace': hl_format('lightGray'), + 'defclass': hl_format('white', 'bold'), + 'string': hl_format('magenta'), + 'string2': hl_format('lightMagenta'), + 'comment': hl_format('lightGreen', 'italic'), + 'self': hl_format('white', 'italic'), + 'numbers': hl_format('lightBrown'), + 'inprompt': hl_format('lightBlue', 'bold'), + 'outprompt': hl_format('lightRed', 'bold'), + } + class QTextEditLogger(logging.Handler): def __init__(self, parent, app): @@ -177,8 +213,10 @@ class MainWindow(QtWidgets.QMainWindow): self.actionTest_tox = QtWidgets.QAction(window) self.actionTest_tox.setObjectName("actionTest_tox") - self.actionTest_socks = QtWidgets.QAction(window) - self.actionTest_socks.setObjectName("actionTest_socks") + self.actionTest_nmap = QtWidgets.QAction(window) + self.actionTest_nmap.setObjectName("actionTest_nmap") + self.actionTest_main = QtWidgets.QAction(window) + self.actionTest_main.setObjectName("actionTest_main") self.actionQuit_program = QtWidgets.QAction(window) self.actionQuit_program.setObjectName("actionQuit_program") @@ -231,7 +269,8 @@ class MainWindow(QtWidgets.QMainWindow): self.menuProfile.addAction(self.actionSettings) self.menuProfile.addAction(self.lockApp) self.menuProfile.addAction(self.actionTest_tox) - self.menuProfile.addAction(self.actionTest_socks) + self.menuProfile.addAction(self.actionTest_nmap) + self.menuProfile.addAction(self.actionTest_main) self.menuProfile.addAction(self.actionQuit_program) self.menuGC.addAction(self.createGC) @@ -260,7 +299,8 @@ class MainWindow(QtWidgets.QMainWindow): self.menubar.addAction(self.menuPlugins.menuAction()) self.menubar.addAction(self.menuAbout.menuAction()) - self.actionTest_socks.triggered.connect(self.test_socks) + self.actionTest_nmap.triggered.connect(self.test_nmap) + self.actionTest_main.triggered.connect(self.test_main) self.actionTest_tox.triggered.connect(self.test_tox) self.actionQuit_program.triggered.connect(self.quit_program) @@ -322,7 +362,8 @@ class MainWindow(QtWidgets.QMainWindow): self.actionLog_console.setText(util_ui.tr("Console Log")) self.actionPython_console.setText(util_ui.tr("Python Console")) self.actionTest_tox.setText(util_ui.tr("Bootstrap")) - self.actionTest_socks.setText(util_ui.tr("Test program")) + self.actionTest_nmap.setText(util_ui.tr("Test Nmap")) + self.actionTest_main.setText(util_ui.tr("Test Program")) self.actionQuit_program.setText(util_ui.tr("Quit program")) self.actionSettings.setText(util_ui.tr("Settings")) self.audioSettings.setText(util_ui.tr("Audio")) @@ -628,13 +669,17 @@ class MainWindow(QtWidgets.QMainWindow): try: if not self._pe: - self._pe = PythonConsole(sFont=font_name, - formats=aFORMATS, - bBold=True, - font_width=size) + self._pe = PythonConsole(formats=aFORMATS) self._pe.setWindowTitle('variable: app is the application') # self._pe.edit.setStyleSheet('foreground: white; background-color: black;}') # Fix the pyconsole geometry + + font = self._pe.edit.document().defaultFont() + font.setFamily(font_name) + font.setBold(True) + if font_width is None: + font_width = QFontMetrics(font).width('M') + self._pe.setFont(font) geometry = self._pe.geometry() geometry.setWidth(font_width*80+20) geometry.setHeight(font_width*40) @@ -736,6 +781,9 @@ class MainWindow(QtWidgets.QMainWindow): def test_nmap(self): self._app._test_nmap() + def test_main(self): + self._app._test_main() + def quit_program(self): try: self.close_window() diff --git a/toxygen/user_data/profile_manager.py b/toxygen/user_data/profile_manager.py index 7fd430a..6d7329f 100644 --- a/toxygen/user_data/profile_manager.py +++ b/toxygen/user_data/profile_manager.py @@ -63,9 +63,17 @@ class ProfileManager: def save_profile(self, data): if self._toxes.has_password(): data = self._toxes.pass_encrypt(data) - with open(self._path, 'wb') as fl: - fl.write(data) - LOG_INFO('Profile saved successfully to' +self._path) + try: + suf = f"{os.getpid()}" + with open(self._path+suf, 'wb') as fl: + fl.write(data) + stat = os.stat(self._path+suf) + if hasattr(stat, 'st_blocks'): + assert stat.st_blocks > 0, f"Zero length file {self._path+suf}" + os.rename(self._path+suf,self._path) + LOG_INFO('Profile saved successfully to' +self._path) + except Exception as e: + LOG_WARN(f"Profile save failed to {self._path}\n{e}") self._profile_saved_event(data) diff --git a/toxygen/user_data/settings.py b/toxygen/user_data/settings.py index 1b516a8..7fdcca5 100644 --- a/toxygen/user_data/settings.py +++ b/toxygen/user_data/settings.py @@ -138,9 +138,8 @@ class Settings(dict): self._profile_path = path.replace('.json', '.tox') self._toxes = toxes self._app = app - self._args = app._args - self.LOG = lambda l: LOG.log(self._args.loglevel, l) - self._log = self.LOG + self._oArgs = app._oArgs + self._log = lambda l: LOG.log(self._oArgs.loglevel, l) self._settings_saved_event = Event() if path and os.path.isfile(path): @@ -156,29 +155,29 @@ class Settings(dict): text = title + path LOG.error(title +str(ex)) util_ui.message_box(text, title) - info = Settings.get_default_settings(app._args) + info = Settings.get_default_settings(app._oArgs) user_data.settings.clean_settings(info) else: LOG.debug('get_default_settings for: ' + repr(path)) - info = Settings.get_default_settings(app._args) + info = Settings.get_default_settings(app._oArgs) if not os.path.exists(path): - merge_args_into_settings(app._args, info) + merge_oArgs_into_settings(app._oArgs, info) else: - aC = self._changed(app._args, info) + aC = self._changed(app._oArgs, info) if aC: title = 'Override profile with commandline - ' if path: title += os.path.basename(path) text = 'Override profile with command-line settings? \n' # text += '\n'.join([str(key) +'=' +str(val) for - # key,val in self._changed(app._args).items()]) + # key,val in self._changed(app._oArgs).items()]) text += repr(aC) reply = util_ui.question(text, title) if reply: - merge_args_into_settings(app._args, info) - info['audio'] = getattr(app._args, 'audio') - info['video'] = getattr(app._args, 'video') + merge_oArgs_into_settings(app._oArgs, info) + info['audio'] = getattr(app._oArgs, 'audio') + info['video'] = getattr(app._oArgs, 'video') super().__init__(info) self._upgrade() @@ -316,6 +315,7 @@ class Settings(dict): # FixMe: match? /var/local/src/c-toxcore/toxcore/tox.h 'ipv6_enabled': True, 'udp_enabled': True, + 'trace_enabled': False, 'local_discovery_enabled': True, 'dht_announcements_enabled': True, 'proxy_type': 0,