/
Picon.py
154 lines (139 loc) · 4.52 KB
/
Picon.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os, re, unicodedata
from Renderer import Renderer
from enigma import ePixmap, ePicLoad
from Tools.Alternatives import GetWithAlternative
from Tools.Directories import pathExists, SCOPE_ACTIVE_SKIN, resolveFilename
from Components.Harddisk import harddiskmanager
from ServiceReference import ServiceReference
searchPaths = []
lastPiconPath = None
def initPiconPaths():
global searchPaths
searchPaths = []
for mp in ('/usr/share/enigma2/', '/'):
onMountpointAdded(mp)
for part in harddiskmanager.getMountedPartitions():
onMountpointAdded(part.mountpoint)
def onMountpointAdded(mountpoint):
global searchPaths
try:
path = os.path.join(mountpoint, 'picon') + '/'
if os.path.isdir(path) and path not in searchPaths:
for fn in os.listdir(path):
if fn.endswith('.png'):
print "[Picon] adding path:", path
searchPaths.append(path)
break
except Exception, ex:
print "[Picon] Failed to investigate %s:" % mountpoint, ex
def onMountpointRemoved(mountpoint):
global searchPaths
path = os.path.join(mountpoint, 'picon') + '/'
try:
searchPaths.remove(path)
print "[Picon] removed path:", path
except:
pass
def onPartitionChange(why, part):
if why == 'add':
onMountpointAdded(part.mountpoint)
elif why == 'remove':
onMountpointRemoved(part.mountpoint)
def findPicon(serviceName):
global lastPiconPath
if lastPiconPath is not None:
pngname = lastPiconPath + serviceName + ".png"
if pathExists(pngname):
return pngname
else:
return ""
else:
global searchPaths
pngname = ""
for path in searchPaths:
if pathExists(path) and not path.startswith('/media/net') and not path.startswith('/media/autofs'):
pngname = path + serviceName + ".png"
if pathExists(pngname):
lastPiconPath = path
break
elif pathExists(path):
pngname = path + serviceName + ".png"
if pathExists(pngname):
lastPiconPath = path
break
if pathExists(pngname):
return pngname
else:
return ""
def getPiconName(serviceName):
#remove the path and name fields, and replace ':' by '_'
sname = '_'.join(GetWithAlternative(serviceName).split(':', 10)[:10])
pngname = findPicon(sname)
if not pngname:
fields = sname.split('_', 3)
if len(fields) > 2 and fields[2] != '2': #fallback to 1 for tv services with nonstandard servicetypes
fields[2] = '1'
if len(fields) > 0 and fields[0] == '4097': #fallback to 1 for IPTV streams
fields[0] = '1'
pngname = findPicon('_'.join(fields))
if not pngname: # picon by channel name
name = ServiceReference(serviceName).getServiceName()
name = unicodedata.normalize('NFKD', unicode(name, 'utf_8', errors='ignore')).encode('ASCII', 'ignore')
name = re.sub('[^a-z0-9]', '', name.replace('&', 'and').replace('+', 'plus').replace('*', 'star').lower())
if len(name) > 0:
pngname = findPicon(name)
if not pngname and len(name) > 2 and name.endswith('hd'):
pngname = findPicon(name[:-2])
return pngname
class Picon(Renderer):
def __init__(self):
Renderer.__init__(self)
self.PicLoad = ePicLoad()
self.PicLoad.PictureData.get().append(self.updatePicon)
self.piconsize = (0,0)
self.pngname = ""
self.lastPath = None
pngname = findPicon("picon_default")
self.defaultpngname = resolveFilename(SCOPE_ACTIVE_SKIN, "picon_default.png")
def addPath(self, value):
if pathExists(value):
global searchPaths
if not value.endswith('/'):
value += '/'
if value not in searchPaths:
searchPaths.append(value)
def applySkin(self, desktop, parent):
attribs = self.skinAttributes[:]
for (attrib, value) in self.skinAttributes:
if attrib == "path":
self.addPath(value)
attribs.remove((attrib,value))
elif attrib == "size":
self.piconsize = value
self.skinAttributes = attribs
return Renderer.applySkin(self, desktop, parent)
GUI_WIDGET = ePixmap
def postWidgetCreate(self, instance):
self.changed((self.CHANGED_DEFAULT,))
def updatePicon(self, picInfo=None):
ptr = self.PicLoad.getData()
if ptr is not None:
self.instance.setPixmap(ptr.__deref__())
self.instance.show()
def changed(self, what):
if self.instance:
pngname = ""
if what[0] == 1 or what[0] == 3:
pngname = getPiconName(self.source.text)
if not pathExists(pngname): # no picon for service found
pngname = self.defaultpngname
if self.pngname != pngname:
if pngname:
self.instance.setScale(1)
self.instance.setPixmapFromFile(pngname)
self.instance.show()
else:
self.instance.hide()
self.pngname = pngname
harddiskmanager.on_partition_list_change.append(onPartitionChange)
initPiconPaths()