diff --git a/.gitignore b/.gitignore index 58555148..25b5791e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,4 @@ docs/_build/ .vscode /temp/ docs/dev/reference/ -.flakeheaven_cache/ +.ruff_cache/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 98258e52..11b32b25 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/mirrors-prettier - rev: v3.0.1 + rev: v3.0.3 hooks: - id: prettier types_or: @@ -25,12 +25,6 @@ repos: name: check-hooks-apply - id: check-useless-excludes name: check-useless-excludes - - repo: https://github.com/asottile/pyupgrade - rev: v3.10.1 - hooks: - - id: pyupgrade - args: - - --py37-plus - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 hooks: @@ -39,74 +33,33 @@ repos: exclude: tests/test_unit/(test_po2md/translate-examples/code-blocks.*|test_event\.py) - id: end-of-file-fixer name: end-of-file-fixer - - id: double-quote-string-fixer - name: double-quote-string-fixer - - id: debug-statements - name: debug-statements - repo: https://github.com/Lucas-C/pre-commit-hooks rev: v1.5.1 hooks: - id: remove-crlf files: \.bat$ name: "*.bat end of lines" - - repo: https://github.com/asottile/add-trailing-comma - rev: v3.0.1 - hooks: - - id: add-trailing-comma - name: add-trailing-comma - - repo: https://github.com/flakeheaven/flakeheaven - rev: 3.3.0 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.0.292 hooks: - - id: flakeheaven - additional_dependencies: - - flake8-builtins - - flake8-comprehensions - - flake8-docstrings - - flake8-executable - - flake8-implicit-str-concat - - flake8-print - - flake8-printf-formatting - - flake8-pytest-style - - flake8-bugbear - - flake8-encodings - - flake8-no-pep420 - - flake8-absolute-import - - flake8-slots - - flake8-unused-arguments - - dlint - - pysetenv - entry: pysetenv FLAKEHEAVEN_CACHE_TIMEOUT=0 flakeheaven lint + - id: ruff + args: + - --fix + - --exit-non-zero-on-fix - repo: https://github.com/pre-commit/mirrors-autopep8 - rev: v2.0.2 + rev: v2.0.4 hooks: - id: autopep8 - - repo: https://github.com/PyCQA/isort - rev: 5.12.0 - hooks: - - id: isort - repo: https://github.com/adrienverge/yamllint rev: v1.32.0 hooks: - id: yamllint - args: - - -c - - .yamllint.yaml - repo: https://github.com/editorconfig-checker/editorconfig-checker.python rev: 2.7.2 hooks: - id: editorconfig-checker name: editorconfig-checker alias: ec - - repo: https://github.com/myint/autoflake - rev: v2.2.0 - hooks: - - id: autoflake - args: - - --in-place - - --remove-all-unused-imports - - --remove-unused-variables - - --remove-duplicate-keys - - --ignore-init-module-imports - repo: https://github.com/tcort/markdown-link-check rev: v3.11.2 hooks: @@ -114,7 +67,7 @@ repos: name: markdown-link-check files: ^README\.md$ - repo: https://github.com/DavidAnson/markdownlint-cli2 - rev: v0.8.1 + rev: v0.10.0 hooks: - id: markdownlint-cli2 name: markdownlint-po2md-tests diff --git a/docs/conf.py b/docs/conf.py index d33ccffb..378cf313 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -27,7 +27,7 @@ ' = ', )[1].strip().strip('"').strip("'") elif line.startswith('authors ='): - metadata['author'] = re.search("name = \"([^\"]+)\"", line).group(1) + metadata['author'] = re.search('name = \"([^\"]+)\"', line).group(1) elif not line: break diff --git a/pyproject.toml b/pyproject.toml index 25b92f76..7cd20c67 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -109,12 +109,9 @@ targets = [{ file = "pyproject.toml" }, { file = "docs/pre-commit-hooks.rst" }] [tool.project-config] cache = "2 days" style = [ - "gh://mondeja/project-config-styles@v4.4/python/base.json5", - "gh://mondeja/project-config-styles@v4.4/python/single-quotes.json5", - "gh://mondeja/project-config-styles@v4.4/python/tests.json5", - "gh://mondeja/project-config-styles@v4.4/python/sphinx.json5", - "gh://mondeja/project-config-styles@v4.4/python/readthedocs.json5", - "gh://mondeja/project-config-styles@v4.4/python/google-docstrings.json5" + "gh://mondeja/project-config-styles@v5/python/base.json5", + "gh://mondeja/project-config-styles@v5/python/sphinx.json5", + "gh://mondeja/project-config-styles@v5/python/readthedocs.json5", ] [tool.pytest.ini_options] @@ -132,81 +129,62 @@ exclude_lines = [ "if '-h' in args or '--help' in args:" ] -[tool.flakeheaven] -inline-quotes = "single" -max-line-length = 80 -pytest-fixture-no-parentheses = true -pytest-parametrize-values-type = "tuple" -docstring-convention = "google" -unused-arguments-ignore-abstract-functions = true - -[tool.flakeheaven.plugins] -pycodestyle = ["+*", "-W503"] -pyflakes = ["+*"] -pylint = ["+*"] -flake8-builtins = ["+*"] -flake8-comprehensions = ["+*"] -flake8-docstrings = ["+*", "-D107", "-D105"] -flake8-executable = ["+*"] -flake8-implicit-str-concat = ["+*"] -flake8-print = ["+*"] -flake8-printf-formatting = ["+*"] -flake8-pytest-style = ["+*"] -flake8-bugbear = ["+*"] -flake8-encodings = ["+*"] -flake8-no-pep420 = ["+*"] -flake8-absolute-import = ["+*"] -flake8-unused-arguments = ["+*"] -flake8-slots = ["+*"] -dlint = ["+*"] - -[tool.flakeheaven.exceptions."docs/**"] -flake8-print = ["-T201"] -flake8-docstrings = ["-D100", "-D101", "-D102"] -flake8-no-pep420 = ["-INP001"] - -[tool.flakeheaven.exceptions."tests/**"] -flake8-docstrings = [ - "-D100", - "-D101", - "-D102", - "-D103", - "-D104", - "-D107", - "-D205", - "-D415" +[tool.ruff] +line-length = 80 +target-version = "py37" +select = [ + "W", + "B", + "E", + "I", + "F", + "A", + "D", + "G", + "Q", + "PL", + "UP", + "PT", + "C4", + "EXE", + "ISC", + "T20", + "INP", + "ARG", + "SIM", + "RET", + "FBT", + "ERA", + "T10", + "COM", + "SLOT", ] -flake8-no-pep420 = ["-INP001"] - -[tool.flakeheaven.exceptions."setup.py"] -flake8-docstrings = ["-D205"] -flake8-no-pep420 = ["-INP001"] - -[tool.flakeheaven.exceptions."src/**/*.py"] -flake8-docstrings = ["-D101", "-D102", "-D103", "-D107"] - -[tool.flakeheaven.exceptions."src/md.py"] -flake8-docstrings = ["-D101", "-D102", "-D107"] - -[tool.isort] -lines_after_imports = 2 -multi_line_output = 3 -line_length = 79 -use_parentheses = true -combine_as_imports = true -include_trailing_comma = true -remove_redundant_aliases = true -known_tests = "tests" -sections = [ - "FUTURE", - "STDLIB", - "THIRDPARTY", - "FIRSTPARTY", - "TESTS", - "LOCALFOLDER" +ignore = [ + "PLR0911", + "PLR0912", + "PLR0915", + "PLR0913", + "FBT002", ] -py_version = 37 -extra_standard_library = [ + +[tool.ruff.pydocstyle] +convention = "google" + +[tool.ruff.flake8-quotes] +inline-quotes = "single" +multiline-quotes = "single" + +[tool.ruff.flake8-pytest-style] +fixture-parentheses = false +parametrize-values-type = "tuple" +parametrize-values-row-type = "tuple" + +[tool.ruff.isort] +lines-after-imports = 2 +combine-as-imports = true +force-wrap-aliases = true +known-local-folder = ["tests"] +extra-standard-library = [ "contextvars", "dataclasses", "importlib.resources", @@ -217,6 +195,27 @@ extra_standard_library = [ "wsgiref.types" ] +[tool.ruff.per-file-ignores] +"tests/**" = [ + "I002", + "D100", + "D101", + "D102", + "D103", + "D104", + "D107", + "D205", + "D415", + "INP001", + "PLR0913", + "PLR2004", +] +"setup.py" = ["D205", "INP001", "I002"] +"src/**/*.py" = ["D101", "D102", "D103", "D107"] +"src/md.py" = ["D101", "D102", "D107"] +"docs/conf.py" = ["INP001"] +"tests/test_unit/test_event.py" = ["E501", "ISC003"] + [build-system] requires = ["hatchling"] build-backend = "hatchling.build" diff --git a/src/mdpo/__init__.py b/src/mdpo/__init__.py index 6f0d3625..8f926827 100644 --- a/src/mdpo/__init__.py +++ b/src/mdpo/__init__.py @@ -1,5 +1,6 @@ """mdpo package.""" + func_package_map = { 'markdown_pofile_to_html': 'mdpo2html', 'markdown_to_pofile': 'md2po', @@ -7,7 +8,7 @@ 'pofile_to_markdown': 'po2md', } -__all__ = list(func_package_map.keys()) +__all__ = list(func_package_map) # noqa: PLE0605 (not implemented by Ruff) def __getattr__(name): @@ -21,7 +22,7 @@ def __getattr__(name): ) except KeyError: raise ImportError( - f'cannot import name \'{name}\' from \'mdpo\' ({__file__})', + f"cannot import name '{name}' from 'mdpo' ({__file__})", name=name, path='mdpo', ) from None diff --git a/src/mdpo/cli.py b/src/mdpo/cli.py index 25395d0d..8555d5c5 100644 --- a/src/mdpo/cli.py +++ b/src/mdpo/cli.py @@ -14,7 +14,7 @@ CLOSE_QUOTE_CHAR = '”' if SPHINX_IS_RUNNING else '"' -def cli_codespan(value, cli=True, sphinx=True): +def cli_codespan(value, cli=True): """Command line codespan wrapper. This is a compatibility function to make CLI codespans looks good in @@ -25,12 +25,12 @@ def cli_codespan(value, cli=True, sphinx=True): Args: value (str): Value to wrap. cli (bool): Wrap when used from command line. - sphinx (bool): Wrap when used from Sphinx. """ if SPHINX_IS_RUNNING: - return f'``{value}``' if sphinx else value - else: - return f'\'{value}\'' if cli else value + return f'``{value}``' + if cli: # pragma: no cover + return f"'{value}'" + return value def parse_escaped_pairs_cli_argument( @@ -213,7 +213,7 @@ def add_nolocation_option(parser): """ parser.add_argument( '--no-location', '--nolocation', dest='location', action='store_false', - help='Do not write \'#: filename:line\' lines. Note that using this' + help="Do not write '#: filename:line' lines. Note that using this" ' option makes it harder for technically skilled translators to' ' understand the context of each message. Same as' f' {cli_codespan("gettext --no-location")}.', @@ -246,8 +246,8 @@ def add_encoding_arguments( po_encoding_help = ( 'PO files encoding. If you need different encodings for each' ' file, you must define them in the "Content-Type" field of each' - ' PO file metadata, in the form \'Content-Type: text/plain;' - ' charset=\'.' + " PO file metadata, in the form 'Content-Type: text/plain;" + " charset='." ) if po_encoding_help is None else po_encoding_help parser.add_argument( '--po-encoding', dest='po_encoding', default=None, metavar='ENCODING', @@ -296,7 +296,7 @@ def add_wrapwidth_argument( else: to_render = 'the Markdown output, when possible' kwargs['help'] = ( - f'Maximum width rendering {to_render}. If negative, \'0\' or \'inf\',' + f"Maximum width rendering {to_render}. If negative, '0' or 'inf'," ' the content will not be wrapped.' ) parser.add_argument(*args, **kwargs) diff --git a/src/mdpo/event.py b/src/mdpo/event.py index f27d0649..3f7ca7f7 100644 --- a/src/mdpo/event.py +++ b/src/mdpo/event.py @@ -53,47 +53,59 @@ def debug(event, msg): date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') sys.stdout.write(f'{program}[DEBUG]::{date}::{event}:: {msg}\n') - def print_msgid(self, msgid, msgstr, msgctxt, tcomment, flags): - msg = f'msgid=\'{msgid}\'' + def print_msgid( + self, # noqa: ARG001 + msgid, + msgstr, + msgctxt, + tcomment, + flags, + ): + msg = f"msgid='{msgid}'" if msgstr: - msg += f' - msgstr=\'{msgstr}\'' + msg += f" - msgstr='{msgstr}'" if msgctxt: - msg += f' - msgctxt=\'{msgctxt}\'' + msg += f" - msgctxt='{msgctxt}'" if tcomment: - msg += f' - tcomment=\'{tcomment}\'' + msg += f" - tcomment='{tcomment}'" if flags: - msg += f' - flags=\'{flags}\'' + msg += f" - flags='{flags}'" debug('msgid', msg) - def print_command(self, mdpo_command, comment, original_command): + def print_command( + self, # noqa: ARG001 + mdpo_command, + comment, + original_command, + ): msg = mdpo_command if comment: msg += f' - {comment}' if original_command and original_command != mdpo_command: - msg += f' (original command: \'{original_command}\')' + msg += f" (original command: '{original_command}')" debug('command', msg) - def print_enter_block(self, block, details): + def print_enter_block(self, block, details): # noqa: ARG001 debug('enter_block', _block_msg(block, details)) - def print_leave_block(self, block, details): + def print_leave_block(self, block, details): # noqa: ARG001 debug('leave_block', _block_msg(block, details)) - def print_enter_span(self, span, details): + def print_enter_span(self, span, details): # noqa: ARG001 debug('enter_span', _block_msg(span, details)) - def print_leave_span(self, span, details): + def print_leave_span(self, span, details): # noqa: ARG001 debug('leave_span', _block_msg(span, details)) - def print_text(self, block, text): # noqa: U100 + def print_text(self, block, text): # noqa: ARG001 debug('text', text) - def print_link_reference(self, target, href, title): - msg = f'target=\'{target}\'' + def print_link_reference(self, target, href, title): # noqa: ARG001 + msg = f"target='{target}'" if href: - msg += f' - href=\'{href}\'' + msg += f" - href='{href}'" if title: - msg += f' - title=\'{title}\'' + msg += f" - title='{title}'" debug('link_reference', msg) return { diff --git a/src/mdpo/io.py b/src/mdpo/io.py index 01808475..34c2aafe 100644 --- a/src/mdpo/io.py +++ b/src/mdpo/io.py @@ -98,7 +98,6 @@ def to_files_or_content(value): return (True, value) except Exception as err: # some strings like '[s-m]' will produce - # 're.error: bad character range ... at position' if err.__module__ == 're' and err.__class__.__name__ == 'error': return (False, value) raise err @@ -112,8 +111,9 @@ def save_file_checking_file_changed(filepath, content, encoding='utf-8'): """Save a file checking if the content has changed. Args: - pofile (:py:class:`polib.POFile`): PO file to save. - po_filepath (str): Path to the new file to save in. + filepath (:py:class:`polib.POFile`): Path to the file to save. + content (str): Content to save. + encoding (str): Encoding to use when saving the file. Returns: bool: If the PO file content has been changed. diff --git a/src/mdpo/md.py b/src/mdpo/md.py index e459f5f6..033fade4 100644 --- a/src/mdpo/md.py +++ b/src/mdpo/md.py @@ -1,5 +1,6 @@ """Markdown related utilities for mdpo.""" + LINK_REFERENCE_REGEX = ( r'^\[([^\]]+)\]:\s+]+)>?\s*["\'\(]?([^"\'\)]+)?' ) @@ -90,13 +91,11 @@ def solve_link_reference_targets(translations): for msgid_linkr_group in msgid_linkr_groups: for link_reference_text_targets in link_references_text_targets: - # link_reference_text_targets[msgid][link_reference] if link_reference_text_targets[0][0] == msgid_linkr_group[1]: replacer = ( f'[{msgid_linkr_group[0]}][{msgid_linkr_group[1]}]' ) replacement = ( - # link_reference_text_targets[msgid][target] f'[{msgid_linkr_group[0]}]' f'({link_reference_text_targets[0][1]})' ) @@ -114,13 +113,11 @@ def solve_link_reference_targets(translations): for msgstr_linkr_group in msgstr_linkr_groups: for link_reference_text_targets in link_references_text_targets: - # link_reference_text_targets[msgstr][link_reference] if link_reference_text_targets[1][0] == msgstr_linkr_group[1]: replacer = ( f'[{msgstr_linkr_group[0]}][{msgstr_linkr_group[1]}]' ) replacement = ( - # link_reference_text_targets[msgstr][target] f'[{msgstr_linkr_group[0]}]' f'({link_reference_text_targets[1][1]})' ) @@ -136,10 +133,4 @@ def solve_link_reference_targets(translations): # store in solutions solutions[new_msgid] = new_msgstr - # print('----> new_msgid', new_msgid) - # print('----> new_msgstr', new_msgstr) - - # print('----> link_references_text_targets', link_references_text_targets) - # print('----> msgid_msgstrs_with_links', msgid_msgstrs_with_links) - # print('----> solutions', solutions) return solutions diff --git a/src/mdpo/md2po/__init__.py b/src/mdpo/md2po/__init__.py index b4a0381f..aa8a7934 100644 --- a/src/mdpo/md2po/__init__.py +++ b/src/mdpo/md2po/__init__.py @@ -1,5 +1,6 @@ """Markdown to PO files extractor according to mdpo specification.""" +import contextlib import glob import os @@ -448,7 +449,11 @@ def _save_msgid( self.pofile.append(entry) self.found_entries.append(entry) - def _save_current_msgid(self, msgstr='', fuzzy=False): + def _save_current_msgid( + self, + msgstr='', + fuzzy=False, + ): # raise 'msgid' event if raise_skip_event( self.events, @@ -527,14 +532,14 @@ def command(self, mdpo_command, comment, original_command): raise ValueError( 'You need to specify a string for the' ' extracted comment with the command' - f' \'{original_command}\'.', + f" '{original_command}'.", ) self.current_tcomment = comment elif mdpo_command == 'mdpo-context': if not comment: raise ValueError( 'You need to specify a string for the' - f' context with the command \'{original_command}\'.', + f" context with the command '{original_command}'.", ) self.current_msgctxt = comment elif mdpo_command == 'mdpo-include': @@ -542,7 +547,7 @@ def command(self, mdpo_command, comment, original_command): raise ValueError( 'You need to specify a message for the' ' comment to include with the command' - f' \'{original_command}\'.', + f" '{original_command}'.", ) self.current_msgid = comment self._save_current_msgid() @@ -634,14 +639,13 @@ def enter_block(self, block, details): ]): self._current_top_level_block_number += 1 self._current_top_level_block_type = md4c.BlockType.HTML.value - elif block is md4c.BlockType.TABLE: - if not any([ - self._quoteblocks_deep, - self._inside_olblock, - self._uls_deep, - ]): - self._current_top_level_block_number += 1 - self._current_top_level_block_type = md4c.BlockType.TABLE.value + elif block is md4c.BlockType.TABLE and not any([ + self._quoteblocks_deep, + self._inside_olblock, + self._uls_deep, + ]): + self._current_top_level_block_number += 1 + self._current_top_level_block_type = md4c.BlockType.TABLE.value def leave_block(self, block, details): # raise 'leave_block' event @@ -650,9 +654,10 @@ def leave_block(self, block, details): if block is md4c.BlockType.CODE: self._inside_codeblock = False - if not self.disable_next_codeblock: - if self.include_codeblocks or self.include_next_codeblock: - self._save_current_msgid() + if not self.disable_next_codeblock and ( + self.include_codeblocks or self.include_next_codeblock + ): + self._save_current_msgid() self.include_next_codeblock = False self.disable_next_codeblock = False elif block is md4c.BlockType.HTML: @@ -689,19 +694,15 @@ def not_plaintext_enter_span(self, span, details): # underline spans for double '_' character enters two times if not self._inside_uspan: if self._inside_aspan: # span inside link text - try: + with contextlib.suppress(KeyError): self._current_aspan_text += self._enterspan_replacer[ span.value ] - except KeyError: - pass else: - try: + with contextlib.suppress(KeyError): self.current_msgid += ( self._enterspan_replacer[span.value] ) - except KeyError: - pass if span is md4c.SpanType.A: # here resides the logic of discover if the current link @@ -765,19 +766,15 @@ def not_plaintext_leave_span(self, span, details): self.current_msgid += self._current_wikilink_target self._current_wikilink_target = None if self._inside_aspan: # span inside link text - try: + with contextlib.suppress(KeyError): self._current_aspan_text += self._leavespan_replacer[ span.value ] - except KeyError: - pass else: - try: + with contextlib.suppress(KeyError): self.current_msgid += ( self._leavespan_replacer[span.value] ) - except KeyError: - pass if span is md4c.SpanType.A: if self._current_aspan_ref_target: # referenced link @@ -866,7 +863,7 @@ def text(self, block, text): if self._current_imgspan: self._current_imgspan['text'] = text return - elif self._inside_codespan: + if self._inside_codespan: # fix backticks for codespan start and end to escape # internal backticks self._codespan_backticks = min_not_max_chars_in_a_row( @@ -902,10 +899,10 @@ def text(self, block, text): ) return self.current_msgid += text - else: - if not self.disable_next_codeblock: - if self.include_codeblocks or self.include_next_codeblock: - self.current_msgid += text + elif not self.disable_next_codeblock and ( + self.include_codeblocks or self.include_next_codeblock + ): + self.current_msgid += text else: self._process_command(text) @@ -1166,6 +1163,8 @@ def msgid_event(self, msgid, *args): self.disable_next_block = True debug (bool): Add events displaying all parsed elements in the extraction process. + **kwargs: Extra arguments passed to + :py:class:`mdpo.md2po.Md2Po` constructor. Examples: >>> content = 'Some text with `inline code`' diff --git a/src/mdpo/md2po2md/__init__.py b/src/mdpo/md2po2md/__init__.py index fef28de2..cb3faa02 100644 --- a/src/mdpo/md2po2md/__init__.py +++ b/src/mdpo/md2po2md/__init__.py @@ -83,16 +83,15 @@ def markdown_to_pofile_to_markdown( and err.__class__.__name__ == 'error' ): # some strings like '[s-m]' will produce - # 're.error: bad character range ... at position' raise FileNotFoundError( "The argument 'input_paths_glob' must be a valid glob or file" ' path.', - ) + ) from None raise err else: if not input_paths_glob_: raise FileNotFoundError( - f'The glob \'{input_paths_glob}\' does not match any file.', + f"The glob '{input_paths_glob}' does not match any file.", ) _saved_files_changed = None if not _check_saved_files_changed else False diff --git a/src/mdpo/md2po2md/__main__.py b/src/mdpo/md2po2md/__main__.py index 1eb9fe9b..142e6d3e 100755 --- a/src/mdpo/md2po2md/__main__.py +++ b/src/mdpo/md2po2md/__main__.py @@ -52,12 +52,12 @@ def build_parser(): ) output_paths_schema_help = '' if SPHINX_IS_RUNNING else ( - ' For example, for the schema \'locale/{lang}\', the languages' - ' \'es\' and \'fr\' and a \'README.md\' as input, the next files' - ' will be written: \'locale/es/README.po\', \'locale/es/README.md\',' - ' \'locale/fr/README.po\' and \'locale/fr/README.md\'.' - ' Note that you can omit \'{basename}\', specifying a' - ' directory for each language with \'locale/{lang}\' for this' + " For example, for the schema 'locale/{lang}', the languages" + " 'es' and 'fr' and a 'README.md' as input, the next files" + " will be written: 'locale/es/README.po', 'locale/es/README.md'," + " 'locale/fr/README.po' and 'locale/fr/README.md'." + " Note that you can omit '{basename}', specifying a" + " directory for each language with 'locale/{lang}' for this" ' example.' ) parser.add_argument( diff --git a/src/mdpo/md4c.py b/src/mdpo/md4c.py index eb5fa712..8cf44e6a 100644 --- a/src/mdpo/md4c.py +++ b/src/mdpo/md4c.py @@ -4,6 +4,8 @@ #: :list: `md4c parser `_ extensions #: used by default on :doc:`md2po ` and #: :doc:`po2md ` implementations. + + DEFAULT_MD4C_GENERIC_PARSER_EXTENSIONS = [ 'collapse_whitespace', 'tables', diff --git a/src/mdpo/mdpo2html/__init__.py b/src/mdpo/mdpo2html/__init__.py index 25b536fa..9afd3634 100644 --- a/src/mdpo/mdpo2html/__init__.py +++ b/src/mdpo/mdpo2html/__init__.py @@ -1,5 +1,6 @@ """HTML-produced-from-Markdown files translator using PO files as reference.""" +import contextlib import html import re import warnings @@ -149,7 +150,6 @@ def _remove_lastline_from_output_if_empty(self): self.output = '\n'.join(split_output)[:-1] def _process_replacer(self): - # print('REPLACER:', self.replacer) template_tags = [] raw_html_template, _current_replacement = ('', '') @@ -186,15 +186,11 @@ def _process_replacer(self): raw_html_template += f'<{handled}' - # attrs_except_href_title = [] for attr in attrs: if attr in ['title', 'href']: raw_html_template += f' {attr}="{{}}"' - # else: # These attributes are not included in output - # attrs_except_href_title.append((attr, value)) # if attrs_except_href_title: - # raw_html_template += ' ' # raw_html_template += _html_attrs_to_str( # attrs_except_href_title) + '>' raw_html_template += '>' @@ -280,11 +276,6 @@ def _process_replacer(self): if tag not in template_tags: template_tags.append(tag) - # print('RAW TEMPLATE:', raw_html_template) - # print('TEMPLATE TAGS:', template_tags) - # print(f'CURRENT MSGID: \'{_current_replacement}\'') - # print('MSGSTR:', replacement) - html_before_first_replacement = raw_html_template.split('{')[0] html_after_last_replacement = raw_html_template.split('}')[-1] for tags_group in [self.bold_tags, self.italic_tags, self.code_tags]: @@ -318,10 +309,7 @@ def _process_replacer(self): self.enable_next_block = False self.current_msgctxt = None - # print('________________________________________________') - def handle_starttag(self, tag, attrs): - # print('START TAG: %s | POS: %d:%d' % (tag, *self.getpos())) if tag in self.ignore_grouper_tags: self.context.append(tag) @@ -351,7 +339,6 @@ def handle_starttag(self, tag, attrs): self.context.append(tag) def handle_endtag(self, tag): - # print('END TAG: %s | POS: %d:%d' % (tag, *self.getpos())) if tag in self.ignore_grouper_tags: self.output += f'' @@ -370,7 +357,6 @@ def handle_endtag(self, tag): self.context.pop() def handle_startendtag(self, tag, attrs): - # print('STARTEND TAG: %s | POS: %d:%d' % (tag, *self.getpos())) if not self.replacer: self.output += self.get_starttag_text() @@ -378,7 +364,6 @@ def handle_startendtag(self, tag, attrs): self.replacer.append(('startend', tag, OrderedDict(attrs))) def handle_data(self, data): - # print(f' DATA: \'{data}\'') if data: if ( @@ -396,7 +381,6 @@ def handle_data(self, data): self.replacer.append(('data', data, None)) def handle_comment(self, data): - # print(f' COMMENT: \'{data}\'') if self.replacer: self.replacer.append(('comment', data, None)) @@ -410,10 +394,8 @@ def handle_comment(self, data): else: self._remove_lastline_from_output_if_empty() - try: + with contextlib.suppress(KeyError): # not custom command command = self.command_aliases[command] - except KeyError: # not custom command - pass if command in ( 'mdpo-disable-next-block', @@ -504,6 +486,8 @@ def markdown_pofile_to_html( instead of ````, you can pass the dictionaries ``{"mdpo-on": "mdpo-enable"}`` or ``{"mdpo-on": "enable"}`` to this parameter. + **kwargs: Extra keyword arguments passed to + :py:class:`mdpo.mdpo2html.MdPo2HTML` constructor. .. rubric:: Known limitations: diff --git a/src/mdpo/po2md/__init__.py b/src/mdpo/po2md/__init__.py index 90650140..eb1fea65 100644 --- a/src/mdpo/po2md/__init__.py +++ b/src/mdpo/po2md/__init__.py @@ -1,5 +1,7 @@ """Markdown files translator using PO files as reference.""" +import contextlib + import md4c import md_ulb_pwrap import polib @@ -420,23 +422,22 @@ def _save_current_msgid(self): else: first_line_width_diff = -3 - if not self._inside_codeblock: - # Wrap lines with Unicode Line Break algorithm - # - # Only execute it for text outside tables - if not self._current_thead_aligns: - translation = md_ulb_pwrap.ulb_wrap_paragraph( - translation, - self.wrapwidth, - self.wrapwidth + first_line_width_diff, - ) + # Wrap lines with Unicode Line Break algorithm + # + # Only execute it for text outside tables + if not self._inside_codeblock and not self._current_thead_aligns: + translation = md_ulb_pwrap.ulb_wrap_paragraph( + translation, + self.wrapwidth, + self.wrapwidth + first_line_width_diff, + ) - if not any([ - self._inside_liblock, - self._inside_hblock, - self._inside_quoteblock, - ]): - translation += '\n' + if not any([ + self._inside_liblock, + self._inside_hblock, + self._inside_quoteblock, + ]): + translation += '\n' if translation.rstrip('\n'): self.current_line += translation @@ -586,13 +587,12 @@ def leave_block(self, block, details): indent = ' ' * len(self._current_list_type) self.current_line = f'{indent}{self.current_line}' self._save_current_line() + elif self._inside_liblock_first_p: + self._inside_liblock_first_p = False else: - if self._inside_liblock_first_p: - self._inside_liblock_first_p = False - else: - indent = ' ' * len(self._current_list_type) - self.current_line = f'\n{indent}{self.current_line}' - self._save_current_line() + indent = ' ' * len(self._current_list_type) + self.current_line = f'\n{indent}{self.current_line}' + self._save_current_line() else: self._save_current_line() @@ -615,10 +615,11 @@ def leave_block(self, block, details): self.current_line += f'{indent}{fence_chars}' self._save_current_line() - if not self._inside_liblock: - # prevent two newlines after indented code block - if not self._inside_indented_codeblock: - self._save_current_line() + # prevent two newlines after indented code block + if not self._inside_liblock and ( + not self._inside_indented_codeblock + ): + self._save_current_line() self._inside_indented_codeblock = False elif block is md4c.BlockType.H: self._save_current_msgid() @@ -666,7 +667,7 @@ def leave_block(self, block, details): thead_separator += '| --- ' elif align == 1: thead_separator += '| :-- ' - elif align == 2: + elif align == 2: # noqa: PLR2004 thead_separator += '| :-: ' else: thead_separator += '| --: ' @@ -703,17 +704,13 @@ def enter_span(self, span, details): return if self._inside_aspan: # span inside link text - try: + with contextlib.suppress(KeyError): self._current_aspan_text += self._enterspan_replacer[ span.value ] - except KeyError: - pass else: - try: + with contextlib.suppress(KeyError): self.current_msgid += self._enterspan_replacer[span.value] - except KeyError: - pass if span is md4c.SpanType.A: self._inside_aspan = True @@ -769,17 +766,13 @@ def leave_span(self, span, details): self._current_wikilink_target = None if self._inside_aspan: # span inside link text - try: + with contextlib.suppress(KeyError): self._current_aspan_text += self._leavespan_replacer[ span.value ] - except KeyError: - pass else: - try: + with contextlib.suppress(KeyError): self.current_msgid += self._leavespan_replacer[span.value] - except KeyError: - pass if span is md4c.SpanType.A: if self._current_aspan_ref_target: # referenced link @@ -789,24 +782,23 @@ def leave_span(self, span, details): f'[{self._current_aspan_ref_target}]' ) self._current_aspan_ref_target = None - else: - if self._current_aspan_text == self._current_aspan_href: - # autolink vs link clash (see implementation notes) - self.current_msgid += f'<{self._current_aspan_text}' - if details['title']: - escaped_title = polib.escape(details['title'][0][1]) - self.current_msgid += f' "{escaped_title}"' - self.current_msgid += '>' - elif self._current_aspan_href: - self.current_msgid += ( - f'[{self._current_aspan_text}]' - f'({self._current_aspan_href}' - ) - if details['title']: - self._aimg_title_inside_current_msgid = True - escaped_title = polib.escape(details['title'][0][1]) - self.current_msgid += f' "{escaped_title}"' - self.current_msgid += ')' + elif self._current_aspan_text == self._current_aspan_href: + # autolink vs link clash (see implementation notes) + self.current_msgid += f'<{self._current_aspan_text}' + if details['title']: + escaped_title = polib.escape(details['title'][0][1]) + self.current_msgid += f' "{escaped_title}"' + self.current_msgid += '>' + elif self._current_aspan_href: + self.current_msgid += ( + f'[{self._current_aspan_text}]' + f'({self._current_aspan_href}' + ) + if details['title']: + self._aimg_title_inside_current_msgid = True + escaped_title = polib.escape(details['title'][0][1]) + self.current_msgid += f' "{escaped_title}"' + self.current_msgid += ')' self._current_aspan_href = None self._inside_aspan = False self._current_aspan_text = '' @@ -866,7 +858,7 @@ def text(self, block, text): if self._current_imgspan: self._current_imgspan['text'] = text return - elif self._inside_codespan: + if self._inside_codespan: self._codespan_backticks = min_not_max_chars_in_a_row( self.code_start_string, text, @@ -910,9 +902,8 @@ def text(self, block, text): if self.current_line[:len(indent) + 1] != indent: self.current_line += indent self.current_msgid += text - else: - if not self._process_command(text): - self.current_line += text + elif not self._process_command(text): + self.current_line += text def _append_link_references(self): if self.link_references: @@ -1013,7 +1004,7 @@ def pofile_to_markdown( Args: filepath_or_content (str): Markdown filepath or content to translate. - pofiles (str, list) Glob or list of globs matching a set of PO files + pofiles (str, list): Glob or list of globs matching a set of PO files from where to extract messages to make the replacements translating strings. ignore (list): Paths of PO files to ignore. Useful when a glob does not @@ -1062,6 +1053,8 @@ def pofile_to_markdown( with the syntax ``path/to/file.py::function_name``. debug (bool): Add events displaying all parsed elements in the translation process. + **kwargs: Extra arguments passed to + :py:class:`mdpo.po2md.Po2Md` constructor. Returns: str: Markdown output file with translated content. diff --git a/src/mdpo/polib.py b/src/mdpo/polib.py index e597bf0a..cacaccbe 100644 --- a/src/mdpo/polib.py +++ b/src/mdpo/polib.py @@ -26,12 +26,8 @@ def poentry__cmp__( compare_occurrences (bool): Indicates if the ``occurrences`` property of the entries will be used comparing them. """ - if compare_obsolete: - if self.obsolete != other.obsolete: - if self.obsolete: - return -1 - else: - return 1 + if compare_obsolete and self.obsolete != other.obsolete: + return -1 if self.obsolete else 1 if compare_occurrences: # `.copy` is an equivalent for full slice [:], but is faster # `.copy` doesn't exists for lists in Python2 so for this reason, @@ -46,27 +42,27 @@ def poentry__cmp__( othermsgctxt = other.msgctxt or '0' if msgctxt > othermsgctxt: return 1 - elif msgctxt < othermsgctxt: + if msgctxt < othermsgctxt: return -1 msgid_plural = self.msgid_plural or '0' othermsgid_plural = other.msgid_plural or '0' if msgid_plural > othermsgid_plural: return 1 - elif msgid_plural < othermsgid_plural: + if msgid_plural < othermsgid_plural: return -1 msgstr_plural = self.msgstr_plural or '0' othermsgstr_plural = other.msgstr_plural or '0' if msgstr_plural > othermsgstr_plural: return 1 - elif msgstr_plural < othermsgstr_plural: + if msgstr_plural < othermsgstr_plural: return -1 - elif self.msgid > other.msgid: + if self.msgid > other.msgid: return 1 - elif self.msgid < other.msgid: + if self.msgid < other.msgid: return -1 if compare_msgstr: if self.msgstr > other.msgstr: return 1 - elif self.msgstr < other.msgstr: + if self.msgstr < other.msgstr: return -1 return 0 diff --git a/src/mdpo/text.py b/src/mdpo/text.py index 8ef8a76a..d78c9c17 100644 --- a/src/mdpo/text.py +++ b/src/mdpo/text.py @@ -17,7 +17,7 @@ def and_join(values): def min_not_max_chars_in_a_row(char, text, default=1): - r"""Return the minimum possible of characters not found in a row for a string. + r"""Return minimum possible of characters not found in a row for a string. For example, given the string ``"c cc cccc"`` and the character ``"c"``, returns the minimum number of characters in a row that are not found, so @@ -31,6 +31,7 @@ def min_not_max_chars_in_a_row(char, text, default=1): Args: char (str): Character to search. text (str): Text inside which find the character repeated in a row. + default (int): Default value to return if the character is not found. Returns: int: Minimum number possible of characters not found in a row. @@ -108,8 +109,8 @@ def parse_escaped_pairs(pairs, separator=':'): for pair in pairs: try: key, value = parse_escaped_pair(pair, separator=separator) - except ValueError: - raise ValueError(pair) + except ValueError as exc: + raise ValueError(pair) from exc if key in response: raise KeyError(key) response[key] = value @@ -143,7 +144,7 @@ def parse_wrapwidth_argument(value): """ try: value = parse_strint_0_inf(value) - except ValueError: + except ValueError as exc: if os.environ.get('_MDPO_RUNNING'): # executed as CLI sys.stderr.write( f"Invalid value '{value}' for -w/--wrapwidth argument.\n", @@ -151,5 +152,5 @@ def parse_wrapwidth_argument(value): sys.exit(1) raise ValueError( f"Invalid value '{value}' for wrapwidth argument.", - ) + ) from exc return value diff --git a/tests/conftest.py b/tests/conftest.py index 83f07472..83897ece 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -85,6 +85,7 @@ def _git_init(cwd=None): ['git', 'init'], cwd=cwd, capture_output=True, + check=False, ) @@ -101,12 +102,14 @@ def _git_add_commit(message, files='.', cwd=None): ['git', 'add', files], cwd=cwd, capture_output=True, + check=False, ) commit_proc = subprocess.run( ['git', 'commit', '-m', message], cwd=cwd, capture_output=True, + check=False, ) return not any([add_proc.returncode, commit_proc.returncode]) @@ -150,15 +153,12 @@ def class_slots(): def _wrap_location_comment(filepath, rest): target_type, number, context = rest.split(' ', 2) result = f'#: {filepath}:{target_type}' - if len(result) > 77: + if len(result) + len(number) > 77: result += f'\n#: {number}' else: - if len(result) + len(number) > 77: - result += f'\n#: {number}' - else: - result += f' {number}' - if len(result) > 77: - result += '\n#:' + result += f' {number}' + if len(result) > 77: + result += '\n#:' result += f' {context}' return result diff --git a/tests/test_integration/test_pre_commit_hooks.py b/tests/test_integration/test_pre_commit_hooks.py index 2baa33aa..111ee42b 100644 --- a/tests/test_integration/test_pre_commit_hooks.py +++ b/tests/test_integration/test_pre_commit_hooks.py @@ -8,6 +8,7 @@ def pre_commit_run_all_files(cwd=None): ['pre-commit', 'run', '--all-files'], cwd=cwd, capture_output=True, + check=False, ) diff --git a/tests/test_unit/test_cli.py b/tests/test_unit/test_cli.py index 7803cbc0..42f3c7e0 100644 --- a/tests/test_unit/test_cli.py +++ b/tests/test_unit/test_cli.py @@ -1,5 +1,4 @@ import pytest - from mdpo.cli import parse_command_aliases_cli_arguments diff --git a/tests/test_unit/test_command.py b/tests/test_unit/test_command.py index ba313f28..05c62ca7 100644 --- a/tests/test_unit/test_command.py +++ b/tests/test_unit/test_command.py @@ -1,7 +1,6 @@ """HTML mdpo command utitlites tests.""" import pytest - from mdpo.command import ( get_valid_mdpo_command_names, normalize_mdpo_command, diff --git a/tests/test_unit/test_event.py b/tests/test_unit/test_event.py index a078d02e..e75c4c80 100644 --- a/tests/test_unit/test_event.py +++ b/tests/test_unit/test_event.py @@ -2,7 +2,6 @@ import io import pytest - from mdpo.event import parse_events_kwarg from mdpo.md2po import markdown_to_pofile @@ -83,7 +82,7 @@ def test_debug_event(): md2po[DEBUG]::::msgid:: msgid='' md2po[DEBUG]::::link_reference:: target='link' - href='https://foo.bar' - title='Title' md2po[DEBUG]::::msgid:: msgid='[link]: https://foo.bar "Title"' - msgstr='[link]: https://foo.bar "Title"' - flags='['fuzzy']' -''' # noqa E501 +''' assert comparable_debug_output == expected_output diff --git a/tests/test_unit/test_init.py b/tests/test_unit/test_init.py index 4fc620dc..f0c147f5 100644 --- a/tests/test_unit/test_init.py +++ b/tests/test_unit/test_init.py @@ -10,6 +10,6 @@ def test_pep562(): 'mdpo.md2po', ).markdown_to_pofile - expected_msg = 'cannot import name \'foobarbaz\' from \'mdpo\'' + expected_msg = "cannot import name 'foobarbaz' from 'mdpo'" with pytest.raises(ImportError, match=expected_msg): - mdpo.foobarbaz + mdpo.foobarbaz # noqa: B018 diff --git a/tests/test_unit/test_io.py b/tests/test_unit/test_io.py index 548d7e18..af1bd4e9 100644 --- a/tests/test_unit/test_io.py +++ b/tests/test_unit/test_io.py @@ -5,7 +5,6 @@ import os import pytest - from mdpo.io import ( filter_paths, save_file_checking_file_changed, diff --git a/tests/test_unit/test_md2po/test_commands/test_md2po_codeblocks.py b/tests/test_unit/test_md2po/test_commands/test_md2po_codeblocks.py index 51335b8c..02b3c2e4 100644 --- a/tests/test_unit/test_md2po/test_commands/test_md2po_codeblocks.py +++ b/tests/test_unit/test_md2po/test_commands/test_md2po_codeblocks.py @@ -1,5 +1,4 @@ import pytest - from mdpo.md2po import markdown_to_pofile diff --git a/tests/test_unit/test_md2po/test_commands/test_md2po_context.py b/tests/test_unit/test_md2po/test_commands/test_md2po_context.py index e1868e2f..600f0db3 100644 --- a/tests/test_unit/test_md2po/test_commands/test_md2po_context.py +++ b/tests/test_unit/test_md2po/test_commands/test_md2po_context.py @@ -1,5 +1,4 @@ import pytest - from mdpo.md2po import markdown_to_pofile @@ -40,7 +39,7 @@ def test_context(command, command_aliases): def test_context_without_value(command, command_aliases): expected_msg = ( 'You need to specify a string for the context with the command' - f' \'{command}\'.' + f" '{command}'." ) with pytest.raises(ValueError, match=expected_msg): markdown_to_pofile( diff --git a/tests/test_unit/test_md2po/test_commands/test_md2po_disable.py b/tests/test_unit/test_md2po/test_commands/test_md2po_disable.py index c58b01ed..34864762 100644 --- a/tests/test_unit/test_md2po/test_commands/test_md2po_disable.py +++ b/tests/test_unit/test_md2po/test_commands/test_md2po_disable.py @@ -1,5 +1,4 @@ import pytest - from mdpo.md2po import Md2Po, markdown_to_pofile diff --git a/tests/test_unit/test_md2po/test_commands/test_md2po_include.py b/tests/test_unit/test_md2po/test_commands/test_md2po_include.py index 7e4b6fdd..8bd3f905 100644 --- a/tests/test_unit/test_md2po/test_commands/test_md2po_include.py +++ b/tests/test_unit/test_md2po/test_commands/test_md2po_include.py @@ -1,5 +1,4 @@ import pytest - from mdpo.md2po import markdown_to_pofile @@ -40,7 +39,7 @@ def test_include_comment(command, command_aliases): def test_include_comment_without_value(command, command_aliases): expected_msg = ( 'You need to specify a message for the comment to include with the' - f' command \'{command}\'.' + f" command '{command}'." ) with pytest.raises(ValueError, match=expected_msg): markdown_to_pofile( diff --git a/tests/test_unit/test_md2po/test_commands/test_md2po_translator.py b/tests/test_unit/test_md2po/test_commands/test_md2po_translator.py index c23c1d39..a44d0e9f 100644 --- a/tests/test_unit/test_md2po/test_commands/test_md2po_translator.py +++ b/tests/test_unit/test_md2po/test_commands/test_md2po_translator.py @@ -1,5 +1,4 @@ import pytest - from mdpo.md2po import markdown_to_pofile @@ -42,7 +41,7 @@ def test_translator_command_without_value(command, command_aliases): ''' expected_msg = ( 'You need to specify a string for the extracted comment with the' - f' command \'{command}\'.' + f" command '{command}'." ) with pytest.raises(ValueError, match=expected_msg): markdown_to_pofile(content, command_aliases=command_aliases) diff --git a/tests/test_unit/test_md2po/test_extract.py b/tests/test_unit/test_md2po/test_extract.py index 4670faef..76699753 100644 --- a/tests/test_unit/test_md2po/test_extract.py +++ b/tests/test_unit/test_md2po/test_extract.py @@ -3,7 +3,6 @@ import random import pytest - from mdpo.md2po import markdown_to_pofile from mdpo.md4c import DEFAULT_MD4C_GENERIC_PARSER_EXTENSIONS diff --git a/tests/test_unit/test_md2po/test_extractor.py b/tests/test_unit/test_md2po/test_extractor.py index 16b2e337..797dbaf4 100644 --- a/tests/test_unit/test_md2po/test_extractor.py +++ b/tests/test_unit/test_md2po/test_extractor.py @@ -1,7 +1,6 @@ import os import pytest - from mdpo.md2po import Md2Po diff --git a/tests/test_unit/test_md2po/test_md2po_cli.py b/tests/test_unit/test_md2po/test_md2po_cli.py index 4ddfd484..e7eb44dd 100644 --- a/tests/test_unit/test_md2po/test_md2po_cli.py +++ b/tests/test_unit/test_md2po/test_md2po_cli.py @@ -7,7 +7,6 @@ import sys import pytest - from mdpo.compat import importlib_metadata from mdpo.md2po.__main__ import run @@ -76,6 +75,7 @@ def test_pipe_redirect_file_stdin(tmp_file): input=EXAMPLE['input'], capture_output=True, shell=True, + check=False, ) assert proc.stdout == EXAMPLE['output'] assert proc.returncode == 0 @@ -160,7 +160,7 @@ def test_debug(capsys, arg): ), line, ) - if line.endswith('msgid=\'\''): + if line.endswith("msgid=''"): assert '\n'.join([*stdout_lines[i + 1:], '']) == EXAMPLE['output'] po_output_checked = True break @@ -367,7 +367,7 @@ def test_wrapwidth(capsys, arg, value): stdout, stderr = capsys.readouterr() assert stdout == '' assert stderr == ( - 'Invalid value \'invalid\' for -w/--wrapwidth argument.\n' + "Invalid value 'invalid' for -w/--wrapwidth argument.\n" ) return diff --git a/tests/test_unit/test_md2po/test_md2po_events.py b/tests/test_unit/test_md2po/test_md2po_events.py index 0995f62c..ea1e6432 100644 --- a/tests/test_unit/test_md2po/test_md2po_events.py +++ b/tests/test_unit/test_md2po/test_md2po_events.py @@ -5,14 +5,17 @@ import md4c import pytest - from mdpo.command import normalize_mdpo_command from mdpo.md2po import Md2Po, markdown_to_pofile @pytest.mark.parametrize(('abort_event'), (True, False)) def test_enter_leave_block_event(abort_event): - def print_is_task_list_item(self, block, details): + def print_is_task_list_item( + self, # noqa: ARG001 + block, + details, + ): if block is md4c.BlockType.LI: sys.stdout.write(str(details['is_task'])) @@ -95,10 +98,10 @@ def test_text_event(abort_event): @pytest.mark.parametrize(('abort_event'), (True, False)) def test_command_event(abort_event): def error_when_unknown_command_event( - self, + self, # noqa: ARG001 command, - comment, # noqa: U100 - original_command, # noqa: U100 + comment, # noqa: ARG001 + original_command, # noqa: ARG001 ): # here 'normalize_mdpo_command' is added to simulate a real behaviour, # is not related with the test itself @@ -126,14 +129,15 @@ def test_command_event_return_false(): def skip_counter_command_event( self, command, - comment, # noqa: U100 - original_command, # noqa: U100 + comment, # noqa: ARG001 + original_command, # noqa: ARG001 ): if command == 'mdpo-skip': self.skip_counter += 1 return False - elif command == 'mdpo-disable-next-line': + if command == 'mdpo-disable-next-line': return False + return None content = ''' @@ -179,9 +183,10 @@ def __init__(self, *args, **kwargs): def test_msgid_event(): - def dont_save_hate_msgid(self, msgid, *args): # noqa: U100 + def dont_save_hate_msgid(self, msgid, *args): # noqa: ARG001 if msgid == 'hate': return False + return None content = ''' hate @@ -210,9 +215,10 @@ def dont_save_hate_msgid(self, msgid, *args): # noqa: U100 def test_link_reference_event(): - def process_footnotes(self, target, href, title): # noqa: U100 + def process_footnotes(self, target, href, title): # noqa: ARG001 if re.match(r'^\^\d', target): return False + return None content = '''This is a footnote[^1]. This is another[^2]. diff --git a/tests/test_unit/test_md2po/test_obsoletes.py b/tests/test_unit/test_md2po/test_obsoletes.py index be8f2b08..6a20729b 100644 --- a/tests/test_unit/test_md2po/test_obsoletes.py +++ b/tests/test_unit/test_md2po/test_obsoletes.py @@ -3,7 +3,6 @@ """ import pytest - from mdpo.md2po import markdown_to_pofile diff --git a/tests/test_unit/test_md2po2md/test_md2po2md_cli.py b/tests/test_unit/test_md2po2md/test_md2po2md_cli.py index 5214821b..ab84789b 100644 --- a/tests/test_unit/test_md2po2md/test_md2po2md_cli.py +++ b/tests/test_unit/test_md2po2md/test_md2po2md_cli.py @@ -3,7 +3,6 @@ import os import pytest - from mdpo.md2po2md.__main__ import run diff --git a/tests/test_unit/test_mdpo2html/test_commands/test_mdpo2html_context.py b/tests/test_unit/test_mdpo2html/test_commands/test_mdpo2html_context.py index d149d312..21c86c9f 100644 --- a/tests/test_unit/test_mdpo2html/test_commands/test_mdpo2html_context.py +++ b/tests/test_unit/test_mdpo2html/test_commands/test_mdpo2html_context.py @@ -1,5 +1,4 @@ import pytest - from mdpo.mdpo2html import markdown_pofile_to_html diff --git a/tests/test_unit/test_mdpo2html/test_commands/test_mdpo2html_disable.py b/tests/test_unit/test_mdpo2html/test_commands/test_mdpo2html_disable.py index 0de2b8da..051e3819 100644 --- a/tests/test_unit/test_mdpo2html/test_commands/test_mdpo2html_disable.py +++ b/tests/test_unit/test_mdpo2html/test_commands/test_mdpo2html_disable.py @@ -1,5 +1,4 @@ import pytest - from mdpo.mdpo2html import MdPo2HTML, markdown_pofile_to_html diff --git a/tests/test_unit/test_mdpo2html/test_commands/test_mdpo2html_include_codeblock.py b/tests/test_unit/test_mdpo2html/test_commands/test_mdpo2html_include_codeblock.py index 2603db3c..1229fcdf 100644 --- a/tests/test_unit/test_mdpo2html/test_commands/test_mdpo2html_include_codeblock.py +++ b/tests/test_unit/test_mdpo2html/test_commands/test_mdpo2html_include_codeblock.py @@ -3,7 +3,6 @@ """ import pytest - from mdpo.mdpo2html import markdown_pofile_to_html @@ -20,13 +19,13 @@ def test_include_codeblock(command, command_aliases, tmp_file): msgid "" msgstr "" ''' - with pytest.warns(SyntaxWarning) as record: - with tmp_file(pofile_content, '.po') as po_filepath: - markdown_pofile_to_html( - html_input, - po_filepath, - command_aliases=command_aliases, - ) + with pytest.warns(SyntaxWarning) as record, \ + tmp_file(pofile_content, '.po') as po_filepath: + markdown_pofile_to_html( + html_input, + po_filepath, + command_aliases=command_aliases, + ) assert len(record) == 1 assert record[0].message.args[0] == ( diff --git a/tests/test_unit/test_mdpo2html/test_mdpo2html_cli.py b/tests/test_unit/test_mdpo2html/test_mdpo2html_cli.py index 06d66945..af434e5a 100644 --- a/tests/test_unit/test_mdpo2html/test_mdpo2html_cli.py +++ b/tests/test_unit/test_mdpo2html/test_mdpo2html_cli.py @@ -2,7 +2,6 @@ import os import pytest - from mdpo.mdpo2html.__main__ import run diff --git a/tests/test_unit/test_mdpo2html/test_mdpo2html_translate.py b/tests/test_unit/test_mdpo2html/test_mdpo2html_translate.py index f080cb04..4cef0a9e 100644 --- a/tests/test_unit/test_mdpo2html/test_mdpo2html_translate.py +++ b/tests/test_unit/test_mdpo2html/test_mdpo2html_translate.py @@ -2,7 +2,6 @@ import os import pytest - from mdpo.mdpo2html import markdown_pofile_to_html diff --git a/tests/test_unit/test_po.py b/tests/test_unit/test_po.py index e81ad099..47e225e6 100644 --- a/tests/test_unit/test_po.py +++ b/tests/test_unit/test_po.py @@ -1,5 +1,4 @@ import pytest - from mdpo.po import po_escaped_string diff --git a/tests/test_unit/test_po2md/test_commands/test_po2md_context.py b/tests/test_unit/test_po2md/test_commands/test_po2md_context.py index 732938a6..b7cc3b6a 100644 --- a/tests/test_unit/test_po2md/test_commands/test_po2md_context.py +++ b/tests/test_unit/test_po2md/test_commands/test_po2md_context.py @@ -1,5 +1,4 @@ import pytest - from mdpo.po2md import pofile_to_markdown diff --git a/tests/test_unit/test_po2md/test_commands/test_po2md_disable.py b/tests/test_unit/test_po2md/test_commands/test_po2md_disable.py index 764b8154..8e7f622d 100644 --- a/tests/test_unit/test_po2md/test_commands/test_po2md_disable.py +++ b/tests/test_unit/test_po2md/test_commands/test_po2md_disable.py @@ -1,5 +1,4 @@ import pytest - from mdpo.po2md import Po2Md, pofile_to_markdown diff --git a/tests/test_unit/test_po2md/test_po2md_cli.py b/tests/test_unit/test_po2md/test_po2md_cli.py index 62d49c1b..f122a8b3 100644 --- a/tests/test_unit/test_po2md/test_po2md_cli.py +++ b/tests/test_unit/test_po2md/test_po2md_cli.py @@ -5,7 +5,6 @@ import re import pytest - from mdpo.po2md.__main__ import run diff --git a/tests/test_unit/test_po2md/test_po2md_events.py b/tests/test_unit/test_po2md/test_po2md_events.py index c98cd0be..c6996b81 100644 --- a/tests/test_unit/test_po2md/test_po2md_events.py +++ b/tests/test_unit/test_po2md/test_po2md_events.py @@ -6,11 +6,12 @@ def test_link_reference_footnotes(tmp_file): - def process_footnote_references(self, target, href, title): # noqa: U100 + def process_footnote_references(self, target, href, title): # noqa: ARG001 if re.match(r'^\^\d', target): # footnotes are treated as text blocks, so we don't need to # translate them here return False + return None markdown_content = '''# Hello @@ -69,7 +70,7 @@ def process_footnote_references(self, target, href, title): # noqa: U100 def test_command_event(tmp_file): - def _abort_command(self, *args): # noqa: U100 + def _abort_command(self, *args): # noqa: ARG001 assert not self.disable return False @@ -97,7 +98,7 @@ def _abort_command(self, *args): # noqa: U100 def test_msgid_event(tmp_file): - def _abort_command(self, *args): # noqa: U100 + def _abort_command(self, *args): # noqa: ARG001 return False input_content = '''hello diff --git a/tests/test_unit/test_po2md/test_po2md_pofiles_glob.py b/tests/test_unit/test_po2md/test_po2md_pofiles_glob.py index ebccc48d..8fc7aacd 100644 --- a/tests/test_unit/test_po2md/test_po2md_pofiles_glob.py +++ b/tests/test_unit/test_po2md/test_po2md_pofiles_glob.py @@ -4,16 +4,17 @@ def test_multiple_pofiles_glob(tmp_dir): - markdown_content = """ + markdown_content = ''' Beyond good intentions, a dictatorship is a dictatorship. How is it that you think beautiful nerd? -""" +''' - expected_output = """Más allá de las buenas intenciones, una dictadura es una dictadura. + expected_output = ( + '''Más allá de las buenas intenciones, una dictadura es una dictadura. ¿Cómo es que te parece nerd lo bello? -""" +''') with tmp_dir({ 'foo.po': '''# diff --git a/tests/test_unit/test_po2md/test_po2md_translate.py b/tests/test_unit/test_po2md/test_po2md_translate.py index a9aa2fe9..1a248b72 100644 --- a/tests/test_unit/test_po2md/test_po2md_translate.py +++ b/tests/test_unit/test_po2md/test_po2md_translate.py @@ -2,7 +2,6 @@ import os import pytest - from mdpo.md2po import markdown_to_pofile from mdpo.po2md import pofile_to_markdown diff --git a/tests/test_unit/test_po2md/test_po2md_wrapwidth.py b/tests/test_unit/test_po2md/test_po2md_wrapwidth.py index eacf8fac..3c41a660 100644 --- a/tests/test_unit/test_po2md/test_po2md_wrapwidth.py +++ b/tests/test_unit/test_po2md/test_po2md_wrapwidth.py @@ -5,7 +5,6 @@ import os import pytest - from mdpo.po2md import pofile_to_markdown diff --git a/tests/test_unit/test_text.py b/tests/test_unit/test_text.py index 24df3b0c..acff1b15 100644 --- a/tests/test_unit/test_text.py +++ b/tests/test_unit/test_text.py @@ -2,7 +2,6 @@ import pytest - from mdpo.text import ( INFINITE_WRAPWIDTH, min_not_max_chars_in_a_row, @@ -88,7 +87,7 @@ def test_parse_strint_0_inf(value, expected_result, maybe_raises): ) def test_parse_wrapwidth_argument(value): if not value.isdigit() and value.lower() not in ('inf', 'infinity'): - expected_msg = f'Invalid value \'{value}\' for wrapwidth argument.' + expected_msg = f"Invalid value '{value}' for wrapwidth argument." with pytest.raises(ValueError, match=expected_msg): parse_wrapwidth_argument(value) else: