/usr/share/pyshared/rtslib/config_tree.py is in python-rtslib 1:3.0~pre4.1~g1b33ceb-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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | '''
This file is part of LIO(tm).
Copyright (c) 2012-2014 by Datera, Inc.
More information on www.datera.io.
Original author: Jerome Martin <jxm@netiant.com>
Datera and LIO are trademarks of Datera, Inc., which may be registered in some
jurisdictions.
Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.
'''
import re, copy, logging
DEBUG = False
if DEBUG:
logging.basicConfig()
log = logging.getLogger('ConfigTree')
log.setLevel(logging.DEBUG)
else:
log = logging.getLogger('ConfigTree')
log.setLevel(logging.INFO)
NO_VALUE = '~~~'
def match_key(search_key, key):
'''
Matches search_key and key tuple items-for-item, with search_key containing
regular expressions patterns or None values, and key containing string ir
None values.
'''
log.debug("match_key(%s, %s)" % (search_key, key))
if len(search_key) == len(key):
for idx, pattern in enumerate(search_key):
item = key[idx]
if not pattern.endswith('$'):
pattern = "%s$" % pattern
if item is None and pattern is None:
continue
elif item is None:
break
else:
match = re.match(pattern, item)
if match is None:
break
else:
return True
class ConfigTreeError(Exception):
pass
class ConfigTree(object):
'''
An ordered tree structure to hold configuration data.
A node can be referred to by its path, relative to the current node.
A path is a list of keys, each key a tuple of either string or None items.
'''
def __init__(self, data=None,
sort_key=lambda x:x,
key_to_string=lambda x:str(x)):
'''
Initializes a new ConfigTree.
The optional sort_key is a function used when ordering children of a
configuration node.
The optional key_to_string is a function used when converting a node
key to string.
Direct instanciation should only happen for the root node of the tree.
Adding a new node to the tree is achieved by using the set()
method of the desired parent for that new node.
'''
self.data = data
self._key = ()
self._nodes = {}
self._parent = None
self._sort_key = sort_key
self._key_to_string = key_to_string
def __repr__(self):
return "(%s)" % self.path_str
def __str__(self):
return self.path_str
def get_clone(self, parent=None):
'''
Returns a clone of the ConfigTree, not sharing any mutable data.
'''
clone = ConfigTree(copy.deepcopy(self.data),
self._sort_key,
self._key_to_string)
clone._parent = parent
clone._key = self._key
clone.data = copy.deepcopy(self.data)
for node in self.nodes:
clone._nodes[node.key] = node.get_clone(parent=clone)
return clone
@property
def root(self):
'''
Returns the root node of the tree.
'''
cur = self
while cur.parent:
cur = cur.parent
return cur
@property
def key(self):
'''
Returns the current node's key tuple.
'''
return self._key
@property
def key_str(self):
'''
Returns the current node's key as a string.
'''
return self._key_to_string(self.key)
@property
def path(self):
'''
Returns the node's full path from the tree root as a list of keys.
'''
if self.is_root:
path = []
else:
path = self.parent.path + [self._key]
return path
@property
def path_str(self):
'''
Returns the node's full path from the tree root as a string.
'''
strings = []
for key in self.path:
strings.append(self._key_to_string(key))
return " ".join(strings)
@property
def nodes(self):
'''
Returns the list of all children nodes, sorted with potential
dependencies first.
'''
nodes = sorted(self._nodes.values(), key=self._sort_key)
return nodes
@property
def keys(self):
'''
Generates all children nodes keys, sorted with potential
dependencies first.
'''
keys = (node.key for node in self.nodes)
return keys
@property
def parent(self):
'''
Returns the parent node of the current node, or None.
'''
return self._parent
@property
def is_root(self):
'''
Returns True if this is a root node, else False.
'''
return self._parent == None
def get(self, node_key):
'''
Returns the current node's child having node_key, or None.
'''
return self._nodes.get(node_key)
def set(self, node_key, node_data=None):
'''
Creates and adds a child node to the current node, and returns that new
node. If the node already exists, then a ConfigTreeError exception will
be raised. Else, the new node will be returned.
node_key is any tuple of strings
node_data is an optional arbitrary value
'''
if node_key not in self.keys:
new_node = ConfigTree(self.data,
self._sort_key,
self._key_to_string)
new_node._parent = self
new_node.data = node_data
new_node._key = node_key
self._nodes[node_key] = new_node
return new_node
else:
raise ConfigTreeError("Node already exists, cannot set: %s"
% self.get(node_key))
def cine(self, node_key, node_data=None):
'''
cine stands for create if not exist: it makes sure a node exists.
If it does not, it will create it using node_data.
Else node_data will not be updated.
Returns the matching node in all cases.
node_key is any tuple of strings
node_data is an optional arbitrary value
'''
if node_key in self.keys:
log.debug("cine(%s %s) -> Already exists"
% (self.path_str, node_key))
return self.get(node_key)
else:
log.debug("cine(%s %s) -> Creating"
% (self.path_str, node_key))
return self.set(node_key, node_data)
def update(self, node_key, node_data=None):
'''
If a node already has node_key as key, its data will be replaced with
node_data. Else, it will be created using node_data.
The matching node will be returned in both cases.
node_key is any tuple of strings.
node_data is an optional arbitrary value.
'''
try:
node = self.set(node_key, node_data)
except ConfigTreeError:
node = self.get(node_key)
node.data = node_data
return node
def delete(self, path):
'''
Given a path, deletes an entire subtree from the configuration,
relative to the current node.
The deleted subtree will be returned, or None is the path does not
exist or is empty. The path must be a list of node keys.
'''
log.debug("delete(%s) getting subtree" % str(path))
subtree = self.get_path(path)
log.debug("delete(%s) got subtree: %s"
% (str(path), subtree))
if subtree is not None:
del subtree.parent._nodes[subtree.key]
return subtree
def get_path(self, path):
'''
Returns either the node matching path, relative to the current node, or
None if the path does not exists.
'''
log.debug("get_path(%s)" % str(path))
cur = self
log.debug("get_path(%s) - cur: %s" % (str(path), cur))
if path:
for key in path:
cur = cur.get(key)
if cur is None:
break
else:
return cur
def search(self, search_path, node_filter=lambda x:x):
'''
Returns a list of nodes matching the search_path, relative to the
current node, or an empty list if no match was found.
The search_path is a list of node search_key. Each will be matched
against node key tuples items-for-item, with search_key containing
regular expressions patterns or None values, and key containing string
or None values.
node_filter is a function applied to each node before returning it:
node_filter(node_in) -> node_out | None (aka filtered out)
'''
results = []
if search_path:
search_key = search_path[0]
for node in self.nodes:
if match_key(search_key, node.key):
if search_path[1:]:
results.extend(node.search(search_path[1:]))
else:
node_out = node_filter(node)
if node_out is not None:
results.append(node_out)
return results
def walk(self, node_filter=lambda x:x):
'''
Returns a generator yielding our children's tree in depth-first order.
node_filter is a function applied to each node before dumping it:
node_filter(node_in) -> node_out | None (aka filtered out)
When a node is filtered out, its children will still be walked and
filtered/yielded as usual.
'''
for node_in in self.nodes:
node_out = node_filter(node_in)
if node_out is not None:
yield node_out
for next in node_in.walk(node_filter):
yield next
|