"""
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 .
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
self.xmlRoot = self.xmlTree.getroot()
if self.xmlRoot.tag != "VentooModule":
raise RuntimeError('Ventoo modules need to start with ')
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...")