aboutsummaryrefslogtreecommitdiff
blob: 35c79f98a2487179ebd4c66b1468ce066389b9f6 (plain)
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
"""

    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:
                #pdb.set_trace()
                return "file:///"+osp.abspath(osp.join(self.docRoot, augeas_utils.stripBothSlashes(elem[0].get("docurl"))))
        except etree.XPathEvalError:
            pass
        return None