/usr/share/pyshared/reconfigure/nodes.py is in python-reconfigure 0.1.29-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | class Node (object):
"""
A base node class for the Node Tree.
This class represents a named container node.
"""
def __init__(self, name=None, *args, **kwargs):
"""
:param name: Node name
:param *args: Children
:param comment: Node comment string
:param origin: Node's source location (usually path to the file)
"""
self.name = name
self.origin = None
self.children = []
for node in list(args) + kwargs.pop('children', []):
self.append(node)
self.comment = kwargs.pop('comment', None)
self.__dict__.update(kwargs)
def __str__(self):
s = '(%s)' % self.name
if self.comment:
s += ' (%s)' % self.comment
s += '\n'
for child in self.children:
s += '\n'.join('\t' + x for x in str(child).splitlines()) + '\n'
return s
def __hash__(self):
return sum(hash(x) for x in [self.name, self.origin, self.comment] + self.children)
def __eq__(self, other):
if other is None:
return False
return \
self.name == other.name and \
self.comment == other.comment and \
self.origin == other.origin and \
set(self.children) == set(other.children)
def __iter__(self):
return iter(self.children)
def __len__(self):
return len(self.children)
def __nonzero__(self):
return True
def __getitem__(self, key):
if type(key) in (int, slice):
return self.children[key]
return self.get(key)
def __setitem__(self, key, value):
if type(key) is int:
self.children[key] = value
self.set_property(key, value)
def __contains__(self, item):
return item in self.children
def indexof(self, node):
"""
:returns: index of the node in the children array or ``None`` if it's not a child
"""
if node in self.children:
return self.children.index(node)
else:
return None
def get(self, name, default=None):
"""
:returns: a child node by its name or ``default``
"""
for child in self.children:
if child.name == name:
return child
if default:
self.append(default)
return default
def get_all(self, name):
"""
:returns: list of child nodes with supplied ``name``
"""
return [n for n in self.children if n.name == name]
def append(self, node):
if not node.origin:
node.origin = self.origin
self.children.append(node)
node.parent = self
def remove(self, node):
self.children.remove(node)
def replace(self, name, node=None):
"""
Replaces the child nodes by ``name``
:param node: replacement node or list of nodes
::
n.append(Node('a'))
n.append(Node('a'))
n.replace('a', None)
assert(len(n.get_all('a')) == 0)
"""
if name:
self.children = [c for c in self.children if c.name != name]
if node is not None:
if type(node) == list:
for n in node:
self.children.append(n)
else:
self.children.append(node)
def set_property(self, name, value):
"""
Creates or replaces a child :class:`PropertyNode` by name.
"""
node = self.get(name)
if not node:
node = PropertyNode(name, value)
self.append(node)
node.value = value
return self
class RootNode (Node):
"""
A special node class that indicates tree root
"""
class PropertyNode (Node):
"""
A node that serves as a property of its parent node.
"""
def __init__(self, name, value, comment=None):
"""
:param name: Property name
:param value: Property value
"""
Node.__init__(self, name, comment=comment)
self.value = value
def __eq__(self, other):
if other is None:
return False
return \
Node.__eq__(self, other) and \
self.value == other.value
def __str__(self):
s = '%s = %s' % (self.name, self.value)
if self.comment:
s += ' (%s)' % self.comment
return s
class IncludeNode (Node):
"""
A node that indicates a junction point between two config files
"""
def __init__(self, files):
"""
:param files: an includer-dependent config location specifier
"""
Node.__init__(self)
self.name = '<include>'
self.files = files
def __str__(self):
return '<include> %s' % self.files
|