[DreamExplorer]
[enigma2-plugins.git] / mphelp / src / XMLHelp.py
1 from MPHelp import HelpPage
2 from xml.etree.cElementTree import parse as cet_parse
3
4 class XMLHelpPage(HelpPage):
5         def __init__(self, node, translate=_):
6                 # calling HelpPage.__init__ is not required
7                 self.node = node
8                 self.__translate = translate
9
10         def getText(self):
11                 node = self.node.find('text')
12                 if node is not None:
13                         return self.__translate(node.get('value', ''))
14                 return ""
15
16         def getTitle(self):
17                 return self.__translate(self.node.get('title', ''))
18
19 class XMLHelpReader:
20         def __init__(self, filename, translate=_):
21                 # this may raise an exception, it is up to the caller to handle that
22                 self.__dom = cet_parse(filename).getroot()
23                 self.__translate = translate
24
25         def __getitem__(self, index):
26                 if self.__dom:
27                         if index == 0:
28                                 _ = self.__translate
29                                 caption = self.__dom.get('caption', '')
30                                 return lambda: _(caption)
31                         elif index == 1:
32                                 _ = self.__translate
33                                 return lambda: [XMLHelpPage(x, _) for x in self.__dom.findall('page')]
34                         elif index == 2:
35                                 return self.__dom.get('skin', "") # additional skin name
36                         raise IndexError('no more indices')
37                 raise RuntimeError('no valid dom')
38
39 __all__ = ['XMLHelpReader']