mphelp: allow to use xml as container for the help
[enigma2-plugins.git] / mphelp / src / XMLHelp.py
1 from MPHelp import HelpPage
2 from xml.etree.cElementTree import parse as cet_parse
3 try:
4         from xml.etree.cElementTree import ParseError
5 except ImportError, ie:
6         ParseError = SyntaxError
7
8 class XMLHelpPage(HelpPage):
9         def __init__(self, node):
10                 # calling HelpPage.__init__ is not required
11                 self.node = node
12
13         def getText(self):
14                 node = self.node.find('text')
15                 if node is not None:
16                         return _(node.get('value', ''))
17                 return ""
18
19         def getTitle(self):
20                 return _(self.node.get('title', ''))
21
22 class XMLHelpReader:
23         def __init__(self, filename):
24                 # this may raise an exception, it is up to the caller to handle that
25                 self.__dom = cet_parse(filename).getroot()
26
27         def __getitem__(self, index):
28                 if self.__dom:
29                         if index == 0:
30                                 caption = self.__dom.get('caption', '')
31                                 return lambda: _(caption)
32                         elif index == 1:
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']