File: xmlfile.py

package info (click to toggle)
aptoncd 0.1-1.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,572 kB
  • ctags: 544
  • sloc: python: 3,125; xml: 1,216; makefile: 125; sh: 6
file content (110 lines) | stat: -rw-r--r-- 3,739 bytes parent folder | download
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
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
######################################################
##
#  This program 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; version 2 only.
#
#  This program 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.
##
######################################################
## 
#  Project: AptOnCd
#  File: xmlfile.py
#  Author: Alfredo Jr. <junix>
#  Creation: 29/10/2006
#  Changed: 12/11/2006
#  Purpose: XML Class
##
######################################################

import xml.dom.minidom
import string
import tempfile

import utils

(BOLVAL, METHOD, HOST, DISTRIBUTION, VERSION, SECTION, ARCHITECTURE, PATH, MEDIA) = range(9)

TEMPDIR = tempfile.mkdtemp()

class XMLFile:
	def node_text(self, node):
		text = ''
		for child in node.childNodes:
			if child.nodeType is child.TEXT_NODE:
				text += child.data
			return text

	def parse(self, file):
		children_names = []
		if not utils.fileExist(file):
			self.createDefaultConfigFile(file)
			
		x = xml.dom.minidom.parse(file)
		nodes = x.documentElement
		children1 = [node for node in nodes.childNodes if node.nodeType == x.ELEMENT_NODE]
		for father in children1:
			children2 = [node for node in father.childNodes if node.nodeType == x.ELEMENT_NODE]
			for child in children2:
				children_names.append(child.nodeName + ', ' + self.node_text(child))
		return (children_names)

	def createDefaultConfigFile(self, file):
		"""Persists the server list and its folders to a xml file."""
		util = utils.SystemInfo()
		aFile = open(file,"w")
		aFile.write('<?xml version="1.0" encoding="UTF-8" ?>\n')
		aFile.write('<download version="1.0">\n')
		aFile.write('    <settings>\n')
		if util.distro == 'ubuntu':
			aFile.write('        <host>archive.ubuntu.com</host>\n')
		else:
			aFile.write('        <host>ftp.debian.org</host>\n')
		aFile.write('        <dist>%s</dist>\n' % util.distro )
		aFile.write('        <method>http</method>\n')
		aFile.write('        <version>%s</version>\n' % util.codename)
		aFile.write('        <section>main</section>\n')
		aFile.write('        <arch>%s</arch>\n'  % util.architecture)
		aFile.write('        <path>%s</path>\n' % TEMPDIR)
		aFile.write('        <media>CD</media>\n')
		aFile.write('    </settings>\n')
		aFile.write('</download>\n')
		aFile.close()

	def load_conf(self, file):
		util = utils.SystemInfo()
		host = 'archive.ubuntu.com'
		dist =  util.distro
		method = 'http'
		version = util.codename
		section = 'main'
		arch = util.architecture
		path = TEMPDIR
		media = 'CD'
		try:
			node_text = self.parse(file)
			for child in node_text:
				if child.split(",")[0] == 'host':
					host = string.strip(child.split(",")[1])
				elif child.split(",")[0] == 'dist':
					dist = string.strip(child.split(",")[1])
				elif child.split(",")[0] == 'method':
					method = string.strip(child.split(",")[1])
				elif child.split(",")[0] == 'version':
					version = string.strip(child.split(",")[1])
				elif child.split(",")[0] == 'section':
					section = string.strip(child.split(",")[1])
				elif child.split(",")[0] == 'arch':
					arch = string.strip(child.split(",")[1])
				elif child.split(",")[0] == 'path':
					path = string.strip(child.split(",")[1]).encode('utf8')
				elif child.split(",")[0] == 'media':
					media = string.strip(child.split(",")[1])	
		except:
			return [False,'','','','','','','','']
		return [True, method, host, dist,  version, section, arch, path, media]