-
Notifications
You must be signed in to change notification settings - Fork 7
/
SmileyManager.py
96 lines (73 loc) · 2.66 KB
/
SmileyManager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright (C) 2009-2011 AG Projects. See LICENSE for details.
#
from Foundation import NSBundle
import os
from application.python.types import Singleton
from util import escape_html
SMILEY_STYLE="MSN"
class SmileyManager(object, metaclass=Singleton):
def __init__(self):
self.smiley_directory = None
self.icon = None
self.smileys = {}
self.smileys_html = {}
self.smiley_keys = []
self.load_theme(str(NSBundle.mainBundle().resourcePath())+"/smileys" , "default")
def load_theme(self, smiley_theme_directory, name="default"):
self.smiley_directory = smiley_theme_directory
self.theme = name
self.icon = None
in_header = True
found = False
f = open(os.path.join(self.smiley_directory, self.theme, "theme"), "r")
for line in f:
line = line.strip()
if not line or line[0] == "#":
continue
if in_header:
if "=" in line:
k, v = line.split("=", 1)
if k == "Icon":
self.icon = v
continue
if line[0] == "[":
in_header = False
else:
continue
if line == "[%s]"%SMILEY_STYLE:
if found:
break
found = True
continue
elif line.startswith("["):
if found:
break
if found:
line = line.replace("\\\\", "\\")
toks = [s.strip() for s in line.split()]
if len(toks) >= 2:
file = toks[0]
for text in toks[1:]:
self.smileys[text] = file
self.smiley_keys.append(toks[1])
f.close()
self.smileys_html = {}
for k, v in self.smileys.items():
# pre-escape the smiley so that it matches escaped text later
ek = escape_html(k)
self.smileys_html[ek] = "<img src='file:%s' class='smiley' />"%(self.get_smiley(k))
def get_smiley(self, text):
if text in self.smileys:
return os.path.join(self.smiley_directory, self.theme, self.smileys[text])
return None
def subst_smileys_html(self, text):
items = list(self.smileys_html.items())
sorted(items, key=lambda x: x[0], reverse=True)
for k, v in items:
text = text.replace(k, v)
return text
def get_smiley_list(self):
l = []
for text in self.smiley_keys:
l.append((text, self.get_smiley(text)))
return l