root/make_serverxml.py @ 241

Revision 241, 2.0 kB (checked in by d0nut, 5 years ago)

try/catch for server xml python script

Line 
1# -*- coding: utf8 -*-
2#!/usr/bin/python
3
4import os
5import xml.dom.minidom
6
7
8def dive(mydir="."):
9    "searchs for all package.xml files"
10    l = []
11    for filename in os.listdir(mydir):
12        subdir = mydir+"/"+filename
13        if os.path.isdir(subdir):
14            tmp = dive(subdir)
15            if len(tmp):
16                l.extend(tmp)
17        elif filename == "package.xml":
18            l.append(mydir)
19    return l
20
21
22class Package(object):
23    "representating a package"
24
25    def __init__(self, path):
26        "construcor gets path, opens package.xml and extracts the relevant information"
27        try:
28            dom = xml.dom.minidom.parse(path+"/package.xml").documentElement
29        except:
30            print path,"nicht wohlgeformt"
31        self.name = dom.getAttribute("name")
32        self.packageinformation = dom.getElementsByTagName("packageinformation")
33        self.authorinformation = dom.getElementsByTagName("authorinformation")
34        self.requiredpackages = dom.getElementsByTagName("requiredpackages")
35        self.instructions = dom.getElementsByTagName("instructions")
36
37    def toxml(self):
38        "returns a dom object"
39        dom = xml.dom.minidom.Document().createElement("package")
40        dom.setAttribute("name", self.name)
41        return dom
42
43    def __str__(self):
44        "returns packagename"
45        return self.name
46
47
48class Section(object):
49    "representating a section"
50
51    def __init__(self):
52        "constructor inits new dom object with root elements"
53        self.tree = xml.dom.minidom.Document()
54        self.packages = xml.dom.minidom.Document().createElement("packages")
55        self.tree.appendChild(self.packages)
56
57    def addPackage(self, package):
58        "adds a new package"
59        self.packages.appendChild(package.toxml())
60
61    def __str__(self):
62        "returns xml output in utf-8 encoding"
63        return self.tree.toprettyxml(encoding='utf-8')
64
65
66#main applikation
67s = Section()
68for path in dive():
69    dom = Package(path)
70    s.addPackage(dom)
71
72print s
Note: See TracBrowser for help on using the browser.