1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
"""
This file is part of the Ventoo program.
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this software. If not, see <http://www.gnu.org/licenses/>.
Copyright 2010 Christopher Harvey
"""
import os.path as osp
from lxml import etree
import augeas_utils
import search_paths
class VentooModule:
def __init__(self, moduleName):
#see if we can find the module files
found = False;
for p in search_paths.modules:
thisPath = osp.join(p,moduleName,"main.xml")
if osp.isfile(thisPath):
self.pathFound = thisPath
found = True
self.docRoot = osp.join(p, moduleName)
break
if not found:
raise RuntimeError('Could not find '+moduleName+' Module')
self.xmlTree = etree.parse(self.pathFound)
#validate the module main.xml file.
#make sure it starts with <VentooModule>
self.xmlRoot = self.xmlTree.getroot()
if self.xmlRoot.tag != "VentooModule":
raise RuntimeError('Ventoo modules need to start with <VentooModule>')
def getChildrenOf(self, xPath):
children = self.xmlTree.xpath(osp.join(xPath, '*'))
ret = []
for i in range(len(children)):
if not children[i].tag.startswith('ventoo_'):
ret.extend([children[i]])
return ret
def getMultOf(self, xPath):
elem = self.xmlTree.xpath(osp.join(xPath))
if len(elem) >= 1:
return elem[0].get("mult")
else:
return '0'
def getDocURLOf(self, xPath):
try:
elem = self.xmlTree.xpath(osp.join(xPath))
if len(elem) >= 1 and not elem[0].get("docurl") == None:
return "file:///"+osp.abspath(osp.join(self.docRoot, augeas_utils.stripBothSlashes(elem[0].get("docurl"))))
except etree.XPathEvalError:
pass
return None
"""
Returns true if a comes before b
Raise ValueError if either a or b are not in the xml file.
a and b must both have the same parent node.
a and b are both tags, not paths.
"""
def comesBefore(self, a, b):
try:
elemAlist = self.xmlTree.xpath("//"+a)
elemBlist = self.xmlTree.xpath("//"+b)
except etree.XPathEvalError:
raise ValueError("Could not find one of "+b)
if len(elemBlist) != 1:
return ValueError("Could not find one of "+b)
if len(elemAlist) != 1:
return ValueError("Could not find one of "+a)
elemAparent = elemAlist[0].getparent()
elemBparent = elemBlist[0].getparent()
if elemBparent.tag != elemAparent.tag:
raise ValueError(a+" and "+b+" do not have the same parent.")
for child in elemAparent:
if child.tag == elemBlist[0].tag:
return False
if child.tag == elemAlist[0].tag:
return True
raise RuntimeError("Something is wrong in comesBefore in VentooModule...")
|