utils refactoring

This commit is contained in:
ingvar1995 2018-05-10 20:47:34 +03:00
parent ae903cf405
commit 729bd84d2b
39 changed files with 97 additions and 74 deletions

View file

@ -4,7 +4,7 @@ from db.database import History
from toxygen.smileys import SmileyLoader from toxygen.smileys import SmileyLoader
from messenger.messages import * from messenger.messages import *
import user_data.toxes as encr import user_data.toxes as encr
import toxygen.util as util import toxygen.utils as util
import time import time

View file

@ -2,7 +2,7 @@ from middleware import threads
import middleware.callbacks as callbacks import middleware.callbacks as callbacks
from PyQt5 import QtWidgets, QtGui, QtCore from PyQt5 import QtWidgets, QtGui, QtCore
import ui.password_screen as password_screen import ui.password_screen as password_screen
from util.util import * from utils.util import *
import updater.updater as updater import updater.updater as updater
import os import os
from middleware.tox_factory import tox_factory 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 plugin_support.plugin_support import PluginLoader
from ui.main_screen import MainWindow from ui.main_screen import MainWindow
from ui import tray from ui import tray
import util.ui as util_ui import utils.ui as util_ui
import util.util as util import utils.util as util
from contacts.profile import Profile from contacts.profile import Profile
from file_transfers.file_transfers_handler import FileTransfersHandler from file_transfers.file_transfers_handler import FileTransfersHandler
from contacts.contact_provider import ContactProvider from contacts.contact_provider import ContactProvider

View file

@ -1,6 +1,6 @@
import random import random
import urllib.request import urllib.request
from util.util import * from utils.util import *
from PyQt5 import QtNetwork, QtCore from PyQt5 import QtNetwork, QtCore
import json import json

View file

@ -6,7 +6,7 @@ class Event:
self._callbacks = set() self._callbacks = set()
def __iadd__(self, callback): def __iadd__(self, callback):
self._callbacks.add(callback) self.add_callback(callback)
def __isub__(self, callback): def __isub__(self, callback):
self.remove_callback(callback) self.remove_callback(callback)

View file

@ -0,0 +1,9 @@
class ToxSave:
def __init__(self, tox):
self._tox = tox
def set_tox(self, tox):
self._tox = tox

View file

@ -1,7 +1,7 @@
from user_data.settings import * from user_data.settings import *
from PyQt5 import QtCore, QtGui from PyQt5 import QtCore, QtGui
from wrapper.toxcore_enums_and_consts import TOX_PUBLIC_KEY_SIZE 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 import common.event as event
@ -24,10 +24,11 @@ class BaseContact:
self._name, self._status_message = name, status_message self._name, self._status_message = name, status_message
self._status, self._widget = None, widget self._status, self._widget = None, widget
self._tox_id = tox_id self._tox_id = tox_id
self.init_widget()
self._name_changed_event = event.Event() self._name_changed_event = event.Event()
self._status_message_changed_event = event.Event() self._status_message_changed_event = event.Event()
self._status_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 # 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, self._widget.avatar_label.setPixmap(pixmap.scaled(width, width, QtCore.Qt.KeepAspectRatio,
QtCore.Qt.SmoothTransformation)) QtCore.Qt.SmoothTransformation))
self._widget.avatar_label.repaint() self._widget.avatar_label.repaint()
self._avatar_changed_event(avatar_path)
def reset_avatar(self): def reset_avatar(self):
avatar_path = self.get_avatar_path() avatar_path = self.get_avatar_path()
@ -143,12 +145,17 @@ class BaseContact:
@staticmethod @staticmethod
def get_default_avatar_name(): def get_default_avatar_name():
return 'avatar.png' return 'avatar.png'
def get_avatar_changed_event(self):
return self._avatar_changed_event
avatar_changed_event = property(get_avatar_changed_event)
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
# Widgets # Widgets
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
def init_widget(self): def init_widget(self):
if self._widget is not None:
self._widget.name.setText(self._name) self._widget.name.setText(self._name)
self._widget.status_message.setText(self._status_message) self._widget.status_message.setText(self._status_message)
self._widget.connection_status.update(self._status) self._widget.connection_status.update(self._status)

View file

@ -1,6 +1,6 @@
from history.database import * from history.database import *
from contacts import basecontact, common from contacts import basecontact, common
import util.util as util import utils.util as util
from messenger.messages import * from messenger.messages import *
from file_transfers import file_transfers as ft from file_transfers import file_transfers as ft
import re import re

View file

@ -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): def __init__(self, tox, friend_factory):
super().__init__(tox) super().__init__(tox)
@ -50,7 +51,7 @@ class ContactProvider(util.ToxSave):
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
def get_all(self): def get_all(self):
return self.get_all_friends() + self.get_all_gc() return self.get_all_friends() + self.get_all_groups()
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
# Caching # Caching

View file

@ -1,8 +1,7 @@
import util.util as util import utils.util as util
import util.ui as util_ui import utils.ui as util_ui
from contacts.friend import Friend from contacts.friend import Friend
from PyQt5 import QtCore, QtGui from PyQt5 import QtCore, QtGui
from messenger.messages import *
from wrapper.toxcore_enums_and_consts import * from wrapper.toxcore_enums_and_consts import *
from history.history_loader import HistoryLoader from history.history_loader import HistoryLoader
@ -440,9 +439,27 @@ class ContactsManager:
def _subscribe_to_events(self, contact): def _subscribe_to_events(self, contact):
contact.name_changed_event.add_callback(self._current_contact_name_changed) 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): def _unsubscribe_from_events(self, contact):
contact.name_changed_event.remove_callback(self._current_contact_name_changed) 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): def _current_contact_name_changed(self, name):
self._screen.account_name.setText(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))

View file

@ -1,5 +1,5 @@
from contacts import contact from contacts import contact
import util.util as util import utils.util as util
from PyQt5 import QtGui, QtCore from PyQt5 import QtGui, QtCore
from wrapper import toxcore_enums_and_consts as constants from wrapper import toxcore_enums_and_consts as constants

View file

@ -5,7 +5,7 @@ from file_transfers.file_transfers import *
import time import time
from contacts import basecontact from contacts import basecontact
from contacts.group_chat import * from contacts.group_chat import *
import util.ui as util_ui import utils.ui as util_ui
class Profile(basecontact.BaseContact): class Profile(basecontact.BaseContact):

View file

@ -3,7 +3,7 @@ from messenger.messages import *
from history.database import MESSAGE_AUTHOR from history.database import MESSAGE_AUTHOR
from ui.list_items import * from ui.list_items import *
from PyQt5 import QtWidgets from PyQt5 import QtWidgets
import util.util as util import utils.util as util
class FileTransfersHandler: class FileTransfersHandler:

View file

@ -1,6 +1,6 @@
from sqlite3 import connect from sqlite3 import connect
import os.path import os.path
import util.util as util import utils.util as util
TIMEOUT = 11 TIMEOUT = 11

View file

@ -1,5 +1,5 @@
from messenger.messages import * from messenger.messages import *
import util.util as util import utils.util as util
class HistoryLogsGenerator: class HistoryLogsGenerator:

View file

@ -1,6 +1,6 @@
import app import app
from user_data.settings import * from user_data.settings import *
import util.util as util import utils.util as util
import argparse import argparse

View file

@ -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 wrapper.toxcore_enums_and_consts import *
from messenger.messages import * from messenger.messages import *
# TODO: sub profile name changed event? # 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): def __init__(self, tox, plugin_loader, screen, contacts_manager, contacts_provider, items_factory, profile):
super().__init__(tox) super().__init__(tox)

View file

@ -4,8 +4,8 @@ from contacts.profile import Profile
from wrapper.toxcore_enums_and_consts import * from wrapper.toxcore_enums_and_consts import *
from wrapper.toxav_enums import * from wrapper.toxav_enums import *
from wrapper.tox import bin_to_string from wrapper.tox import bin_to_string
import util.ui as util_ui import utils.ui as util_ui
import util.util as util import utils.util as util
import cv2 import cv2
import numpy as np import numpy as np
from middleware.threads import invoke_in_main_thread, execute from middleware.threads import invoke_in_main_thread, execute

View file

@ -1,7 +1,7 @@
from bootstrap.bootstrap import * from bootstrap.bootstrap import *
import threading import threading
import queue import queue
from util import util from utils import util
import time import time

View file

@ -1,6 +1,6 @@
import json import json
import urllib.request import urllib.request
import util.util as util import utils.util as util
from PyQt5 import QtNetwork, QtCore from PyQt5 import QtNetwork, QtCore

View file

@ -1,4 +1,4 @@
import util.util import utils.util
import wave import wave
import pyaudio import pyaudio
import os.path import os.path
@ -51,4 +51,4 @@ def sound_notification(t):
def get_file_path(file_name): 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)

View file

@ -1,4 +1,4 @@
import util.util as util import utils.util as util
from contacts import profile from contacts import profile
import os import os
import importlib import importlib

View file

@ -1,4 +1,4 @@
from util import util from utils import util
import json import json
import os import os
from collections import OrderedDict from collections import OrderedDict

View file

@ -1,5 +1,5 @@
import os import os
import util.util as util import utils.util as util
def load_stickers(): def load_stickers():

View file

@ -1,11 +1,11 @@
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5 import QtCore, QtGui, QtWidgets
from ui import widgets from ui import widgets
from contacts import profile from contacts import profile
import util import utils
import pyaudio import pyaudio
import wave import wave
from user_data import settings from user_data import settings
from util.util import * from utils.util import *
class IncomingCallWidget(widgets.CenteredWidget): class IncomingCallWidget(widgets.CenteredWidget):
@ -34,13 +34,13 @@ class IncomingCallWidget(widgets.CenteredWidget):
self.accept_video.setGeometry(QtCore.QRect(170, 100, 150, 150)) self.accept_video.setGeometry(QtCore.QRect(170, 100, 150, 150))
self.decline = QtWidgets.QPushButton(self) self.decline = QtWidgets.QPushButton(self)
self.decline.setGeometry(QtCore.QRect(320, 100, 150, 150)) 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) icon = QtGui.QIcon(pixmap)
self.accept_audio.setIcon(icon) 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) icon = QtGui.QIcon(pixmap)
self.accept_video.setIcon(icon) 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) icon = QtGui.QIcon(pixmap)
self.decline.setIcon(icon) self.decline.setIcon(icon)
self.accept_audio.setIconSize(QtCore.QSize(150, 150)) self.accept_audio.setIconSize(QtCore.QSize(150, 150))

View file

@ -1,7 +1,7 @@
from ui.widgets import * from ui.widgets import *
from PyQt5 import uic from PyQt5 import uic
import util.util as util import utils.util as util
import util.ui as util_ui import utils.ui as util_ui
class CreateProfileScreenResult: class CreateProfileScreenResult:

View file

@ -2,7 +2,7 @@ from wrapper.toxcore_enums_and_consts import *
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5 import QtCore, QtGui, QtWidgets
from contacts import profile 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 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 from ui.widgets import DataLabel, create_menu
import html as h import html as h
import smileys import smileys

View file

@ -2,8 +2,8 @@ from contacts.profile import *
from ui.list_items import * from ui.list_items import *
from ui.widgets import MultilineEdit, ComboBox from ui.widgets import MultilineEdit, ComboBox
from ui.main_screen_widgets import * from ui.main_screen_widgets import *
import util.util as util import utils.util as util
import util.ui as util_ui import utils.ui as util_ui
class MainWindow(QtWidgets.QMainWindow): class MainWindow(QtWidgets.QMainWindow):

View file

@ -2,8 +2,8 @@ from PyQt5 import QtCore, QtGui, QtWidgets
from ui.widgets import RubberBandWindow, create_menu, QRightClickButton, CenteredWidget, LineEdit from ui.widgets import RubberBandWindow, create_menu, QRightClickButton, CenteredWidget, LineEdit
from contacts.profile import Profile from contacts.profile import Profile
import urllib import urllib
import util.util as util import utils.util as util
import util.ui as util_ui import utils.ui as util_ui
from stickers.stickers import load_stickers from stickers.stickers import load_stickers

View file

@ -1,12 +1,12 @@
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5 import QtCore, QtGui, QtWidgets
from user_data.settings import * from user_data.settings import *
from contacts.profile import Profile from contacts.profile import Profile
from util.util import * from utils.util import *
from ui.widgets import CenteredWidget, DataLabel, LineEdit, RubberBandWindow from ui.widgets import CenteredWidget, DataLabel, LineEdit, RubberBandWindow
import pyaudio import pyaudio
from user_data import toxes from user_data import toxes
import updater.updater as updater import updater.updater as updater
import util.ui as util_ui import utils.ui as util_ui
class AddContact(CenteredWidget): class AddContact(CenteredWidget):

View file

@ -1,8 +1,8 @@
from PyQt5 import QtWidgets, QtGui, QtCore from PyQt5 import QtWidgets, QtGui, QtCore
from wrapper.toxcore_enums_and_consts import * from wrapper.toxcore_enums_and_consts import *
import ui.widgets as widgets import ui.widgets as widgets
import util.ui as util_ui import utils.ui as util_ui
import util.util as util import utils.util as util
import ui.menu as menu import ui.menu as menu
import html as h import html as h
import re import re

View file

@ -1,6 +1,6 @@
from PyQt5 import QtWidgets, QtGui, QtCore from PyQt5 import QtWidgets, QtGui, QtCore
from util.ui import tr from utils.ui import tr
from util.util import * from utils.util import *
import os.path import os.path

View file

@ -1,5 +1,5 @@
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5 import QtCore, QtGui, QtWidgets
import util.ui as util_ui import utils.ui as util_ui
class DataLabel(QtWidgets.QLabel): class DataLabel(QtWidgets.QLabel):

View file

@ -1,7 +1,6 @@
import util.util as util import utils.util as util
import util.ui as util_ui import utils.ui as util_ui
import os import os
from user_data import settings
import platform import platform
import urllib import urllib
from PyQt5 import QtNetwork, QtCore from PyQt5 import QtNetwork, QtCore

View file

@ -1,4 +1,4 @@
import util.util as util import utils.util as util
import os import os
from user_data.settings import Settings from user_data.settings import Settings

View file

@ -1,6 +1,6 @@
import json import json
import os import os
from util.util import * from utils.util import *
import pyaudio import pyaudio
import smileys.smileys as smileys import smileys.smileys as smileys

View file

@ -1,5 +1,5 @@
from PyQt5 import QtWidgets from PyQt5 import QtWidgets
import util.util as util import utils.util as util
def tr(s): def tr(s):

View file

@ -156,14 +156,3 @@ def is_re_valid(regex):
def get_platform(): def get_platform():
return platform.system() return platform.system()
# TODO: to common
class ToxSave:
def __init__(self, tox):
self._tox = tox
def set_tox(self, tox):
self._tox = tox

View file

@ -1,6 +1,6 @@
from platform import system from platform import system
from ctypes import CDLL from ctypes import CDLL
import util.util as util import utils.util as util
class LibToxCore: class LibToxCore: