Skip to content

Commit

Permalink
Autoformat: black + clang-format + clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
earwig committed Sep 2, 2021
1 parent 911d7e5 commit 8cd0bdb
Show file tree
Hide file tree
Showing 68 changed files with 4,288 additions and 2,698 deletions.
13 changes: 13 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
BasedOnStyle: LLVM
AlignConsecutiveMacros: AcrossEmptyLines
AllowShortFunctionsOnASingleLine: Inline
AlwaysBreakAfterReturnType: TopLevelDefinitions
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Linux
ColumnLimit: 88
IndentPPDirectives: AfterHash
IndentWidth: 4
SpaceAfterCStyleCast: true
StatementMacros:
- PyObject_HEAD
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ dist
docs/_build
scripts/*.log
htmlcov/
compile_commands.json
.idea/
.pytest_cache/
17 changes: 11 additions & 6 deletions scripts/memtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

LOOPS = 10000


class Color:
GRAY = "\x1b[30;1m"
GREEN = "\x1b[92m"
Expand All @@ -63,11 +64,11 @@ def _parse_file(self, name, text):
data = {"name": None, "label": None, "input": None, "output": None}
for line in test.strip().splitlines():
if line.startswith("name:"):
data["name"] = line[len("name:"):].strip()
data["name"] = line[len("name:") :].strip()
elif line.startswith("label:"):
data["label"] = line[len("label:"):].strip()
data["label"] = line[len("label:") :].strip()
elif line.startswith("input:"):
raw = line[len("input:"):].strip()
raw = line[len("input:") :].strip()
if raw[0] == '"' and raw[-1] == '"':
raw = raw[1:-1]
raw = raw.encode("raw_unicode_escape")
Expand All @@ -81,7 +82,7 @@ def _load(self):
def load_file(filename):
with open(filename, "rU") as fp:
text = fp.read()
name = path.split(filename)[1][:0-len(extension)]
name = path.split(filename)[1][: 0 - len(extension)]
self._parse_file(name, text)

root = path.split(path.dirname(path.abspath(__file__)))[0]
Expand Down Expand Up @@ -119,8 +120,11 @@ def run(self):

tmpl = "{0}[{1:03}/{2}]{3} {4}: "
for i, (name, text) in enumerate(self._tests, 1):
sys.stdout.write(tmpl.format(Color.GRAY, i, len(self._tests),
Color.RESET, name.ljust(width)))
sys.stdout.write(
tmpl.format(
Color.GRAY, i, len(self._tests), Color.RESET, name.ljust(width)
)
)
sys.stdout.flush()
parent, child = Pipe()
p = Process(target=_runner, args=(text, child))
Expand Down Expand Up @@ -156,6 +160,7 @@ def _runner(text, child):
child.send("OK")
child.recv()


if __name__ == "__main__":
setlocale(LC_ALL, "")
MemoryTest().run()
55 changes: 32 additions & 23 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@

# Remove the command line argument as it isn't understood by setuptools:

sys.argv = [arg for arg in sys.argv
if arg not in ("--without-extension", "--with-extension")]
sys.argv = [
arg for arg in sys.argv if arg not in ("--without-extension", "--with-extension")
]


def build_ext_patched(self):
try:
Expand All @@ -63,33 +65,40 @@ def build_ext_patched(self):
print("Falling back to pure Python mode.")
del self.extensions[:]


if fallback:
build_ext.run, build_ext_original = build_ext_patched, build_ext.run

# Project-specific part begins here:

tokenizer = Extension("mwparserfromhell.parser._tokenizer",
sources=sorted(glob("src/mwparserfromhell/parser/ctokenizer/*.c")),
depends=sorted(glob("src/mwparserfromhell/parser/ctokenizer/*.h")))
tokenizer = Extension(
"mwparserfromhell.parser._tokenizer",
sources=sorted(glob("src/mwparserfromhell/parser/ctokenizer/*.c")),
depends=sorted(glob("src/mwparserfromhell/parser/ctokenizer/*.h")),
)

setup(
name = "mwparserfromhell",
packages = find_packages("src"),
package_dir = {"": "src"},
ext_modules = [tokenizer] if use_extension else [],
setup_requires = ["pytest-runner"] if "test" in sys.argv or "pytest" in sys.argv else [],
tests_require = ["pytest"],
version = __version__,
python_requires = ">= 3.5",
author = "Ben Kurtovic",
author_email = "[email protected]",
url = "https://github.com/earwig/mwparserfromhell",
description = "MWParserFromHell is a parser for MediaWiki wikicode.",
long_description = long_docs,
download_url = "https://github.com/earwig/mwparserfromhell/tarball/v{}".format(__version__),
keywords = "earwig mwparserfromhell wikipedia wiki mediawiki wikicode template parsing",
license = "MIT License",
classifiers = [
name="mwparserfromhell",
packages=find_packages("src"),
package_dir={"": "src"},
ext_modules=[tokenizer] if use_extension else [],
setup_requires=["pytest-runner"]
if "test" in sys.argv or "pytest" in sys.argv
else [],
tests_require=["pytest"],
version=__version__,
python_requires=">= 3.5",
author="Ben Kurtovic",
author_email="[email protected]",
url="https://github.com/earwig/mwparserfromhell",
description="MWParserFromHell is a parser for MediaWiki wikicode.",
long_description=long_docs,
download_url="https://github.com/earwig/mwparserfromhell/tarball/v{}".format(
__version__
),
keywords="earwig mwparserfromhell wikipedia wiki mediawiki wikicode template parsing",
license="MIT License",
classifiers=[
"Development Status :: 4 - Beta",
"Environment :: Console",
"Intended Audience :: Developers",
Expand All @@ -101,6 +110,6 @@ def build_ext_patched(self):
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Topic :: Text Processing :: Markup"
"Topic :: Text Processing :: Markup",
],
)
3 changes: 1 addition & 2 deletions src/mwparserfromhell/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
__version__ = "0.7.dev0"
__email__ = "[email protected]"

from . import (definitions, nodes, parser, smart_list, string_mixin,
utils, wikicode)
from . import definitions, nodes, parser, smart_list, string_mixin, utils, wikicode

parse = utils.parse_anything
20 changes: 16 additions & 4 deletions src/mwparserfromhell/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@
- mwparserfromhell/parser/ctokenizer/definitions.h
"""

__all__ = ["get_html_tag", "is_parsable", "is_visible", "is_single",
"is_single_only", "is_scheme"]
__all__ = [
"get_html_tag",
"is_parsable",
"is_visible",
"is_single",
"is_single_only",
"is_scheme",
]

URI_SCHEMES = {
# [wikimedia/mediawiki.git]/includes/DefaultSettings.php @ 5c660de5d0
Expand Down Expand Up @@ -92,7 +98,7 @@
"score",
"section",
"templatedata",
"timeline"
"timeline",
]

# [wikimedia/mediawiki.git]/includes/parser/Sanitizer.php @ 95e17ee645
Expand All @@ -103,29 +109,35 @@
"#": "li",
"*": "li",
";": "dt",
":": "dd"
":": "dd",
}


def get_html_tag(markup):
"""Return the HTML tag associated with the given wiki-markup."""
return MARKUP_TO_HTML[markup]


def is_parsable(tag):
"""Return if the given *tag*'s contents should be passed to the parser."""
return tag.lower() not in PARSER_BLACKLIST


def is_visible(tag):
"""Return whether or not the given *tag* contains visible text."""
return tag.lower() not in INVISIBLE_TAGS


def is_single(tag):
"""Return whether or not the given *tag* can exist without a close tag."""
return tag.lower() in SINGLE


def is_single_only(tag):
"""Return whether or not the given *tag* must exist without a close tag."""
return tag.lower() in SINGLE_ONLY


def is_scheme(scheme, slashes=True):
"""Return whether *scheme* is valid for external links."""
scheme = scheme.lower()
Expand Down
14 changes: 12 additions & 2 deletions src/mwparserfromhell/nodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,15 @@
from .template import Template
from .wikilink import Wikilink

__all__ = ["Argument", "Comment", "ExternalLink", "HTMLEntity", "Heading",
"Node", "Tag", "Template", "Text", "Wikilink"]
__all__ = [
"Argument",
"Comment",
"ExternalLink",
"HTMLEntity",
"Heading",
"Node",
"Tag",
"Template",
"Text",
"Wikilink",
]
2 changes: 2 additions & 0 deletions src/mwparserfromhell/nodes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

__all__ = ["Node"]


class Node(StringMixIn):
"""Represents the base Node type, demonstrating the methods to override.
Expand All @@ -35,6 +36,7 @@ class Node(StringMixIn):
:meth:`__showtree__` can be overridden to build a nice tree representation
of the node, if desired, for :meth:`~.Wikicode.get_tree`.
"""

def __str__(self):
raise NotImplementedError()

Expand Down
1 change: 1 addition & 0 deletions src/mwparserfromhell/nodes/argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

__all__ = ["Argument"]


class Argument(Node):
"""Represents a template argument substitution, like ``{{{foo}}}``."""

Expand Down
1 change: 1 addition & 0 deletions src/mwparserfromhell/nodes/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

__all__ = ["Comment"]


class Comment(Node):
"""Represents a hidden HTML comment, like ``<!-- foobar -->``."""

Expand Down
2 changes: 2 additions & 0 deletions src/mwparserfromhell/nodes/external_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

__all__ = ["ExternalLink"]


class ExternalLink(Node):
"""Represents an external link, like ``[http://example.com/ Example]``."""

Expand Down Expand Up @@ -83,6 +84,7 @@ def brackets(self):
def url(self, value):
# pylint: disable=import-outside-toplevel
from ..parser import contexts

self._url = parse_anything(value, contexts.EXT_LINK_URI)

@title.setter
Expand Down
12 changes: 10 additions & 2 deletions src/mwparserfromhell/nodes/extras/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

__all__ = ["Attribute"]


class Attribute(StringMixIn):
"""Represents an attribute of an HTML tag.
Expand All @@ -32,8 +33,15 @@ class Attribute(StringMixIn):
whose value is ``"foo"``.
"""

def __init__(self, name, value=None, quotes='"', pad_first=" ",
pad_before_eq="", pad_after_eq=""):
def __init__(
self,
name,
value=None,
quotes='"',
pad_first=" ",
pad_before_eq="",
pad_after_eq="",
):
super().__init__()
self.name = name
self._quotes = None
Expand Down
4 changes: 2 additions & 2 deletions src/mwparserfromhell/nodes/extras/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

__all__ = ["Parameter"]


class Parameter(StringMixIn):
"""Represents a paramater of a template.
Expand Down Expand Up @@ -77,6 +78,5 @@ def value(self, newval):
def showkey(self, newval):
newval = bool(newval)
if not newval and not self.can_hide_key(self.name):
raise ValueError("parameter key {!r} cannot be hidden".format(
self.name))
raise ValueError("parameter key {!r} cannot be hidden".format(self.name))
self._showkey = newval
1 change: 1 addition & 0 deletions src/mwparserfromhell/nodes/heading.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

__all__ = ["Heading"]


class Heading(Node):
"""Represents a section heading in wikicode, like ``== Foo ==``."""

Expand Down
17 changes: 12 additions & 5 deletions src/mwparserfromhell/nodes/html_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

__all__ = ["HTMLEntity"]


class HTMLEntity(Node):
"""Represents an HTML entity, like ``&nbsp;``, either named or unnamed."""

Expand Down Expand Up @@ -101,19 +102,23 @@ def value(self, newval):
except ValueError:
if newval not in htmlentities.entitydefs:
raise ValueError(
"entity value {!r} is not a valid name".format(newval)) from None
"entity value {!r} is not a valid name".format(newval)
) from None
self._named = True
self._hexadecimal = False
else:
if intval < 0 or intval > 0x10FFFF:
raise ValueError(
"entity value 0x{:x} is not in range(0x110000)".format(intval)) from None
"entity value 0x{:x} is not in range(0x110000)".format(intval)
) from None
self._named = False
self._hexadecimal = True
else:
test = int(newval, 16 if self.hexadecimal else 10)
if test < 0 or test > 0x10FFFF:
raise ValueError("entity value {} is not in range(0x110000)".format(test))
raise ValueError(
"entity value {} is not in range(0x110000)".format(test)
)
self._named = False
self._value = newval

Expand All @@ -126,8 +131,10 @@ def named(self, newval):
try:
int(self.value, 16)
except ValueError as exc:
raise ValueError("current entity value {!r} is not a valid "
"Unicode codepoint".format(self.value)) from exc
raise ValueError(
"current entity value {!r} is not a valid "
"Unicode codepoint".format(self.value)
) from exc
self._named = newval

@hexadecimal.setter
Expand Down
Loading

0 comments on commit 8cd0bdb

Please sign in to comment.