speed up satellites.xml-parsing
[enigma2.git] / lib / python / Components / NimManager.py
1 from config import config       #global config instance
2
3 from config import configElement
4 from config import ConfigSubsection
5 from config import ConfigSlider
6 from config import configSelection
7 from config import configSatlist
8
9 import xml.dom.minidom
10 from xml.dom import EMPTY_NAMESPACE
11 from skin import elementsWithTag
12 from Tools import XMLTools
13
14 class boundFunction:
15         def __init__(self, fnc, *args):
16                 self.fnc = fnc
17                 self.args = args
18         def __call__(self, *args):
19                 self.fnc(*self.args + args)
20
21 class nimSlot:
22         def __init__(self, slotid, nimtype, name):
23                 self.slotid = slotid
24                 self.nimType = nimtype
25                 self.name = name
26
27 class NimManager:
28         def readSatsfromFile(self):
29                 self.satellites = { }
30                 #FIXME: path ok???
31                 satdom = xml.dom.minidom.parse('/etc/tuxbox/satellites.xml')
32
33
34                 for entries in elementsWithTag(satdom.childNodes, "satellites"):
35                         for x in elementsWithTag(entries.childNodes, "sat"):
36                                 #print "found sat " + x.getAttribute('name') + " " + str(x.getAttribute('position'))
37                                 tpos = x.getAttribute('position')
38                                 tname = x.getAttribute('name')
39                                 #tname.encode('utf8')
40                                 self.satellites[tpos] = tname
41                                 self.satList.append( (tname, tpos) )
42
43         def getNimType(self, slotID):
44                 #FIXME get it from /proc
45                 if slotID == 0:
46                         return self.nimType["DVB-S"]
47                 else:
48                         return self.nimType["empty/unknown"]
49
50         def getNimName(self, slotID):
51                 #FIXME get it from /proc
52                 return "Alps BSBE1"
53
54         def getNimSocketCount(self):
55                 #FIXME get it from /proc
56                 return 2
57
58         def getConfigPrefix(self, slotid):
59                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
60                         
61         def __init__(self):
62                 #use as enum
63                 self.nimType = {                "empty/unknown": -1,
64                                                                                                 "DVB-S": 0,
65                                                                                                 "DVB-C": 1,
66                                                                                                 "DVB-T": 2}
67                 self.satList = [ ]                                                                              
68                                                                                                 
69                 self.readSatsfromFile()                                                                         
70                                                                                                 
71                 self.nimCount = self.getNimSocketCount()
72
73                 self.nimslots = [ ]
74                 x = 0
75                 while x < self.nimCount:
76                         tType = self.getNimType(x)
77                         tName = self.getNimName(x)
78                         tNim = nimSlot(x, tType, tName)
79                         self.nimslots.append(tNim)
80                         x += 1
81                 
82                 InitNimManager(self)    #init config stuff
83
84         def nimList(self):
85                 list = [ ]
86                 for slot in self.nimslots:
87                         nimText = "Socket " + ("A", "B", "C", "D")[slot.slotid] + ": "
88                         if slot.nimType == -1:
89                                 nimText += "empty/unknown"
90                         else:
91                                 nimText += slot.name + " ("     
92                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
93                         list.append((nimText, slot))
94                 return list
95
96         #callbacks for c++ config
97         def nimConfigModeChanged(self, slotid, mode):
98                 print "nimConfigModeChanged set to " + str(mode)
99         def nimDiseqcModeChanged(self, slotid, mode):
100                 print "nimDiseqcModeChanged set to " + str(mode)
101         def nimPortAChanged(self, slotid, val):
102                 print "nimDiseqcA set to " + str(val)
103         def nimPortBChanged(self, slotid, val):
104                 print "nimDiseqcB set to " + str(val)
105         def nimPortCChanged(self, slotid, val):
106                 print "nimDiseqcC set to " + str(val)
107         def nimPortDChanged(self, slotid, val):
108                 print "nimDiseqcD set to " + str(val)
109
110
111 def InitNimManager(nimmgr):
112         config.Nims = [ConfigSubsection()] * nimmgr.nimCount
113
114         def nimConfigModeChanged(slotid, configElement):
115                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
116         def nimDiseqcModeChanged(slotid, configElement):
117                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
118                 
119         def nimPortAChanged(slotid, configElement):
120                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
121         def nimPortBChanged(slotid, configElement):
122                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
123         def nimPortCChanged(slotid, configElement):
124                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
125         def nimPortDChanged(slotid, configElement):
126                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
127
128         for slot in nimmgr.nimslots:
129                 x = slot.slotid
130                 cname = nimmgr.getConfigPrefix(x)
131                 
132                 if slot.nimType == nimmgr.nimType["DVB-S"]:
133                         config.Nims[x].configMode = configElement(cname + "configMode",configSelection, 0, ("Simple", "Advanced"));
134                         config.Nims[x].diseqcMode = configElement(cname + "diseqcMode",configSelection, 2, ("Single", "Toneburst A/B", "DiSEqC A/B", "DiSEqC A/B/C/D"));
135                         config.Nims[x].diseqcA = configElement(cname + "diseqcA",configSatlist, 192, nimmgr.satList);
136                         config.Nims[x].diseqcB = configElement(cname + "diseqcB",configSatlist, 130, nimmgr.satList);
137                         config.Nims[x].diseqcC = configElement(cname + "diseqcC",configSatlist, 0, nimmgr.satList);
138                         config.Nims[x].diseqcD = configElement(cname + "diseqcD",configSatlist, 0, nimmgr.satList);
139                         
140                         #perhaps the instance of the slot is more useful?
141                         config.Nims[x].configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
142                         config.Nims[x].diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
143                         config.Nims[x].diseqcA.addNotifier(boundFunction(nimPortAChanged,x))
144                         config.Nims[x].diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
145                         config.Nims[x].diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
146                         config.Nims[x].diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
147                 else:
148                         print "pls add support for this frontend type!"         
149
150 nimmanager = NimManager()