Trying to fix iotlab driver, need to be tested
[sfa.git] / sfatables / xmlextension.py
1 # Matches and targets are specified using XML files.
2 # They provide the following information:
3 #   - The context required by the match
4 #   - The processor that actually implements the match or target
5 #   - The parameters that the processor needs to evaluate the context
6
7 import libxml2
8
9 class Xmlextension:
10     def __init__(self, file_path):
11
12         self.context = ""
13         self.processor = ""
14         self.operand = "VALUE"
15         self.arguments = []
16         self.terminal = 0
17
18         self.xmldoc = libxml2.parseFile(file_path)
19
20         # TODO: Check xmldoc against a schema
21         p = self.xmldoc.xpathNewContext()
22
23         # <context select="..."/>
24         # <rule><argument param="..."/></rule>
25         # <processor name="..."/>
26
27         context = p.xpathEval('//context/@select')
28         self.context = context[0].content
29
30         processor = p.xpathEval('//processor/@filename')
31         self.context = processor[0].content
32
33         name = p.xpathEval('//rule/argument/name')
34         help = p.xpathEval('//rule/argument/help')
35         target = p.xpathEval('//rule/argument/operand')
36
37         context = p.xpathEval('//attributes/attribute[@terminal="yes"]')
38         self.terminal = (context != [])
39
40         self.arguments = map(lambda (name,help,target):{'name':name.content,'help':help.content,'target':target.content}, zip(name,help,target))
41         
42         p.xpathFreeContext()
43         self.xmldoc.freeDoc()
44
45         return
46