tubelib: import/code cleanup
[enigma2-plugins.git] / tubelib / src / ChannelListTubeServiceHelper.py
1 from Screens.ChoiceBox import ChoiceBox
2 from Tools.Log import Log
3
4 class ChannelListServiceProviderBase(object):
5         def __init__(self, session, csel, callback):
6                 self._session = session
7                 self._csel = csel
8                 self._callback = callback
9                 self._tubeUri = None
10                 self._createServiceForChannelList()
11
12         def _createServiceForChannelList(self):
13                 raise NotImplementedError
14
15         def _finish(self, ref):
16                 self._callback(self._csel, ref)
17
18 class ChannelListTubeServiceHelper(object):
19         PROVIDER = {}
20
21         @staticmethod
22         def addProvider(key, cls):
23                 Log.w("%s, %s" % (key, cls))
24                 assert(isinstance(cls, ChannelListServiceProviderBase))
25                 mykey = key
26                 i = 1
27                 while ChannelListTubeServiceHelper.PROVIDER.has_key(mykey):
28                         mykey = "%s_%s" % (key, i)
29                         i += 1
30                 ChannelListTubeServiceHelper.PROVIDER[mykey] = cls
31
32         def __init__(self, session, csel, callback):
33                 self._session = session
34                 self._csel = csel
35                 self._callback = callback
36                 self.addTubeServiceToChannelList()
37
38         def _run(self, provider):
39                 return provider(self._session, self._csel, self._callback)
40
41         def addTubeServiceToChannelList(self):
42                 if not self.PROVIDER:
43                         return False
44                 if len(self.PROVIDER) == 1 and False:
45                         provider = self.PROVIDER[self.PROVIDER.keys()[0]]
46                         return self._run(provider)
47                 self._pickTube()
48
49         def _pickTube(self):
50                 choices = []
51                 for key, cls in self.PROVIDER.iteritems():
52                         choices.append((cls.DESCRIPTION, key))
53                 self._session.openWithCallback(self._onTubePicked, ChoiceBox, list=choices, windowTitle=_("Pick a Source"), title="Available Sources")
54
55         def _onTubePicked(self, data):
56                 if data:
57                         provider = self.PROVIDER[data[1]]
58                         self._run(provider)