From acb7e579045e4ab74fbc025894d49cad72241b51 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 23 Feb 2013 12:14:06 -0500 Subject: [PATCH] Make mwparserfromhell.parser() be an alias for parse_anything(). Some other changes, including removal of the 'string' import in the tokenizer. --- mwparserfromhell/__init__.py | 5 ++--- mwparserfromhell/parser/__init__.py | 5 +---- mwparserfromhell/parser/tokenizer.py | 5 ++--- mwparserfromhell/utils.py | 14 +++++++------- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/mwparserfromhell/__init__.py b/mwparserfromhell/__init__.py index e18000b..99bc0c2 100644 --- a/mwparserfromhell/__init__.py +++ b/mwparserfromhell/__init__.py @@ -34,7 +34,6 @@ __license__ = "MIT License" __version__ = "0.2.dev" __email__ = "ben.kurtovic@verizon.net" -from . import nodes, parser, smart_list, string_mixin, wikicode +from . import compat, nodes, parser, smart_list, string_mixin, utils, wikicode -parse = lambda text: parser.Parser(text).parse() -parse.__doc__ = "Short for :py:meth:`.Parser.parse`." +parse = utils.parse_anything diff --git a/mwparserfromhell/parser/__init__.py b/mwparserfromhell/parser/__init__.py index 074b9ba..3f034f6 100644 --- a/mwparserfromhell/parser/__init__.py +++ b/mwparserfromhell/parser/__init__.py @@ -26,10 +26,7 @@ modules: the :py:mod:`~.tokenizer` and the :py:mod:`~.builder`. This module joins them together under one interface. """ -try: - from ._builder import CBuilder as Builder -except ImportError: - from .builder import Builder +from .builder import Builder try: from ._tokenizer import CTokenizer as Tokenizer except ImportError: diff --git a/mwparserfromhell/parser/tokenizer.py b/mwparserfromhell/parser/tokenizer.py index eead131..c02e353 100644 --- a/mwparserfromhell/parser/tokenizer.py +++ b/mwparserfromhell/parser/tokenizer.py @@ -23,7 +23,6 @@ from __future__ import unicode_literals from math import log import re -import string from . import contexts from . import tokens @@ -377,9 +376,9 @@ class Tokenizer(object): else: numeric = hexadecimal = False - valid = string.hexdigits if hexadecimal else string.digits + valid = "0123456789abcdefABCDEF" if hexadecimal else "0123456789" if not numeric and not hexadecimal: - valid += string.ascii_letters + valid += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" if not all([char in valid for char in this]): self._fail_route() diff --git a/mwparserfromhell/utils.py b/mwparserfromhell/utils.py index 83264e2..b797419 100644 --- a/mwparserfromhell/utils.py +++ b/mwparserfromhell/utils.py @@ -34,16 +34,16 @@ from .smart_list import SmartList def parse_anything(value): """Return a :py:class:`~.Wikicode` for *value*, allowing multiple types. - This differs from :py:func:`mwparserfromhell.parse` in that we accept more - than just a string to be parsed. Unicode objects (strings in py3k), strings - (bytes in py3k), integers (converted to strings), ``None``, existing + This differs from :py:meth:`.Parser.parse` in that we accept more than just + a string to be parsed. Unicode objects (strings in py3k), strings (bytes in + py3k), integers (converted to strings), ``None``, existing :py:class:`~.Node` or :py:class:`~.Wikicode` objects, as well as an iterable of these types, are supported. This is used to parse input on-the-fly by various methods of :py:class:`~.Wikicode` and others like :py:class:`~.Template`, such as :py:meth:`wikicode.insert() <.Wikicode.insert>` or setting :py:meth:`template.name <.Template.name>`. """ - from . import parse + from .parser import Parser from .wikicode import Wikicode if isinstance(value, Wikicode): @@ -51,11 +51,11 @@ def parse_anything(value): elif isinstance(value, Node): return Wikicode(SmartList([value])) elif isinstance(value, str): - return parse(value) + return Parser(value).parse() elif isinstance(value, bytes): - return parse(value.decode("utf8")) + return Parser(value.decode("utf8")).parse() elif isinstance(value, int): - return parse(str(value)) + return Parser(str(value)).parse() elif value is None: return Wikicode(SmartList()) try: