add node iteration apis

TODO: add tests
This commit is contained in:
Ronny Pfannschmidt 2018-03-26 17:53:04 +02:00
parent ee51fa5881
commit 8805036fd8
1 changed files with 19 additions and 3 deletions

View File

@ -2,7 +2,7 @@ from __future__ import absolute_import, division, print_function
import os
from itertools import chain
from operator import itemgetter
import six
import py
import attr
@ -187,11 +187,18 @@ class Node(object):
"""find all marks with the given name on the node and its parents
:param str name: name of the marker
:returns: iterator over marks matching the name
:returns: iterator over marks matching the name"""
return map(itemgetter(1), self.find_markers_with_node(name))
def find_markers_with_node(self, name):
"""find all marks with the given name on the node and its parents
:param str name: name of the marker
:returns: iterator over (node, mark) matching the name
"""
for node in reversed(self.listchain()):
for mark in node._markers.find(name):
yield mark
yield node, mark
def iter_markers(self):
"""
@ -199,6 +206,15 @@ class Node(object):
"""
return chain.from_iterable(x._markers for x in reversed(self.listchain()))
def iter_markers_with_node(self):
"""
iterate over all markers of the node
returns sequence of tuples (node, mark)
"""
for node in reversed(self.listchain()):
for mark in node._markers:
yield node, mark
def get_marker(self, name):
""" get a marker object from this node or None if
the node doesn't have a marker with that name.