/usr/lib/python3/dist-packages/asdf/treeutil.py is in python3-asdf 1.3.3-1.
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 | # Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-
"""
Utility functions for managing tree-like data structures.
"""
from __future__ import absolute_import, division, unicode_literals, print_function
import inspect
import six
from .tagged import tag_object
def walk(top, callback):
"""
Walking through a tree of objects, calling a given function at
each node.
Parameters
----------
top : object
The root of the tree. May be a dict, list or other Python object.
callback : callable
A function to call at each node in the tree.
The callback is called on an instance after all of its
children have been visited (depth-first order).
Returns
-------
tree : object
The modified tree.
"""
for x in iter_tree(top):
callback(x)
def iter_tree(top):
"""
Iterate over all nodes in a tree, in depth-first order.
Parameters
----------
top : object
The root of the tree. May be a dict, list or other Python object.
callback : callable
A function to call at each node in the tree.
The callback is called on an instance after all of its
children have been visited (depth-first order).
Returns
-------
tree : object
The modified tree.
"""
seen = set()
def recurse(tree):
tree_id = id(tree)
if tree_id in seen:
return
if isinstance(tree, (list, tuple)):
seen.add(tree_id)
for val in tree:
for sub in recurse(val):
yield sub
seen.remove(tree_id)
elif isinstance(tree, dict):
seen.add(tree_id)
for val in six.itervalues(tree):
for sub in recurse(val):
yield sub
seen.remove(tree_id)
yield tree
return recurse(top)
def walk_and_modify(top, callback):
"""Modify a tree by walking it with a callback function. It also has
the effect of doing a deep copy.
Parameters
----------
top : object
The root of the tree. May be a dict, list or other Python object.
callback : callable
A function to call at each node in the tree. It takes either
one or two arguments:
- an instance from the tere
- a json id (optional)
It may return a different instance in order to modify the
tree.
The json id is the context under which any relative URLs
should be resolved. It may be `None` if no ids are in the file
The callback is called on an instance after all of its
children have been visited (depth-first order).
Returns
-------
tree : object
The modified tree.
"""
# For speed reasons, there are two different versions of the inner
# function
seen = set()
def recurse(tree):
id_tree = id(tree)
if id_tree in seen:
return tree
if isinstance(tree, dict):
result = tree.__class__()
seen.add(id_tree)
for key, val in six.iteritems(tree):
val = recurse(val)
if val is not None:
result[key] = val
seen.remove(id_tree)
if hasattr(tree, '_tag'):
result = tag_object(tree._tag, result)
elif isinstance(tree, (list, tuple)):
seen.add(id_tree)
result = tree.__class__(
[recurse(val) for val in tree])
seen.remove(id_tree)
if hasattr(tree, '_tag'):
result = tag_object(tree._tag, result)
else:
result = tree
result = callback(result)
return result
def recurse_with_json_ids(tree, json_id):
id_tree = id(tree)
if id_tree in seen:
return tree
if isinstance(tree, dict):
if 'id' in tree:
json_id = tree['id']
result = tree.__class__()
seen.add(id_tree)
for key, val in six.iteritems(tree):
val = recurse_with_json_ids(val, json_id)
if val is not None:
result[key] = val
seen.remove(id_tree)
if hasattr(tree, '_tag'):
result = tag_object(tree._tag, result)
elif isinstance(tree, (list, tuple)):
seen.add(id_tree)
result = tree.__class__(
[recurse_with_json_ids(val, json_id) for val in tree])
seen.remove(id_tree)
if hasattr(tree, '_tag'):
result = tag_object(tree._tag, result)
else:
result = tree
result = callback(result, json_id)
return result
if callback.__code__.co_argcount == 2:
return recurse_with_json_ids(top, None)
else:
return recurse(top)
|