From 729bd84d2baaaee371e948bd0a9b12148db5564d Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Thu, 10 May 2018 20:47:34 +0300 Subject: [PATCH] utils refactoring --- tests/tests.py | 2 +- toxygen/app.py | 6 ++--- toxygen/bootstrap/bootstrap.py | 2 +- toxygen/common/event.py | 2 +- toxygen/common/tox_save.py | 9 ++++++++ toxygen/contacts/basecontact.py | 21 +++++++++++------ toxygen/contacts/contact.py | 2 +- toxygen/contacts/contact_provider.py | 7 +++--- toxygen/contacts/contacts_manager.py | 23 ++++++++++++++++--- toxygen/contacts/group_chat.py | 2 +- toxygen/contacts/profile.py | 2 +- .../file_transfers/file_transfers_handler.py | 2 +- toxygen/history/database.py | 2 +- toxygen/history/history_logs_generators.py | 2 +- toxygen/main.py | 2 +- toxygen/messenger/messenger.py | 5 ++-- toxygen/middleware/callbacks.py | 4 ++-- toxygen/middleware/threads.py | 2 +- toxygen/network/tox_dns.py | 2 +- toxygen/notifications/sound.py | 4 ++-- toxygen/plugin_support/plugin_support.py | 2 +- toxygen/smileys/smileys.py | 2 +- toxygen/stickers/stickers.py | 2 +- toxygen/ui/av_widgets.py | 10 ++++---- toxygen/ui/create_profile_screen.py | 4 ++-- toxygen/ui/list_items.py | 2 +- toxygen/ui/main_screen.py | 4 ++-- toxygen/ui/main_screen_widgets.py | 4 ++-- toxygen/ui/menu.py | 4 ++-- toxygen/ui/messages_widgets.py | 4 ++-- toxygen/ui/tray.py | 4 ++-- toxygen/ui/widgets.py | 2 +- toxygen/updater/updater.py | 5 ++-- toxygen/user_data/profile_manager.py | 2 +- toxygen/user_data/settings.py | 2 +- toxygen/{util => utils}/__init__.py | 0 toxygen/{util => utils}/ui.py | 2 +- toxygen/{util => utils}/util.py | 11 --------- toxygen/wrapper/libtox.py | 2 +- 39 files changed, 97 insertions(+), 74 deletions(-) create mode 100644 toxygen/common/tox_save.py rename toxygen/{util => utils}/__init__.py (100%) rename toxygen/{util => utils}/ui.py (98%) rename toxygen/{util => utils}/util.py (95%) diff --git a/tests/tests.py b/tests/tests.py index b61553e..1047de4 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -4,7 +4,7 @@ from db.database import History from toxygen.smileys import SmileyLoader from messenger.messages import * import user_data.toxes as encr -import toxygen.util as util +import toxygen.utils as util import time diff --git a/toxygen/app.py b/toxygen/app.py index 7215a7a..d5fd513 100644 --- a/toxygen/app.py +++ b/toxygen/app.py @@ -2,7 +2,7 @@ from middleware import threads import middleware.callbacks as callbacks from PyQt5 import QtWidgets, QtGui, QtCore import ui.password_screen as password_screen -from util.util import * +from utils.util import * import updater.updater as updater import os from middleware.tox_factory import tox_factory @@ -14,8 +14,8 @@ from user_data.profile_manager import ProfileManager from plugin_support.plugin_support import PluginLoader from ui.main_screen import MainWindow from ui import tray -import util.ui as util_ui -import util.util as util +import utils.ui as util_ui +import utils.util as util from contacts.profile import Profile from file_transfers.file_transfers_handler import FileTransfersHandler from contacts.contact_provider import ContactProvider diff --git a/toxygen/bootstrap/bootstrap.py b/toxygen/bootstrap/bootstrap.py index 2e5d3dc..6c34e0e 100644 --- a/toxygen/bootstrap/bootstrap.py +++ b/toxygen/bootstrap/bootstrap.py @@ -1,6 +1,6 @@ import random import urllib.request -from util.util import * +from utils.util import * from PyQt5 import QtNetwork, QtCore import json diff --git a/toxygen/common/event.py b/toxygen/common/event.py index 75c29a5..e3ecbf9 100644 --- a/toxygen/common/event.py +++ b/toxygen/common/event.py @@ -6,7 +6,7 @@ class Event: self._callbacks = set() def __iadd__(self, callback): - self._callbacks.add(callback) + self.add_callback(callback) def __isub__(self, callback): self.remove_callback(callback) diff --git a/toxygen/common/tox_save.py b/toxygen/common/tox_save.py new file mode 100644 index 0000000..5d4cee0 --- /dev/null +++ b/toxygen/common/tox_save.py @@ -0,0 +1,9 @@ + + +class ToxSave: + + def __init__(self, tox): + self._tox = tox + + def set_tox(self, tox): + self._tox = tox diff --git a/toxygen/contacts/basecontact.py b/toxygen/contacts/basecontact.py index fd084d7..44a18b3 100644 --- a/toxygen/contacts/basecontact.py +++ b/toxygen/contacts/basecontact.py @@ -1,7 +1,7 @@ from user_data.settings import * from PyQt5 import QtCore, QtGui from wrapper.toxcore_enums_and_consts import TOX_PUBLIC_KEY_SIZE -import util.util as util +import utils.util as util import common.event as event @@ -24,10 +24,11 @@ class BaseContact: self._name, self._status_message = name, status_message self._status, self._widget = None, widget self._tox_id = tox_id - self.init_widget() self._name_changed_event = event.Event() self._status_message_changed_event = event.Event() self._status_changed_event = event.Event() + self._avatar_changed_event = event.Event() + self.init_widget() # ----------------------------------------------------------------------------------------------------------------- # Name - current name or alias of user @@ -116,6 +117,7 @@ class BaseContact: self._widget.avatar_label.setPixmap(pixmap.scaled(width, width, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation)) self._widget.avatar_label.repaint() + self._avatar_changed_event(avatar_path) def reset_avatar(self): avatar_path = self.get_avatar_path() @@ -143,13 +145,18 @@ class BaseContact: @staticmethod def get_default_avatar_name(): return 'avatar.png' + + def get_avatar_changed_event(self): + return self._avatar_changed_event + + avatar_changed_event = property(get_avatar_changed_event) + # ----------------------------------------------------------------------------------------------------------------- # Widgets # ----------------------------------------------------------------------------------------------------------------- def init_widget(self): - if self._widget is not None: - self._widget.name.setText(self._name) - self._widget.status_message.setText(self._status_message) - self._widget.connection_status.update(self._status) - self.load_avatar() + self._widget.name.setText(self._name) + self._widget.status_message.setText(self._status_message) + self._widget.connection_status.update(self._status) + self.load_avatar() diff --git a/toxygen/contacts/contact.py b/toxygen/contacts/contact.py index c9274ac..43f7728 100644 --- a/toxygen/contacts/contact.py +++ b/toxygen/contacts/contact.py @@ -1,6 +1,6 @@ from history.database import * from contacts import basecontact, common -import util.util as util +import utils.util as util from messenger.messages import * from file_transfers import file_transfers as ft import re diff --git a/toxygen/contacts/contact_provider.py b/toxygen/contacts/contact_provider.py index 82cf6e7..5042f97 100644 --- a/toxygen/contacts/contact_provider.py +++ b/toxygen/contacts/contact_provider.py @@ -1,7 +1,8 @@ -import util.util as util +import utils.util as util +import common.tox_save as tox_save -class ContactProvider(util.ToxSave): +class ContactProvider(tox_save.ToxSave): def __init__(self, tox, friend_factory): super().__init__(tox) @@ -50,7 +51,7 @@ class ContactProvider(util.ToxSave): # ----------------------------------------------------------------------------------------------------------------- def get_all(self): - return self.get_all_friends() + self.get_all_gc() + return self.get_all_friends() + self.get_all_groups() # ----------------------------------------------------------------------------------------------------------------- # Caching diff --git a/toxygen/contacts/contacts_manager.py b/toxygen/contacts/contacts_manager.py index 818ae88..464d448 100644 --- a/toxygen/contacts/contacts_manager.py +++ b/toxygen/contacts/contacts_manager.py @@ -1,8 +1,7 @@ -import util.util as util -import util.ui as util_ui +import utils.util as util +import utils.ui as util_ui from contacts.friend import Friend from PyQt5 import QtCore, QtGui -from messenger.messages import * from wrapper.toxcore_enums_and_consts import * from history.history_loader import HistoryLoader @@ -440,9 +439,27 @@ class ContactsManager: def _subscribe_to_events(self, contact): contact.name_changed_event.add_callback(self._current_contact_name_changed) + contact.status_changed_event.add_callback(self._current_contact_status_changed) + contact.status_message_changed_event.add_callback(self._current_contact_status_message_changed) + contact.avatar_changed_event.add_callback(self._current_contact_avatar_changed) def _unsubscribe_from_events(self, contact): contact.name_changed_event.remove_callback(self._current_contact_name_changed) + contact.status_changed_event.remove_callback(self._current_contact_status_changed) + contact.status_message_changed_event.remove_callback(self._current_contact_status_message_changed) + contact.avatar_changed_event.remove_callback(self._current_contact_avatar_changed) def _current_contact_name_changed(self, name): self._screen.account_name.setText(name) + + def _current_contact_status_changed(self, status): + pass + + def _current_contact_status_message_changed(self, status_message): + self._screen.account_status.setText(status_message) + + def _current_contact_avatar_changed(self, avatar_path): + width = self._screen.account_avatar.width() + pixmap = QtGui.QPixmap(avatar_path) + self._screen.account_avatar.avatar_label.setPixmap(pixmap.scaled(width, width, QtCore.Qt.KeepAspectRatio, + QtCore.Qt.SmoothTransformation)) diff --git a/toxygen/contacts/group_chat.py b/toxygen/contacts/group_chat.py index b247281..5121be7 100644 --- a/toxygen/contacts/group_chat.py +++ b/toxygen/contacts/group_chat.py @@ -1,5 +1,5 @@ from contacts import contact -import util.util as util +import utils.util as util from PyQt5 import QtGui, QtCore from wrapper import toxcore_enums_and_consts as constants diff --git a/toxygen/contacts/profile.py b/toxygen/contacts/profile.py index cfeae94..8102f56 100644 --- a/toxygen/contacts/profile.py +++ b/toxygen/contacts/profile.py @@ -5,7 +5,7 @@ from file_transfers.file_transfers import * import time from contacts import basecontact from contacts.group_chat import * -import util.ui as util_ui +import utils.ui as util_ui class Profile(basecontact.BaseContact): diff --git a/toxygen/file_transfers/file_transfers_handler.py b/toxygen/file_transfers/file_transfers_handler.py index 5d9b052..c4ab8b0 100644 --- a/toxygen/file_transfers/file_transfers_handler.py +++ b/toxygen/file_transfers/file_transfers_handler.py @@ -3,7 +3,7 @@ from messenger.messages import * from history.database import MESSAGE_AUTHOR from ui.list_items import * from PyQt5 import QtWidgets -import util.util as util +import utils.util as util class FileTransfersHandler: diff --git a/toxygen/history/database.py b/toxygen/history/database.py index 188e85b..bf593d8 100644 --- a/toxygen/history/database.py +++ b/toxygen/history/database.py @@ -1,6 +1,6 @@ from sqlite3 import connect import os.path -import util.util as util +import utils.util as util TIMEOUT = 11 diff --git a/toxygen/history/history_logs_generators.py b/toxygen/history/history_logs_generators.py index 3e46562..b8d0a56 100644 --- a/toxygen/history/history_logs_generators.py +++ b/toxygen/history/history_logs_generators.py @@ -1,5 +1,5 @@ from messenger.messages import * -import util.util as util +import utils.util as util class HistoryLogsGenerator: diff --git a/toxygen/main.py b/toxygen/main.py index 5ecbd77..232ad93 100644 --- a/toxygen/main.py +++ b/toxygen/main.py @@ -1,6 +1,6 @@ import app from user_data.settings import * -import util.util as util +import utils.util as util import argparse diff --git a/toxygen/messenger/messenger.py b/toxygen/messenger/messenger.py index 1021629..5a94f7f 100644 --- a/toxygen/messenger/messenger.py +++ b/toxygen/messenger/messenger.py @@ -1,11 +1,12 @@ -import util.util as util +import utils.util as util +import common.tox_save as tox_save from wrapper.toxcore_enums_and_consts import * from messenger.messages import * # TODO: sub profile name changed event? -class Messenger(util.ToxSave): +class Messenger(tox_save.ToxSave): def __init__(self, tox, plugin_loader, screen, contacts_manager, contacts_provider, items_factory, profile): super().__init__(tox) diff --git a/toxygen/middleware/callbacks.py b/toxygen/middleware/callbacks.py index 912696b..7b7b759 100644 --- a/toxygen/middleware/callbacks.py +++ b/toxygen/middleware/callbacks.py @@ -4,8 +4,8 @@ from contacts.profile import Profile from wrapper.toxcore_enums_and_consts import * from wrapper.toxav_enums import * from wrapper.tox import bin_to_string -import util.ui as util_ui -import util.util as util +import utils.ui as util_ui +import utils.util as util import cv2 import numpy as np from middleware.threads import invoke_in_main_thread, execute diff --git a/toxygen/middleware/threads.py b/toxygen/middleware/threads.py index e654a66..2e432b6 100644 --- a/toxygen/middleware/threads.py +++ b/toxygen/middleware/threads.py @@ -1,7 +1,7 @@ from bootstrap.bootstrap import * import threading import queue -from util import util +from utils import util import time diff --git a/toxygen/network/tox_dns.py b/toxygen/network/tox_dns.py index 1a525bc..02e97f5 100644 --- a/toxygen/network/tox_dns.py +++ b/toxygen/network/tox_dns.py @@ -1,6 +1,6 @@ import json import urllib.request -import util.util as util +import utils.util as util from PyQt5 import QtNetwork, QtCore diff --git a/toxygen/notifications/sound.py b/toxygen/notifications/sound.py index a0b93f0..361cd05 100644 --- a/toxygen/notifications/sound.py +++ b/toxygen/notifications/sound.py @@ -1,4 +1,4 @@ -import util.util +import utils.util import wave import pyaudio import os.path @@ -51,4 +51,4 @@ def sound_notification(t): def get_file_path(file_name): - return os.path.join(util.util.get_sounds_directory(), file_name) + return os.path.join(utils.util.get_sounds_directory(), file_name) diff --git a/toxygen/plugin_support/plugin_support.py b/toxygen/plugin_support/plugin_support.py index 6f2af9c..bd50cb7 100644 --- a/toxygen/plugin_support/plugin_support.py +++ b/toxygen/plugin_support/plugin_support.py @@ -1,4 +1,4 @@ -import util.util as util +import utils.util as util from contacts import profile import os import importlib diff --git a/toxygen/smileys/smileys.py b/toxygen/smileys/smileys.py index fc40a69..9027bcb 100644 --- a/toxygen/smileys/smileys.py +++ b/toxygen/smileys/smileys.py @@ -1,4 +1,4 @@ -from util import util +from utils import util import json import os from collections import OrderedDict diff --git a/toxygen/stickers/stickers.py b/toxygen/stickers/stickers.py index 5ad6aa1..a406b6c 100644 --- a/toxygen/stickers/stickers.py +++ b/toxygen/stickers/stickers.py @@ -1,5 +1,5 @@ import os -import util.util as util +import utils.util as util def load_stickers(): diff --git a/toxygen/ui/av_widgets.py b/toxygen/ui/av_widgets.py index 6fe3f13..f9eb6ed 100644 --- a/toxygen/ui/av_widgets.py +++ b/toxygen/ui/av_widgets.py @@ -1,11 +1,11 @@ from PyQt5 import QtCore, QtGui, QtWidgets from ui import widgets from contacts import profile -import util +import utils import pyaudio import wave from user_data import settings -from util.util import * +from utils.util import * class IncomingCallWidget(widgets.CenteredWidget): @@ -34,13 +34,13 @@ class IncomingCallWidget(widgets.CenteredWidget): self.accept_video.setGeometry(QtCore.QRect(170, 100, 150, 150)) self.decline = QtWidgets.QPushButton(self) self.decline.setGeometry(QtCore.QRect(320, 100, 150, 150)) - pixmap = QtGui.QPixmap(util.curr_directory() + '/images/accept_audio.png') + pixmap = QtGui.QPixmap(utils.curr_directory() + '/images/accept_audio.png') icon = QtGui.QIcon(pixmap) self.accept_audio.setIcon(icon) - pixmap = QtGui.QPixmap(util.curr_directory() + '/images/accept_video.png') + pixmap = QtGui.QPixmap(utils.curr_directory() + '/images/accept_video.png') icon = QtGui.QIcon(pixmap) self.accept_video.setIcon(icon) - pixmap = QtGui.QPixmap(util.curr_directory() + '/images/decline_call.png') + pixmap = QtGui.QPixmap(utils.curr_directory() + '/images/decline_call.png') icon = QtGui.QIcon(pixmap) self.decline.setIcon(icon) self.accept_audio.setIconSize(QtCore.QSize(150, 150)) diff --git a/toxygen/ui/create_profile_screen.py b/toxygen/ui/create_profile_screen.py index f461719..40c9091 100644 --- a/toxygen/ui/create_profile_screen.py +++ b/toxygen/ui/create_profile_screen.py @@ -1,7 +1,7 @@ from ui.widgets import * from PyQt5 import uic -import util.util as util -import util.ui as util_ui +import utils.util as util +import utils.ui as util_ui class CreateProfileScreenResult: diff --git a/toxygen/ui/list_items.py b/toxygen/ui/list_items.py index d075391..a305ffc 100644 --- a/toxygen/ui/list_items.py +++ b/toxygen/ui/list_items.py @@ -2,7 +2,7 @@ from wrapper.toxcore_enums_and_consts import * from PyQt5 import QtCore, QtGui, QtWidgets from contacts import profile from file_transfers.file_transfers import TOX_FILE_TRANSFER_STATE, PAUSED_FILE_TRANSFERS, DO_NOT_SHOW_ACCEPT_BUTTON, ACTIVE_FILE_TRANSFERS, SHOW_PROGRESS_BAR -from util.util import * +from utils.util import * from ui.widgets import DataLabel, create_menu import html as h import smileys diff --git a/toxygen/ui/main_screen.py b/toxygen/ui/main_screen.py index 412f487..54ca652 100644 --- a/toxygen/ui/main_screen.py +++ b/toxygen/ui/main_screen.py @@ -2,8 +2,8 @@ from contacts.profile import * from ui.list_items import * from ui.widgets import MultilineEdit, ComboBox from ui.main_screen_widgets import * -import util.util as util -import util.ui as util_ui +import utils.util as util +import utils.ui as util_ui class MainWindow(QtWidgets.QMainWindow): diff --git a/toxygen/ui/main_screen_widgets.py b/toxygen/ui/main_screen_widgets.py index 665e5dc..e739167 100644 --- a/toxygen/ui/main_screen_widgets.py +++ b/toxygen/ui/main_screen_widgets.py @@ -2,8 +2,8 @@ from PyQt5 import QtCore, QtGui, QtWidgets from ui.widgets import RubberBandWindow, create_menu, QRightClickButton, CenteredWidget, LineEdit from contacts.profile import Profile import urllib -import util.util as util -import util.ui as util_ui +import utils.util as util +import utils.ui as util_ui from stickers.stickers import load_stickers diff --git a/toxygen/ui/menu.py b/toxygen/ui/menu.py index 33f0690..b28ab3b 100644 --- a/toxygen/ui/menu.py +++ b/toxygen/ui/menu.py @@ -1,12 +1,12 @@ from PyQt5 import QtCore, QtGui, QtWidgets from user_data.settings import * from contacts.profile import Profile -from util.util import * +from utils.util import * from ui.widgets import CenteredWidget, DataLabel, LineEdit, RubberBandWindow import pyaudio from user_data import toxes import updater.updater as updater -import util.ui as util_ui +import utils.ui as util_ui class AddContact(CenteredWidget): diff --git a/toxygen/ui/messages_widgets.py b/toxygen/ui/messages_widgets.py index fbd3154..0e31891 100644 --- a/toxygen/ui/messages_widgets.py +++ b/toxygen/ui/messages_widgets.py @@ -1,8 +1,8 @@ from PyQt5 import QtWidgets, QtGui, QtCore from wrapper.toxcore_enums_and_consts import * import ui.widgets as widgets -import util.ui as util_ui -import util.util as util +import utils.ui as util_ui +import utils.util as util import ui.menu as menu import html as h import re diff --git a/toxygen/ui/tray.py b/toxygen/ui/tray.py index 859eade..2003053 100644 --- a/toxygen/ui/tray.py +++ b/toxygen/ui/tray.py @@ -1,6 +1,6 @@ from PyQt5 import QtWidgets, QtGui, QtCore -from util.ui import tr -from util.util import * +from utils.ui import tr +from utils.util import * import os.path diff --git a/toxygen/ui/widgets.py b/toxygen/ui/widgets.py index 7585f06..ec6c3e4 100644 --- a/toxygen/ui/widgets.py +++ b/toxygen/ui/widgets.py @@ -1,5 +1,5 @@ from PyQt5 import QtCore, QtGui, QtWidgets -import util.ui as util_ui +import utils.ui as util_ui class DataLabel(QtWidgets.QLabel): diff --git a/toxygen/updater/updater.py b/toxygen/updater/updater.py index f274161..329353c 100644 --- a/toxygen/updater/updater.py +++ b/toxygen/updater/updater.py @@ -1,7 +1,6 @@ -import util.util as util -import util.ui as util_ui +import utils.util as util +import utils.ui as util_ui import os -from user_data import settings import platform import urllib from PyQt5 import QtNetwork, QtCore diff --git a/toxygen/user_data/profile_manager.py b/toxygen/user_data/profile_manager.py index 6107d43..a95253f 100644 --- a/toxygen/user_data/profile_manager.py +++ b/toxygen/user_data/profile_manager.py @@ -1,4 +1,4 @@ -import util.util as util +import utils.util as util import os from user_data.settings import Settings diff --git a/toxygen/user_data/settings.py b/toxygen/user_data/settings.py index 8952956..f9390f3 100644 --- a/toxygen/user_data/settings.py +++ b/toxygen/user_data/settings.py @@ -1,6 +1,6 @@ import json import os -from util.util import * +from utils.util import * import pyaudio import smileys.smileys as smileys diff --git a/toxygen/util/__init__.py b/toxygen/utils/__init__.py similarity index 100% rename from toxygen/util/__init__.py rename to toxygen/utils/__init__.py diff --git a/toxygen/util/ui.py b/toxygen/utils/ui.py similarity index 98% rename from toxygen/util/ui.py rename to toxygen/utils/ui.py index b2b5712..cdb5f9a 100644 --- a/toxygen/util/ui.py +++ b/toxygen/utils/ui.py @@ -1,5 +1,5 @@ from PyQt5 import QtWidgets -import util.util as util +import utils.util as util def tr(s): diff --git a/toxygen/util/util.py b/toxygen/utils/util.py similarity index 95% rename from toxygen/util/util.py rename to toxygen/utils/util.py index 329285c..5790a5b 100644 --- a/toxygen/util/util.py +++ b/toxygen/utils/util.py @@ -156,14 +156,3 @@ def is_re_valid(regex): def get_platform(): return platform.system() - - -# TODO: to common - -class ToxSave: - - def __init__(self, tox): - self._tox = tox - - def set_tox(self, tox): - self._tox = tox diff --git a/toxygen/wrapper/libtox.py b/toxygen/wrapper/libtox.py index a04746c..402aa5f 100644 --- a/toxygen/wrapper/libtox.py +++ b/toxygen/wrapper/libtox.py @@ -1,6 +1,6 @@ from platform import system from ctypes import CDLL -import util.util as util +import utils.util as util class LibToxCore: