toxygen/toxygen/smileys/smileys.py

75 lines
2.7 KiB
Python
Raw Normal View History

2018-05-10 17:47:34 +00:00
from utils import util
2016-06-11 10:36:52 +00:00
import json
2016-06-11 12:37:52 +00:00
import os
from collections import OrderedDict
from PyQt5 import QtCore
2016-06-11 10:36:52 +00:00
class SmileyLoader:
2016-06-11 12:37:52 +00:00
"""
Class which loads smileys packs and insert smileys into messages
"""
2016-06-11 10:36:52 +00:00
def __init__(self, settings):
2016-06-22 11:35:22 +00:00
super().__init__()
2016-06-13 21:32:45 +00:00
self._settings = settings
self._curr_pack = None # current pack name
self._smileys = {} # smileys dict. key - smiley (str), value - path to image (str)
self._list = [] # smileys list without duplicates
2016-06-11 10:36:52 +00:00
self.load_pack()
def load_pack(self):
2016-06-11 12:37:52 +00:00
"""
Loads smiley pack
"""
2016-06-13 21:32:45 +00:00
pack_name = self._settings['smiley_pack']
if self._settings['smileys'] and self._curr_pack != pack_name:
self._curr_pack = pack_name
path = util.join_path(self.get_smileys_path(), 'config.json')
2016-06-11 10:36:52 +00:00
try:
2016-06-28 18:47:49 +00:00
with open(path, encoding='utf8') as fl:
2016-06-13 21:32:45 +00:00
self._smileys = json.loads(fl.read())
fl.seek(0)
tmp = json.loads(fl.read(), object_pairs_hook=OrderedDict)
2016-06-21 11:58:11 +00:00
print('Smiley pack {} loaded'.format(pack_name))
keys, values, self._list = [], [], []
for key, value in tmp.items():
value = util.join_path(self.get_smileys_path(), value)
if value not in values:
keys.append(key)
values.append(value)
2016-06-23 08:18:18 +00:00
self._list = list(zip(keys, values))
except Exception as ex:
2016-06-13 21:32:45 +00:00
self._smileys = {}
self._list = []
2016-06-21 11:58:11 +00:00
print('Smiley pack {} was not loaded. Error: {}'.format(pack_name, ex))
2016-06-11 10:36:52 +00:00
def get_smileys_path(self):
return util.join_path(util.get_smileys_directory(), self._curr_pack) if self._curr_pack is not None else None
2016-06-11 10:36:52 +00:00
@staticmethod
def get_packs_list():
d = util.get_smileys_directory()
2016-06-11 12:37:52 +00:00
return [x[1] for x in os.walk(d)][0]
2016-06-14 18:47:03 +00:00
def get_smileys(self):
2016-06-23 08:18:18 +00:00
return list(self._list)
2016-06-14 18:47:03 +00:00
2016-06-11 10:36:52 +00:00
def add_smileys_to_text(self, text, edit):
2016-06-11 12:37:52 +00:00
"""
Adds smileys to text
:param text: message
:param edit: MessageEdit instance
:return text with smileys
"""
2016-06-14 18:47:03 +00:00
if not self._settings['smileys'] or not len(self._smileys):
2016-06-11 10:36:52 +00:00
return text
arr = text.split(' ')
for i in range(len(arr)):
2016-06-13 21:32:45 +00:00
if arr[i] in self._smileys:
file_name = self._smileys[arr[i]] # image name
2016-06-21 11:58:11 +00:00
arr[i] = '<img title=\"{}\" src=\"{}\" />'.format(arr[i], file_name)
2016-06-11 12:37:52 +00:00
if file_name.endswith('.gif'): # animated smiley
2016-06-11 10:36:52 +00:00
edit.addAnimation(QtCore.QUrl(file_name), self.get_smileys_path() + file_name)
return ' '.join(arr)