• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

earwig / mwparserfromhell / 10014163542

19 Jul 2024 08:49PM CUT coverage: 99.201% (-0.002%) from 99.203%
10014163542

Pull #326

github

web-flow
Merge 8c23031f1 into 4e73af2fa
Pull Request #326: Make fallthrough explicit in tok_parse.c

2979 of 3003 relevant lines covered (99.2%)

9.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.94
/src/mwparserfromhell/utils.py
1
# Copyright (C) 2012-2023 Ben Kurtovic <ben.kurtovic@gmail.com>
2
#
3
# Permission is hereby granted, free of charge, to any person obtaining a copy
4
# of this software and associated documentation files (the "Software"), to deal
5
# in the Software without restriction, including without limitation the rights
6
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
# copies of the Software, and to permit persons to whom the Software is
8
# furnished to do so, subject to the following conditions:
9
#
10
# The above copyright notice and this permission notice shall be included in
11
# all copies or substantial portions of the Software.
12
#
13
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
# SOFTWARE.
20

21
"""
22
This module contains accessory functions for other parts of the library. Parser
23
users generally won't need stuff from here.
24
"""
25

26
from __future__ import annotations
10✔
27

28
__all__ = ["parse_anything"]
10✔
29

30
import typing
10✔
31
from typing import Any
10✔
32

33
if typing.TYPE_CHECKING:
10✔
34
    from .wikicode import Wikicode
×
35

36

37
def parse_anything(
10✔
38
    value: Any, context: int = 0, *, skip_style_tags: bool = False
39
) -> Wikicode:
40
    """Return a :class:`.Wikicode` for *value*, allowing multiple types.
41

42
    This differs from :meth:`.Parser.parse` in that we accept more than just a
43
    string to be parsed. Strings, bytes, integers (converted to strings),
44
    ``None``, existing :class:`.Node` or :class:`.Wikicode` objects, as well
45
    as an iterable of these types, are supported. This is used to parse input
46
    on-the-fly by various methods of :class:`.Wikicode` and others like
47
    :class:`.Template`, such as :meth:`wikicode.insert() <.Wikicode.insert>`
48
    or setting :meth:`template.name <.Template.name>`.
49

50
    Additional arguments are passed directly to :meth:`.Parser.parse`.
51
    """
52
    # pylint: disable=cyclic-import,import-outside-toplevel
53
    from .nodes import Node
10✔
54
    from .parser import Parser
10✔
55
    from .smart_list import SmartList
10✔
56
    from .wikicode import Wikicode
10✔
57

58
    if isinstance(value, Wikicode):
10✔
59
        return value
10✔
60
    if isinstance(value, Node):
10✔
61
        return Wikicode(SmartList([value]))
10✔
62
    if isinstance(value, str):
10✔
63
        return Parser().parse(value, context, skip_style_tags)
10✔
64
    if isinstance(value, bytes):
10✔
65
        return Parser().parse(value.decode("utf8"), context, skip_style_tags)
10✔
66
    if isinstance(value, int):
10✔
67
        return Parser().parse(str(value), context, skip_style_tags)
10✔
68
    if value is None:
10✔
69
        return Wikicode(SmartList())
10✔
70
    if hasattr(value, "read"):
10✔
71
        return parse_anything(value.read(), context, skip_style_tags=skip_style_tags)
×
72
    try:
10✔
73
        nodelist = SmartList()
10✔
74
        for item in value:
10✔
75
            nodelist += parse_anything(
10✔
76
                item, context, skip_style_tags=skip_style_tags
77
            ).nodes
78
        return Wikicode(nodelist)
10✔
79
    except TypeError as exc:
10✔
80
        error = (
10✔
81
            "Needs string, Node, Wikicode, file, int, None, or "
82
            "iterable of these, but got {0}: {1}"
83
        )
84
        raise ValueError(error.format(type(value).__name__, value)) from exc
10✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc