Skip to content

Commit

Permalink
chore: use ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
5j9 committed Nov 3, 2023
1 parent f48facb commit 2108fdf
Show file tree
Hide file tree
Showing 38 changed files with 691 additions and 568 deletions.
36 changes: 22 additions & 14 deletions dev/html_tag_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@


def parse(text: str) -> bytes:
return loads(post('https://www.mediawiki.org/w/api.php', data={
'action': 'parse',
'text': text,
'title': 'Test',
'format': 'json',
'formatversion': 2,
'prop': 'text',
}).content)['parse']['text']
return loads(
post(
'https://www.mediawiki.org/w/api.php',
data={
'action': 'parse',
'text': text,
'title': 'Test',
'format': 'json',
'formatversion': 2,
'prop': 'text',
},
).content
)['parse']['text']


content = get( # HTML elements reference
'https://developer.mozilla.org/en-US/docs/Web/HTML/Element').content
soup = BeautifulSoup(content, features="lxml")
'https://developer.mozilla.org/en-US/docs/Web/HTML/Element'
).content
soup = BeautifulSoup(content, features='lxml')
tds = soup.select('td a code')
counts = Counter(td.text.strip()[1:-1] for td in tds)
names = set(counts)
Expand All @@ -33,8 +39,9 @@ def parse(text: str) -> bytes:
self_ending_wikitext = ('#<{}\n/>\n' * names_len).format(*names)
start_only_wikitext = ('#<{}\n>\n' * names_len).format(*names)
end_only_wikitext = ('#</{}\n>\n' * names_len).format(*names)
start_and_end_wikitext = (
'#<{}\n/></{}\n>\n' * names_len).format(*chain(*zip(names, names)))
start_and_end_wikitext = ('#<{}\n/></{}\n>\n' * names_len).format(
*chain(*zip(names, names))
)

# https://www.mediawiki.org/wiki/API:Parsing_wikitext#Example_2:_Parse_a_section_of_a_page_and_fetch_its_table_data
self_ending_html = parse(self_ending_wikitext)
Expand All @@ -59,7 +66,9 @@ def parse(text: str) -> bytes:
assert valid_start_and_end_names == valid_self_ending_names

assert valid_self_ending_names - valid_start_only_names == {
'section', 'source'} # note that both of them are extension tags
'section',
'source',
} # note that both of them are extension tags

# len(valid_start_only_names) == 59
print(valid_start_only_names)
Expand All @@ -73,4 +82,3 @@ def parse(text: str) -> bytes:
# 'span', 'em', 'sup', 'div', 'h5', 'ol', 'bdi', 'kbd', 'dt', 'p', 'caption',
# 'samp', 'strike', 'small', 'dl', 'i', 'tr'}
# to be used in _config.py

15 changes: 7 additions & 8 deletions dev/profiles/vs_mwpfh.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,31 @@
#
# print('len(text):', len(text))

repeat = partial(
repeat,
number=1,
repeat=10000,
globals=globals())
repeat = partial(repeat, number=1, repeat=10000, globals=globals())


def print_min(marker, statement):
print(marker, min(repeat(statement)))


# Note that if the parse time is included, then wtp will be faster than mwp
p1 = wtp.parse("{{t|a|b|c|d}}")
p1 = wtp.parse('{{t|a|b|c|d}}')
print_min(
'wtp,arg_val_assign',
'p1.templates[0].arguments',
) # 1.96000000000085e-05

p2 = mwp.parse("{{t|a|b|c|d}}")
p2 = mwp.parse('{{t|a|b|c|d}}')
print_min(
'mwp,arg_val_assign',
'p2.filter_templates()[0].params',
) # 9.199999999986996e-06


assert p2.filter_templates()[0].params[3].name == p1.templates[0].arguments[3].name
assert (
p2.filter_templates()[0].params[3].name
== p1.templates[0].arguments[3].name
)

profiler = cProfile.Profile()

Expand Down
37 changes: 22 additions & 15 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.doctest',
'sphinx.ext.viewcode']
'sphinx.ext.viewcode',
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Expand Down Expand Up @@ -68,7 +69,7 @@
open(
join(repo_dir, 'wikitextparser', '__init__.py'), encoding='utf8'
).read(),
MULTILINE
MULTILINE,
).group(1)
# The full version, including alpha/beta/rc tags.
release = version
Expand Down Expand Up @@ -134,15 +135,12 @@
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',

# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
Expand All @@ -151,20 +149,23 @@
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [(
master_doc, 'wikitextparser.tex', 'wikitextparser Documentation',
'', # author_texescaped_str
'manual',
)]
latex_documents = [
(
master_doc,
'wikitextparser.tex',
'wikitextparser Documentation',
'', # author_texescaped_str
'manual',
)
]


# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'wikitextparser', 'wikitextparser Documentation',
[author], 1)
(master_doc, 'wikitextparser', 'wikitextparser Documentation', [author], 1)
]


Expand All @@ -174,9 +175,15 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'wikitextparser', 'wikitextparser Documentation',
author, 'wikitextparser', 'One line description of project.',
'Miscellaneous'),
(
master_doc,
'wikitextparser',
'wikitextparser Documentation',
author,
'wikitextparser',
'One line description of project.',
'Miscellaneous',
),
]

# If true, “(C) Copyright …” is shown in the HTML footer. Default is True.
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespaces = false
[tool.setuptools.dynamic.version]
attr = "wikitextparser.__version__"

[tool.isort]
profile = "black"
line_length = 79
combine_as_imports = true
[tool.ruff]
format.quote-style = 'single'
line-length = 79
isort.combine-as-imports = true
10 changes: 5 additions & 5 deletions tests/test_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def test_value_after_convertion_of_positional_to_keywordk():


def test_name_of_positionals():
assert \
['1', '2', '3'] == \
[a.name for a in parse('{{t|a|b|c}}').templates[0].arguments]
assert ['1', '2', '3'] == [
a.name for a in parse('{{t|a|b|c}}').templates[0].arguments
]


def test_dont_confuse_subspan_equal_with_keyword_arg_equal():
Expand All @@ -78,7 +78,7 @@ def test_dont_confuse_subspan_equal_with_keyword_arg_equal():


def test_setting_positionality():
a = Argument("|1=v")
a = Argument('|1=v')
a.positional = False
assert '|1=v' == a.string
a.positional = True
Expand All @@ -103,7 +103,7 @@ def test_section_not_keyword_arg():
assert (a.name, a.value) == ('', '=1==\n')
# Todo: Prevents forming a template!
# a = Argument('|\n==1==')
# assert
# assert
# (a.name == a.value), ('1', '\n==1==')


Expand Down
4 changes: 2 additions & 2 deletions tests/test_cell.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from wikitextparser import parse
from wikitextparser import parse

# noinspection PyProtectedMember
from wikitextparser._table import Cell, Table
Expand All @@ -7,7 +7,7 @@
def test_value():
c = Cell('\n| a ')
assert ' a ' == c.value
assert repr(c) == 'Cell(\'\\n| a \')'
assert repr(c) == "Cell('\\n| a ')"
assert c.attrs == {}
# Use _cached_attrs
assert c.attrs == {}
Expand Down
11 changes: 8 additions & 3 deletions tests/test_comment_bold_italic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from wikitextparser import Bold, Comment, Italic
from wikitextparser import Bold, Comment, Italic


def test_comment():
Expand All @@ -16,7 +16,7 @@ def test_bold():
def test_italic():
assert Italic("''i").text == 'i'
assert Italic("'''''i'''''").text == "'''i'''"
assert Italic("''i<!---->'<!---->'").text == "i<!---->"
assert Italic("''i<!---->'<!---->'").text == 'i<!---->'
assert Italic("''i'''").text == "i'"
# searching "''' ''i'''" for italics gives "''i'''", but it has not end
assert Italic("''i'''", end_token=False).text == "i'''"
Expand Down Expand Up @@ -47,7 +47,12 @@ def test_sub_italics():


def test_ref_text(): # 61
assert Bold("'''<ref>\n</ref>", ).text == '<ref>\n</ref>'
assert (
Bold(
"'''<ref>\n</ref>",
).text
== '<ref>\n</ref>'
)


def test_close_only_comment():
Expand Down
11 changes: 7 additions & 4 deletions tests/test_externallink.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
from wikitextparser import ExternalLink
from wikitextparser import ExternalLink


def test_externallinks():
assert ExternalLink('http://example.org').external_links == []


def test_repr():
assert repr(ExternalLink('HTTP://mediawiki.org')) == \
"ExternalLink('HTTP://mediawiki.org')"
assert (
repr(ExternalLink('HTTP://mediawiki.org'))
== "ExternalLink('HTTP://mediawiki.org')"
)


def test_numberedmailto_change_none_to_empty():
s = (
'[mailto:'
'[email protected]?Subject=URL%20Encoded%20Subject&body='
'Body%20Textinfo]')
'Body%20Textinfo]'
)
el = ExternalLink(s)
assert s[1:-1] == el.url
assert el.text is None
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parameter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pytest import warns
from pytest import warns

from wikitextparser import Parameter

Expand Down
24 changes: 13 additions & 11 deletions tests/test_parser_function.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pytest import mark
from pytest import mark

from wikitextparser import ParserFunction, WikiText

Expand All @@ -7,25 +7,27 @@


def test_parser_function():
assert repr(ParserFunction('{{#if:a|{{#if:b|c}}}}').parser_functions[0]) \
assert (
repr(ParserFunction('{{#if:a|{{#if:b|c}}}}').parser_functions[0])
== "ParserFunction('{{#if:b|c}}')"
)


def test_args_containing_braces():
assert 4 == len(ParserFunction('{{#pf:\n{|2\n|3\n|}\n}}').arguments)


def test_repr():
assert repr(ParserFunction('{{#if:a|b}}')) ==\
"ParserFunction('{{#if:a|b}}')"
assert (
repr(ParserFunction('{{#if:a|b}}')) == "ParserFunction('{{#if:a|b}}')"
)


def test_name_and_args():
f = ParserFunction('{{ #if: test | true | false }}')
assert ' #if' == f.name
args = f.arguments
assert [': test ', '| true ', '| false '] ==\
[a.string for a in args]
assert [': test ', '| true ', '| false '] == [a.string for a in args]
assert args[0].name == '1'
assert args[2].name == '3'

Expand All @@ -46,12 +48,12 @@ def test_pipes_inside_params_or_templates():


def test_strip_empty_wikilink():
pf = ParserFunction("{{ #if: test | [[|Alt]] }}")
pf = ParserFunction('{{ #if: test | [[|Alt]] }}')
assert 2 == len(pf.arguments)


def test_default_parser_function_without_hash_sign():
assert 1 == len(WikiText("{{formatnum:text|R}}").parser_functions)
assert 1 == len(WikiText('{{formatnum:text|R}}').parser_functions)


@mark.xfail
Expand All @@ -60,15 +62,15 @@ def test_parser_function_alias_without_hash_sign():
See: //translatewiki.net/wiki/MediaWiki:Sp-translate-data-MagicWords/fa
"""
assert 1 == len(WikiText("{{آرایش‌عدد:text|R}}").parser_functions)
assert 1 == len(WikiText('{{آرایش‌عدد:text|R}}').parser_functions)


def test_argument_with_existing_span():
"""Test when the span is already in type_to_spans."""
pf = WikiText("{{formatnum:text}}").parser_functions[0]
pf = WikiText('{{formatnum:text}}').parser_functions[0]
assert pf.arguments[0].value == 'text'
assert pf.arguments[0].value == 'text'


def test_tag_containing_pipe():
assert len(ParserFunction("{{text|a<s |>b</s>c}}").arguments) == 1
assert len(ParserFunction('{{text|a<s |>b</s>c}}').arguments) == 1
2 changes: 1 addition & 1 deletion tests/test_section.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pytest import mark
from pytest import mark

from wikitextparser import Section

Expand Down
Loading

0 comments on commit 2108fdf

Please sign in to comment.