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

earwig / mwparserfromhell / 6080278913

05 Sep 2023 04:44AM CUT coverage: 99.136% (+0.04%) from 99.098%
6080278913

push

github

earwig
Fix pickling SmartLists (fixes #289)

23 of 23 new or added lines in 4 files covered. (100.0%)

2982 of 3008 relevant lines covered (99.14%)

6.9 hits per line

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

94.12
/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
"""
7✔
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
7✔
27

28
__all__ = ["parse_anything"]
7✔
29

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

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

36

37
def parse_anything(
7✔
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
7✔
54
    from .parser import Parser
7✔
55
    from .smart_list import SmartList
7✔
56
    from .wikicode import Wikicode
7✔
57

58
    if isinstance(value, Wikicode):
7✔
59
        return value
7✔
60
    if isinstance(value, Node):
7✔
61
        return Wikicode(SmartList([value]))
7✔
62
    if isinstance(value, str):
7✔
63
        return Parser().parse(value, context, skip_style_tags)
7✔
64
    if isinstance(value, bytes):
7✔
65
        return Parser().parse(value.decode("utf8"), context, skip_style_tags)
7✔
66
    if isinstance(value, int):
7✔
67
        return Parser().parse(str(value), context, skip_style_tags)
7✔
68
    if value is None:
7✔
69
        return Wikicode(SmartList())
7✔
70
    if hasattr(value, "read"):
7✔
71
        return parse_anything(value.read(), context, skip_style_tags=skip_style_tags)
×
72
    try:
7✔
73
        nodelist = SmartList()
7✔
74
        for item in value:
7✔
75
            nodelist += parse_anything(
7✔
76
                item, context, skip_style_tags=skip_style_tags
77
            ).nodes
78
        return Wikicode(nodelist)
7✔
79
    except TypeError as exc:
7✔
80
        error = (
7✔
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
7✔
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