提交 0d391121 编写于 作者: L Lukáš Doktor

avocado.core.tree: Cache TreeNode.environment

Usually we create single tree and then multiplex it's values. This
generates lots of TreeNode.environment calls. This patch caches
TreeNode.environment calls and returns last known version.

There is one drawback, in case you modify tree structure after calling
TreeNode.environment you must call TreeNode.set_environment_dirty,
otherwise you get the outdated results.
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>

Conflicts:
	avocado/core/tree.py
上级 63abc384
......@@ -55,6 +55,7 @@ class TreeNode(object):
self.value = value
self.parent = parent
self.children = []
self._environment = None
for child in children:
self.add_child(child)
......@@ -142,21 +143,24 @@ class TreeNode(object):
return self.get_environment()
def get_environment(self):
def update_or_extend(target, source):
for k, _ in source.items():
if k in target and isinstance(target[k], list):
target[k].extend(source[k])
else:
if isinstance(source[k], list):
target[k] = source[k][:]
if self._environment is None:
self._environment = (self.parent.environment.copy()
if self.parent else {})
for key, value in self.value.iteritems():
if isinstance(value, list):
if (key in self._environment
and isinstance(self._environment[key], list)):
self._environment[key] = self._environment[key] + value
else:
target[k] = source[k]
env = {}
rev_parents = reversed(self.get_parents())
for parent in rev_parents:
update_or_extend(env, parent.value)
update_or_extend(env, self.value)
return env
self._environment[key] = value
else:
self._environment[key] = value
return self._environment
def set_environment_dirty(self):
for child in self.children:
child.set_environment_dirty()
self._environment = None
def iter_children_preorder(self, node=None):
q = collections.deque()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册