~ubuntu-branches/ubuntu/feisty/aptoncd/feisty

« back to all changes in this revision

Viewing changes to xmlfile.py

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Proença
  • Date: 2007-02-02 14:00:59 UTC
  • Revision ID: james.westby@ubuntu.com-20070202140059-7p801vwa0h1yymq9
Tags: upstream-0.0.99+svn20070202
Import upstream version 0.0.99+svn20070202

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: iso-8859-15 -*-
 
3
######################################################
 
4
##
 
5
#  This program is free software; you can redistribute it and/or modify
 
6
#  it under the terms of the GNU General Public License as published
 
7
#  by the Free Software Foundation; version 2 only.
 
8
#
 
9
#  This program is distributed in the hope that it will be useful,
 
10
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
#  GNU General Public License for more details.
 
13
##
 
14
######################################################
 
15
## 
 
16
#  Project: AptOnCd
 
17
#  File: xmlfile.py
 
18
#  Author: Alfredo Jr. <junix>
 
19
#  Creation: 29/10/2006
 
20
#  Changed: 12/11/2006
 
21
#  Purpose: XML Class
 
22
##
 
23
######################################################
 
24
 
 
25
import xml.dom.minidom
 
26
import string
 
27
 
 
28
import utils
 
29
 
 
30
(BOLVAL, METHOD, HOST, DISTRIBUTION, VERSION, SECTION, ARCHITECTURE, PATH, MEDIA) = range(9)
 
31
 
 
32
class XMLFile:
 
33
        def node_text(self, node):
 
34
                text = ''
 
35
                for child in node.childNodes:
 
36
                        if child.nodeType is child.TEXT_NODE:
 
37
                                text += child.data
 
38
                        return text
 
39
 
 
40
        def parse(self, file):
 
41
                children_names = []
 
42
                if not utils.fileExist(file):
 
43
                        self.createDefaultConfigFile(file)
 
44
                        
 
45
                x = xml.dom.minidom.parse(file)
 
46
                nodes = x.documentElement
 
47
                children1 = [node for node in nodes.childNodes if node.nodeType == x.ELEMENT_NODE]
 
48
                for father in children1:
 
49
                        children2 = [node for node in father.childNodes if node.nodeType == x.ELEMENT_NODE]
 
50
                        for child in children2:
 
51
                                children_names.append(child.nodeName + ', ' + self.node_text(child))
 
52
                return (children_names)
 
53
 
 
54
        def createDefaultConfigFile(self, file):
 
55
                """Persists the server list and its folders to a xml file."""
 
56
                util = utils.SystemInfo()
 
57
                aFile = open(file,"w")
 
58
                aFile.write('<?xml version="1.0" encoding="UTF-8" ?>\n')
 
59
                aFile.write('<download version="1.0">\n')
 
60
                aFile.write('    <settings>\n')
 
61
                if util.distro == 'ubuntu':
 
62
                        aFile.write('        <host>archive.ubuntu.com</host>\n')
 
63
                else:
 
64
                        aFile.write('        <host>ftp.debian.org</host>\n')
 
65
                aFile.write('        <dist>%s</dist>\n' % util.distro )
 
66
                aFile.write('        <method>http</method>\n')
 
67
                aFile.write('        <version>%s</version>\n' % util.codename)
 
68
                aFile.write('        <section>main</section>\n')
 
69
                aFile.write('        <arch>%s</arch>\n'  % util.architecture)
 
70
                aFile.write('        <path>/tmp/aptoncd</path>\n')
 
71
                aFile.write('        <media>CD</media>\n')
 
72
                aFile.write('    </settings>\n')
 
73
                aFile.write('</download>\n')
 
74
                aFile.close()
 
75
 
 
76
        def load_conf(self, file):
 
77
                util = utils.SystemInfo()
 
78
                host = 'archive.ubuntu.com'
 
79
                dist =  util.distro
 
80
                method = 'http'
 
81
                version = util.codename
 
82
                section = 'main'
 
83
                arch = util.architecture
 
84
                path = '/tmp/aptoncd'
 
85
                media = 'CD'
 
86
                try:
 
87
                        node_text = self.parse(file)
 
88
                        for child in node_text:
 
89
                                if child.split(",")[0] == 'host':
 
90
                                        host = string.strip(child.split(",")[1])
 
91
                                elif child.split(",")[0] == 'dist':
 
92
                                        dist = string.strip(child.split(",")[1])
 
93
                                elif child.split(",")[0] == 'method':
 
94
                                        method = string.strip(child.split(",")[1])
 
95
                                elif child.split(",")[0] == 'version':
 
96
                                        version = string.strip(child.split(",")[1])
 
97
                                elif child.split(",")[0] == 'section':
 
98
                                        section = string.strip(child.split(",")[1])
 
99
                                elif child.split(",")[0] == 'arch':
 
100
                                        arch = string.strip(child.split(",")[1])
 
101
                                elif child.split(",")[0] == 'path':
 
102
                                        path = string.strip(child.split(",")[1]).encode('utf8')
 
103
                                elif child.split(",")[0] == 'media':
 
104
                                        media = string.strip(child.split(",")[1])       
 
105
                except:
 
106
                        return [False,'','','','','','','','']
 
107
                return [True, method, host, dist,  version, section, arch, path, media]