From 9390c9bb9184513f93aaf364d08699ef6e28820a Mon Sep 17 00:00:00 2001 From: emdee Date: Sat, 17 Feb 2024 20:08:31 +0000 Subject: [PATCH] qweechat/__main__.py --- README.md | 25 ++++++--- qweechat/__main__.py | 124 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 6 deletions(-) create mode 100644 qweechat/__main__.py diff --git a/README.md b/README.md index cf27295..d147b71 100644 --- a/README.md +++ b/README.md @@ -20,22 +20,33 @@ Homepage: https://weechat.org/ QWeeChat requires: - Python ≥ 3.7 -- PyQt5 +- PyQt5 or PyQt5 and maybe PySide2 and PySide2 using qtpy - [WeeChat](https://weechat.org) ≥ 0.3.7, on local or remote machine, with relay plugin enabled and listening on a port with protocol "weechat" + ### Install via source distribution +Look at the Makefile and customize the variables; then ``` -$ pip install . +$ make install ``` ## WeeChat setup -You have to add a relay port in WeeChat, for example on port 1234: - +You have to add a relay port in WeeChat. +Follow the normal instructions for adding a ```relay``` to +[weechat](https://github.com/weechat/weechat) ``` -/set relay.network.password "mypass" -/relay add weechat 1234 +/relay add weechat 9000 +/relay start weechat +``` +or +``` +weechat -r '/relay add weechat 9000;/relay start weechat' +``` +and run qweechat either under toxygen, or standalone +``` +python3 -m qweechat ``` ## Connect to WeeChat @@ -68,4 +79,6 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with QWeeChat. If not, see . +## Updates + Up-to-date code is on https://git.plastiras.org/emdee/qweechat diff --git a/qweechat/__main__.py b/qweechat/__main__.py new file mode 100644 index 0000000..73872d4 --- /dev/null +++ b/qweechat/__main__.py @@ -0,0 +1,124 @@ +import os +import sys +import logging + +from qtpy import QtWidgets, QtGui, QtCore +from qtpy.QtWidgets import QApplication + +global LOG +LOG = logging.getLogger('qweechat') + +def iMain(lArgs=None): + try: + from qweechat import qweechat + from qweechat.config import write + LOG.info("Loading WeechatConsole") + except ImportError as e: + LOG.error(f"ImportError Loading import qweechat {e} {sys.path}") + LOG.debug(traceback.print_exc()) + text = f"ImportError Loading import qweechat {e} {sys.path}" + title = util_ui.tr('Error importing qweechat') + util_ui.message_box(text, title) + return 1 + + from qtpy.QtWidgets import QApplication + _app = QApplication([]) + + # is this still needed? + if sys.platform == 'Linux' and \ + hasattr(QtCore.Qt, 'AA_X11InitThreads'): + QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_X11InitThreads) + + try: + # WeeChat backported from PySide6 to PyQt5 + LOG.info("Adding WeechatConsole") + class WeechatConsole(qweechat.MainWindow): + def __init__(self, *args): + qweechat.MainWindow.__init__(self, *args) + + def closeEvent(self, event): + """Called when QWeeChat window is closed.""" + self.network.disconnect_weechat() + if self.network.debug_dialog: + self.network.debug_dialog.close() + write(self.config) + except Exception as e: + LOG.exception(f"ERROR WeechatConsole {e}") + MainWindow = None + return 2 + size = 12 + font_name = "Courier New" + font_name = "DejaVu Sans Mono" + + try: + LOG.info("Creating WeechatConsole") + _we = WeechatConsole() + _we.show() + _we.setWindowTitle('File/Connect to 127.0.0.1:9000') + # Fix the pyconsole geometry + try: + font = _we.buffers[0].widget.chat.defaultFont() + font.setFamily(font_name) + font.setBold(True) + if font_width is None: + font_width = QFontMetrics(font).width('M') + _we.setFont(font) + except Exception as e: +# LOG.debug(e) + font_width = size + geometry = _we.geometry() + # make this configable? + geometry.setWidth(int(font_width*70)) + geometry.setHeight(int(font_width*(2+24)*11/8)) + _we.setGeometry(geometry) + #? QtCore.QSize() + _we.resize(int(font_width*80+20), int(font_width*(2+24)*11/8)) + + _we.list_buffers.setSizePolicy(QtWidgets.QSizePolicy.Preferred, + QtWidgets.QSizePolicy.Preferred) + _we.stacked_buffers.setSizePolicy(QtWidgets.QSizePolicy.Expanding, + QtWidgets.QSizePolicy.Expanding) + + LOG.info("Showing WeechatConsole") + _we.show() + # or _we.eval_in_thread() +#? return 0 + except Exception as e: + LOG.exception(f"Error creating WeechatConsole {e}") + return 4 + + LOG.info("_execute_app") + + # self._app.lastWindowClosed.connect(self._app.quit) + while True: + try: + _app.exec_() + except Exception as ex: + LOG.error('Unhandled exception: ' + str(ex)) + return 5 + else: + pass # break + + +if __name__ == '__main__': + iRet = 0 + try: + iRet = iMain(sys.argv[1:]) + except KeyboardInterrupt: + iRet = 0 + except SystemExit as e: + iRet = e + except Exception as e: + import traceback + sys.stderr.write(f"Exception from main {e}" \ + +'\n' + traceback.format_exc() +'\n' ) + iRet = 1 + + # Exception ignored in: + # File "/usr/lib/python3.9/threading.py", line 1428, in _shutdown + # lock.acquire() + # gevent.exceptions.LoopExit as e: + # This operation would block forever +# sys.stderr.write('Calling sys.exit' +'\n') +# sys.exit(iRet) +