From ebb71af52239516a72adcfc74d48a8874ed1b13d Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 26 Aug 2021 13:51:08 +0200 Subject: lint fixes --- .pylintrc | 1 + wikimini/__init__.py | 28 ++++++++++++++++++---------- wikimini/document.py | 4 ++++ wikimini/templates/__init__.py | 4 +++- wikimini/templates/cite.py | 2 +- wikimini/templates/convert.py | 2 +- wikimini/templates/language.py | 6 +++--- wikimini/templates/various.py | 2 +- 8 files changed, 32 insertions(+), 17 deletions(-) diff --git a/.pylintrc b/.pylintrc index 43b6bca..5ba140a 100644 --- a/.pylintrc +++ b/.pylintrc @@ -154,6 +154,7 @@ disable=print-statement, exception-escape, no-else-return, consider-using-enumerate, + redefined-outer-name, comprehension-escape # Enable the message, report, category or checker with the given id(s). You can diff --git a/wikimini/__init__.py b/wikimini/__init__.py index f927a94..d56aff0 100644 --- a/wikimini/__init__.py +++ b/wikimini/__init__.py @@ -1,17 +1,26 @@ +"""Wikimini is a module that allows you to retrieve and parse Wikipedia +articles to then output them in a different (text) format. + +For the main API, see the wikimini.Wikimini class +For support for Mediawiki templates, see wikimini.templates +For output formatting, see wikimini.formats +For the internal document representation, see wikimini.dcument +""" import re +from typing import List, Union, Tuple, Sequence, Iterable, cast + import mwparserfromhell as mwp import requests from tabulate import tabulate -from typing import List, Union, Tuple, Sequence, Iterable, cast - from .document import ( Plain, BlockLink, InlineLink, Verbatim, Document, Node, Block, ItemList, Paragraph, Heading, insert_into, extract_plaintext, ) + #: The default API URL, pointing to the english Wikipedia. API_URL = "https://en.wikipedia.org/w/api.php" @@ -43,10 +52,12 @@ class Wikimini: self.api_url = api_url self.table_format = table_format if template_registry is None: + # pylint: disable=cyclic-import,import-outside-toplevel + from . import templates template_registry = templates.registry self.template_registry = template_registry - def page_url(self, title: str) -> str: + def page_url(self, title: str) -> str: # pylint: disable=no-self-use """Returns the link for page with the given title. By default, this only replaces the special characters (like the space). @@ -123,6 +134,7 @@ class Wikimini: Returns: The converted object. """ + # pylint: disable=too-many-branches,too-many-return-statements default = lambda obj:\ [Plain(mwp.wikicode.Wikicode([obj]).strip_code(collapse=False))] @@ -147,9 +159,9 @@ class Wikimini: document.extend(cast(Iterable[Block], current)) document.append(Paragraph([])) elif isinstance(current[0], Node): - for c in current: - assert isinstance(c, Node) - insert_into(document, c) + for converted_node in current: + assert isinstance(converted_node, Node) + insert_into(document, converted_node) return document elif isinstance(obj, mwp.nodes.heading.Heading): return [Heading(obj.level, obj.title.strip_code())] @@ -253,7 +265,3 @@ class Wikimini: document.blocks = self.convert(obj) document.cleanup() return document - - -# import at the bottom to avoid circular dependencies -from . import templates # pylint: disable=wrong-import-position diff --git a/wikimini/document.py b/wikimini/document.py index 8c30bf6..bbab901 100644 --- a/wikimini/document.py +++ b/wikimini/document.py @@ -266,6 +266,8 @@ class Paragraph(Block): def _find_index(self, idx): offset = 0 + i = None + node = None for i, node in enumerate(self.nodes): if idx < offset + len(node): return (i, idx - offset) @@ -541,3 +543,5 @@ def extract_plaintext(obj) -> str: return "".join(node.plain() for node in obj) elif isinstance(obj[0], Block): return "\n\n".join(block.plain() for block in obj) + else: + raise TypeError(f"Cannot extract plaintext from {obj}") diff --git a/wikimini/templates/__init__.py b/wikimini/templates/__init__.py index 6614936..e3192ef 100644 --- a/wikimini/templates/__init__.py +++ b/wikimini/templates/__init__.py @@ -117,6 +117,8 @@ class Registry: registry = Registry() -from . import ( # pylint: disable=wrong-import-position +# Import at the bottom to avoid cyclic import issues. The import is only needed +# to trigger all the registry.insert() calls. +from . import ( # pylint: disable=wrong-import-position, cyclic-import convert, mainlinks, quotes, various, cite, language ) diff --git a/wikimini/templates/cite.py b/wikimini/templates/cite.py index e342a5f..2a56f05 100644 --- a/wikimini/templates/cite.py +++ b/wikimini/templates/cite.py @@ -3,7 +3,7 @@ from . import registry from ..document import Plain -def tmpl_citation(wikimini, obj): +def tmpl_citation(_wikimini, obj): """Renders the ``{{citation|...}}`` template.""" title = obj.get("title", None) if title: diff --git a/wikimini/templates/convert.py b/wikimini/templates/convert.py index 8bab782..2712555 100644 --- a/wikimini/templates/convert.py +++ b/wikimini/templates/convert.py @@ -3,7 +3,7 @@ from . import registry from ..document import Plain -def tmpl_convert(wikimini, obj): +def tmpl_convert(_wikimini, obj): """Renders the ``{{convert|...}}`` template.""" if str(obj.params[1]) in {"-", "to"}: return [Plain("{0}{3} {1} {2}{3}".format( diff --git a/wikimini/templates/language.py b/wikimini/templates/language.py index 7579c34..06efc87 100644 --- a/wikimini/templates/language.py +++ b/wikimini/templates/language.py @@ -1,9 +1,9 @@ """Language related templates.""" +import pycountry + from . import registry from ..document import Plain -import pycountry - def tmpl_ipa(wikimini, obj): """Renders the ``{{IPA|...}}`` template.""" @@ -56,7 +56,7 @@ for language in pycountry.languages: def tmpl_country_flag(country): """Creates a template renderer for ``{{BRA}}`` country flags.""" - def inner(wikimini, obj): + def inner(_wikimini, _obj): return [Plain(country)] return inner diff --git a/wikimini/templates/various.py b/wikimini/templates/various.py index 6076ac4..c3daca4 100644 --- a/wikimini/templates/various.py +++ b/wikimini/templates/various.py @@ -3,7 +3,7 @@ from . import registry from ..document import Plain -def tmpl_reign(wikimini, obj): +def tmpl_reign(_wikimini, obj): """Renders the ``{{reign|...}}`` template.""" if not obj.params: return [Plain("r. ")] -- cgit v1.2.3