satipclient: always set force_plts:1
[enigma2-plugins.git] / satipclient / src / ClientConfig.py
1 from enigma import eEnv
2 from Tools.Log import Log
3 from Tools.Directories import fileExists
4 from SatIPTuner import SatIPTuner
5
6 class TunerEntry():
7         VTUNER_TYPE_SATIP_CLIENT = "satip_client"
8
9         TUNER_TYPE_C = "DVB-C"
10         TUNER_TYPE_S = "DVB-S"
11         TUNER_TYPE_S = "DVB-S"
12         TUNER_TYPE_T = "DVB-T"
13
14         KEY_IPADDR = "ipaddr"
15         KEY_TUNER_TYPE = "tuner_type"
16         KEY_VTUNER_TYPE = "vtuner_type"
17         KEY_FORCE_PLTS = "force_plts"
18
19         MAP_FROM_DLNA = {
20                 SatIPTuner.TUNER_TYPE_S : TUNER_TYPE_S,
21                 SatIPTuner.TUNER_TYPE_S2 : TUNER_TYPE_S,
22                 SatIPTuner.TUNER_TYPE_T : TUNER_TYPE_T,
23                 SatIPTuner.TUNER_TYPE_T2 : TUNER_TYPE_T,
24                 SatIPTuner.TUNER_TYPE_C : TUNER_TYPE_C,
25                 SatIPTuner.TUNER_TYPE_C2 : TUNER_TYPE_C
26         }
27
28         @staticmethod
29         def create(ip, tunerType, entryType=VTUNER_TYPE_SATIP_CLIENT):
30                 data = {
31                         TunerEntry.KEY_VTUNER_TYPE : entryType,
32                         TunerEntry.KEY_TUNER_TYPE : tunerType,
33                         TunerEntry.KEY_IPADDR : ip,
34                         TunerEntry.KEY_FORCE_PLTS: "1",
35                 }
36                 return TunerEntry(data)
37
38         def __init__(self, data):
39                 self._data = data
40
41         def getEntryType(self):
42                 return self._data.get(self.KEY_VTUNER_TYPE, None)
43         def setEntryType(self, type):
44                 self._data[self.KEY_VTUNER_TYPE] = type
45         entryType = property(getEntryType, setEntryType)
46
47         def getIpAddress(self):
48                 return self._data.get(self.KEY_IPADDR, None)
49         def setIpAddress(self, ip):
50                 self._data[self.KEY_IPADDR] = ip
51         ipAddress = property(getIpAddress, setIpAddress)
52
53         def getTunerType(self):
54                 return self._data.get(self.KEY_TUNER_TYPE, None)
55         def setTunerType(self, type):
56                 self._data[self.KEY_TUNER_TYPE] = type
57         tunerType = property(getTunerType, setTunerType)
58
59         def setProperty(self, key, value):
60                 self._data[key] = value
61
62         def getProperty(self, key, value, default=None):
63                 return self._data.get(key, default)
64
65         def isValidSatIPEntry(self):
66                 return  self.entryType == self.VTUNER_TYPE_SATIP_CLIENT \
67                         and self.ipAddress \
68                         and self.tunerType
69
70         def data(self):
71                 return self._data
72
73         def __str__(self):
74                 return "TunerEntry~%s" % (self._data)
75
76         def __repr__(self):
77                 return self.__str__()
78
79 class ClientConfig(object):
80         CONFIG_FILE = eEnv.resolve("${sysconfdir}/vtuner.conf")
81         def __init__(self):
82                 self._config = {}
83                 self.reload()
84
85         def getConfig(self):
86                 return self._config
87
88         def addTuner(self, ip, tunerType, entryType=TunerEntry.VTUNER_TYPE_SATIP_CLIENT):
89                 entry = TunerEntry.create(ip, tunerType, entryType=entryType)
90                 self._config[len(self._config.keys())] = entry
91
92         def removeTuner(self, index, tunerEntry):
93                 if index in self._config:
94                         tE = self._config[index]
95                         if tE.ipAddress == tunerEntry.ipAddress and tE.tunerType == tunerEntry.tunerType and tE.entryType == tunerEntry.entryType:
96                                 del self._config[index]
97                                 return True
98                         else:
99                                 Log.w("%s not equal %s, so not deleted!")
100                 return False
101
102         def reload(self):
103                 if not fileExists(self.CONFIG_FILE):
104                         return True
105                 try:
106                         with open(self.CONFIG_FILE) as f:
107                                 self._config = {}
108                                 for line in f:
109                                         Log.i("parsing: %s" % (line,))
110                                         if line.startswith("#"):
111                                                 Log.d("skipping comment")
112                                                 continue
113                                         try:
114                                                 num, data = line.strip().split("=")
115                                                 pairs = data.split(",")
116                                                 tuner_data = {}
117                                                 for pair in pairs:
118                                                         key, value = pair.split(":")
119                                                         tuner_data[key] = value
120                                                 self._config[int(num)] = TunerEntry(tuner_data)
121                                         except Exception as e:
122                                                 Log.w(e)
123                                                 continue
124                 except Exception as e:
125                         Log.w(e)
126                         return False
127                 Log.i(self._config)
128                 return True
129
130         def save(self):
131                 with open(self.CONFIG_FILE, 'w') as f:
132                         f.write("#This file is managed by your dreambox! All comments will be lost on next save using the UI!\n")
133                         index = 0
134                         for tunerEntry in self._config.itervalues():
135                                 tmp = []
136                                 for key, value in tunerEntry.data().iteritems():
137                                         tmp.append("%s:%s" % (key, value))
138                                 tuner_data = ",".join(tmp)
139                                 cfgline = "%s=%s\n" % (index, tuner_data)
140                                 f.write(cfgline)
141                                 index += 1
142                 self.reload()
143
144 vtunerClientConfig = ClientConfig()