From f0a591b32356470449dccd74304cdd1a791e9cf2 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 30 Nov 2013 22:10:36 -0500 Subject: [PATCH] Move get_children() out of utils. --- mwparserfromhell/utils.py | 11 +---------- mwparserfromhell/wikicode.py | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/mwparserfromhell/utils.py b/mwparserfromhell/utils.py index 4248652..46abcc0 100644 --- a/mwparserfromhell/utils.py +++ b/mwparserfromhell/utils.py @@ -31,16 +31,7 @@ from .compat import bytes, str from .nodes import Node from .smart_list import SmartList -__all__ = ["get_children", "parse_anything"] - -def get_children(node, contexts=False, parent=None): - """Iterate over all child :py:class:`.Node`\ s of a given *node*.""" - ## DON'T MAKE THIS RECURSIVE, USE A STACK! - yield (parent, node) if contexts else node - for code in node.__children__(): - for descendant in code.nodes: - for child in get_children(descendant, contexts, code): - yield child +__all__ = ["parse_anything"] def parse_anything(value, context=0): """Return a :py:class:`~.Wikicode` for *value*, allowing multiple types. diff --git a/mwparserfromhell/wikicode.py b/mwparserfromhell/wikicode.py index 8b9daff..2f21a67 100644 --- a/mwparserfromhell/wikicode.py +++ b/mwparserfromhell/wikicode.py @@ -21,6 +21,7 @@ # SOFTWARE. from __future__ import unicode_literals +from collections import deque from itertools import chain import re @@ -28,7 +29,7 @@ from .compat import py3k, range, str from .nodes import (Argument, Comment, ExternalLink, Heading, HTMLEntity, Node, Tag, Template, Text, Wikilink) from .string_mixin import StringMixIn -from .utils import get_children, parse_anything +from .utils import parse_anything __all__ = ["Wikicode"] @@ -53,6 +54,15 @@ class Wikicode(StringMixIn): return "".join([str(node) for node in self.nodes]) @staticmethod + def _get_children(node, contexts=False, parent=None): + """Iterate over all child :py:class:`.Node`\ s of a given *node*.""" + yield (parent, node) if contexts else node + for code in node.__children__(): + for child in code.nodes: + for result in Wikicode._get_children(child, contexts, code): + yield result + + @staticmethod def _slice_replace(code, index, old, new): """Replace the string *old* with *new* across *index* in *code*.""" nodes = [str(node) for node in code.get(index)] @@ -76,7 +86,7 @@ class Wikicode(StringMixIn): if not recursive: return self, mkslice(self.index(obj)) for i, node in enumerate(self.nodes): - for context, child in get_children(node, contexts=True): + for context, child in self._get_children(node, contexts=True): if obj is child: if not context: context = self @@ -240,7 +250,7 @@ class Wikicode(StringMixIn): equivalent = (lambda o, n: o is n) if strict else (lambda o, n: o == n) for i, node in enumerate(self.nodes): if recursive: - for child in get_children(node): + for child in self._get_children(node): if equivalent(obj, child): return i elif equivalent(obj, node): @@ -405,7 +415,8 @@ class Wikicode(StringMixIn): if matches and not callable(matches): pat, matches = matches, lambda obj: re.search(pat, str(obj), flags) if recursive: - nodes = chain.from_iterable(get_children(n) for n in self.nodes) + getter = self._get_children + nodes = chain.from_iterable(getter(n) for n in self.nodes) else: nodes = self.nodes for node in nodes: