toxygen/toxygen/plugins/plugin_super_class.py

237 lines
8.1 KiB
Python
Raw Normal View History

2016-05-28 12:06:13 +02:00
import os
from PyQt5 import QtCore, QtWidgets
2022-09-27 14:38:39 +02:00
import utils.ui as util_ui
import common.tox_save as tox_save
2016-05-28 12:06:13 +02:00
MAX_SHORT_NAME_LENGTH = 5
LOSSY_FIRST_BYTE = 200
LOSSLESS_FIRST_BYTE = 160
def path_to_data(name):
"""
:param name: plugin unique name
:return path do plugin's directory
"""
return os.path.dirname(os.path.realpath(__file__)) + '/' + name + '/'
2016-05-28 21:43:51 +02:00
def log(name, data):
"""
:param name: plugin unique name
:param data: data for saving in log
"""
with open(path_to_data(name) + 'logs.txt', 'a') as fl:
2016-10-29 16:39:48 +02:00
fl.write(str(data) + '\n')
2016-05-28 21:43:51 +02:00
2022-09-27 14:38:39 +02:00
class PluginSuperClass(tox_save.ToxSave):
2016-05-28 12:06:13 +02:00
"""
2017-02-06 22:18:57 +01:00
Superclass for all plugins. Plugin is Python3 module with at least one class derived from PluginSuperClass.
2016-05-28 12:06:13 +02:00
"""
2016-06-22 13:35:22 +02:00
is_plugin = True
2016-05-28 12:06:13 +02:00
2022-09-27 14:38:39 +02:00
def __init__(self, name, short_name, app):
2016-05-28 12:06:13 +02:00
"""
2022-09-27 14:38:39 +02:00
Constructor. In plugin __init__ should take only 1 last argument
2016-05-28 12:06:13 +02:00
:param name: plugin full name
:param short_name: plugin unique short name (length of short name should not exceed MAX_SHORT_NAME_LENGTH)
2022-09-27 14:38:39 +02:00
:param app: App instance
"""
tox = getattr(app, '_tox')
super().__init__(tox)
self._settings = getattr(app, '_settings')
2016-05-28 12:06:13 +02:00
name = name.strip()
short_name = short_name.strip()
if not name or not short_name:
raise NameError('Wrong name')
self._name = name
self._short_name = short_name[:MAX_SHORT_NAME_LENGTH]
self._translator = None # translator for plugin's GUI
# -----------------------------------------------------------------------------------------------------------------
# Get methods
# -----------------------------------------------------------------------------------------------------------------
def get_name(self):
"""
:return plugin full name
"""
return self._name
def get_short_name(self):
"""
:return plugin unique (short) name
"""
return self._short_name
def get_description(self):
"""
Should return plugin description
"""
return self.__doc__
2022-09-27 14:38:39 +02:00
def get_menu(self, row_number):
2016-05-28 12:06:13 +02:00
"""
This method creates items for menu which called on right click in list of friends
:param row_number: number of selected row in list of contacts
2022-09-27 14:38:39 +02:00
:return list of tuples (text, handler)
2016-05-28 12:06:13 +02:00
"""
return []
2016-07-19 22:19:42 +02:00
def get_message_menu(self, menu, text):
"""
This method creates items for menu which called on right click in message
:param menu: menu instance
:param text: selected text
:return list of QAction's
"""
return []
2016-05-28 12:06:13 +02:00
def get_window(self):
"""
This method should return window for plugins with GUI or None
"""
return None
# -----------------------------------------------------------------------------------------------------------------
# Plugin was stopped, started or new command received
# -----------------------------------------------------------------------------------------------------------------
def start(self):
"""
This method called when plugin was started
"""
pass
def stop(self):
"""
This method called when plugin was stopped
"""
pass
def close(self):
"""
App is closing
"""
2016-10-29 16:39:48 +02:00
self.stop()
2016-05-28 12:06:13 +02:00
def command(self, command):
"""
New command. On 'help' this method should provide user list of available commands
:param command: string with command
"""
2016-06-21 13:58:11 +02:00
if command == 'help':
2022-09-27 14:38:39 +02:00
text = util_ui.tr('No commands available')
title = util_ui.tr('List of commands for plugin {}').format(self._name)
util_ui.message_box(text, title)
2016-05-28 12:06:13 +02:00
# -----------------------------------------------------------------------------------------------------------------
# Translations support
# -----------------------------------------------------------------------------------------------------------------
def load_translator(self):
"""
This method loads translations for GUI
"""
app = QtWidgets.QApplication.instance()
2016-05-28 12:06:13 +02:00
langs = self._settings.supported_languages()
curr_lang = self._settings['language']
2016-06-21 13:58:11 +02:00
if curr_lang in langs:
2016-05-28 12:06:13 +02:00
if self._translator is not None:
app.removeTranslator(self._translator)
self._translator = QtCore.QTranslator()
2016-06-21 13:58:11 +02:00
lang_path = langs[curr_lang]
2016-05-28 12:06:13 +02:00
self._translator.load(path_to_data(self._short_name) + lang_path)
app.installTranslator(self._translator)
# -----------------------------------------------------------------------------------------------------------------
# Settings loading and saving
# -----------------------------------------------------------------------------------------------------------------
def load_settings(self):
"""
This method loads settings of plugin and returns raw data
2017-02-06 22:18:57 +01:00
If file doesn't exist this method raises exception
2016-05-28 12:06:13 +02:00
"""
2016-06-22 13:35:22 +02:00
with open(path_to_data(self._short_name) + 'settings.json', 'rb') as fl:
2016-05-28 12:06:13 +02:00
data = fl.read()
2016-06-22 13:35:22 +02:00
return str(data, 'utf-8')
2016-05-28 12:06:13 +02:00
def save_settings(self, data):
"""
This method saves plugin's settings to file
:param data: string with data
"""
with open(path_to_data(self._short_name) + 'settings.json', 'wb') as fl:
2016-06-22 13:35:22 +02:00
fl.write(bytes(data, 'utf-8'))
2016-05-28 12:06:13 +02:00
# -----------------------------------------------------------------------------------------------------------------
# Callbacks
# -----------------------------------------------------------------------------------------------------------------
def lossless_packet(self, data, friend_number):
"""
Incoming lossless packet
:param data: string with data
:param friend_number: number of friend who sent packet
"""
pass
def lossy_packet(self, data, friend_number):
"""
Incoming lossy packet
:param data: string with data
:param friend_number: number of friend who sent packet
"""
pass
def friend_connected(self, friend_number):
"""
Friend with specified number is online now
"""
pass
# -----------------------------------------------------------------------------------------------------------------
# Custom packets sending
# -----------------------------------------------------------------------------------------------------------------
def send_lossless(self, data, friend_number):
"""
This method sends lossless packet to friend
Wrapper for self._tox.friend_send_lossless_packet
Use it instead of direct using self._tox.friend_send_lossless_packet
:return True on success
"""
if data is None:
data = ''
try:
return self._tox.friend_send_lossless_packet(friend_number,
2016-06-22 13:35:22 +02:00
bytes([ord(x) for x in
chr(len(self._short_name) + LOSSLESS_FIRST_BYTE) +
self._short_name + str(data)
]))
2016-05-28 12:06:13 +02:00
except:
return False
def send_lossy(self, data, friend_number):
"""
This method sends lossy packet to friend
Wrapper for self._tox.friend_send_lossy_packet
Use it instead of direct using self._tox.friend_send_lossy_packet
:return True on success
"""
if data is None:
data = ''
try:
return self._tox.friend_send_lossy_packet(friend_number,
2016-06-22 13:35:22 +02:00
bytes([ord(x) for x in
chr(len(self._short_name) + LOSSY_FIRST_BYTE) +
self._short_name + str(data)
]))
2016-05-28 12:06:13 +02:00
except:
return False