aboutsummaryrefslogtreecommitdiff
blob: 5af010f8d8c79e2c1b74c44f79e52d6cf3231b07 (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
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
#! /usr/bin/python
#
# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk>
# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
# Copyright(c) 2004, Gentoo Foundation
#
# Licensed under the GNU General Public License, v2
#
# $Header: $

import sys

class ParseError:
	def __init__(self, error):
		self.error = error

class NodeIter:
	def __init__(self, node):
		self._node = node
		self._index = 0
	def next(self):
		self._index += 1
		if self._index >= len(self._node._kids):
			raise StopIteration
		return self._node._kids[self._index]
        
class Node:
	def __init__(self, name = None, value = None):
		self.name = name
		self.value = value
		self._kids = []
	
	def __iter__(self):
		return NodeIter(self)
	
	def add_kid(self, kid):
		for x in self._kids:
			if x.name == kid.name:
				return
			
		self._kids.append(kid)
	
	def _dump_kids(self, ous, indent):
		for x in self._kids:
			x.dump(ous, indent + 1)
	
	"""
	Dump self as text to stream.
	"""
	def dump(self, ous, indent = 0):
		if self.name:
			ous.write((" " * indent) + self.name + " = " + self.value + "\n")
		
		self._dump_kids(ous, indent)
	
	"""
	Output self as text to stream using the given format.
	"""
	def output(self, ous, before, between, after, wrap = None, indent = ""):		
		if self.name:
			outval = self.value
		
			if wrap != None:
				outval = outval.replace(wrap, wrap + "\n" + indent)
			
			ous.write(before + self.name + between + outval + after + "\n")
		
		for x in self._kids:
			x.output(ous, before, between, after, wrap, indent)

	"""
	Returns a lists of all the node names.
	"""
	def node_names(self):
		names = []
		
		if self.name:
			names.append(self.name)
		
		for x in self._kids:
			names.extend(x.node_names())
		
		return names

	"""
	Find a given node name in a tree.
	
	@param tree - the tree to search in
	@param nodename - the name of the node to search for
	
	@return reference to the found node, if any
	"""
	def find_node(self, nodename):
		if self.name == nodename:
			return self

		else:
			for x in self._kids:
				y = x.find_node(nodename)
				
				if y != None:
					return y
		
		return None
		
if __name__ == "__main__":
	print "This is not an executable module"