Skip to content

Commit

Permalink
Modernize codebase, unify variable names... (#172)
Browse files Browse the repository at this point in the history
* Refactor

* Get rid of `striplastline` tests fixture

* Rewrite `tempfile.NamedTemporaryFile` in tests using context manager

* Remove uneeded casts to str in tests

* Refactor `capsys.readouterr` calls in tests

* Drop uneeded cast to str

* Refactor print comments

* Drop uneeded casts to `str` comparing `polib.POFile` with strings

* Improve test

* Add more tests and refactor function

* Allow to pass negative values to `wrapwidth` arguments.

* Rewrite incorrect statement from docs

* Remove some 'pragma: no cover' comments

* Drop `filehash` and `save_pofile_checking_file_changed` functions

* Fix empty indented code block line error in `po2md`

* Bump version

* Fix image links regression from v0.3.68
  • Loading branch information
mondeja committed Nov 7, 2021
1 parent 4bff661 commit 34af71e
Show file tree
Hide file tree
Showing 43 changed files with 593 additions and 445 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.3.70
current_version = 0.3.71

[bumpversion:file:mdpo/__init__.py]

Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repos:
hooks:
- id: trailing-whitespace
name: trailing-whitespace
exclude: test/test_po2md/translate-examples/code-blocks.*
- id: end-of-file-fixer
name: end-of-file-fixer
- id: double-quote-string-fixer
Expand Down
10 changes: 5 additions & 5 deletions docs/pre-commit-hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ so you don't need to specify them.
.. code-block:: yaml
- repo: https://github.com/mondeja/mdpo
rev: v0.3.70
rev: v0.3.71
hooks:
- id: md2po
args:
Expand Down Expand Up @@ -43,7 +43,7 @@ po2md
.. code-block:: yaml
- repo: https://github.com/mondeja/mdpo
rev: v0.3.70
rev: v0.3.71
hooks:
- id: po2md
args:
Expand All @@ -56,7 +56,7 @@ po2md
.. code-block:: yaml
- repo: https://github.com/mondeja/mdpo
rev: v0.3.70
rev: v0.3.71
hooks:
- id: po2md
files: README\.md
Expand All @@ -75,7 +75,7 @@ md2po2md
.. code-block:: yaml
- repo: https://github.com/mondeja/mdpo
rev: v0.3.70
rev: v0.3.71
hooks:
- id: md2po2md
args:
Expand All @@ -88,7 +88,7 @@ md2po2md
.. code-block:: yaml
- repo: https://github.com/mondeja/mdpo
rev: v0.3.70
rev: v0.3.71
hooks:
- id: md2po2md
files: README\.md
Expand Down
2 changes: 1 addition & 1 deletion mdpo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from mdpo.po2md import pofile_to_markdown


__version__ = '0.3.70'
__version__ = '0.3.71'
__title__ = 'mdpo'
__description__ = ('Markdown files translation using PO files.')
__all__ = (
Expand Down
2 changes: 1 addition & 1 deletion mdpo/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _block_msg(block, details):
return block.name


def debug_events(program): # pragma: no cover
def debug_events(program):
"""Debugging events for interfaces. Displays in STDOUT all event targets.
Args:
Expand Down
4 changes: 2 additions & 2 deletions mdpo/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def html_attrs_tuple_to_string(attrs):
"""
response = ''
for i, (name, value) in enumerate(attrs):
response += '%s' % name
response += name
if value is not None:
response += '="%s"' % value
response += f'="{value}"'
if i < len(attrs) - 1:
response += ' '
return response
Expand Down
24 changes: 8 additions & 16 deletions mdpo/io.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"""mdpo I/O utilities."""

import filecmp
import glob
import hashlib
import os
import re
import tempfile


MDPO_FILECMP_TEMPFILE = os.path.join(tempfile.gettempdir(), '__mpdo-filecmp')


def filter_paths(filepaths, ignore_paths=[]):
Expand Down Expand Up @@ -85,18 +89,6 @@ def to_glob_or_content(value):
return (True, parsed)


def filehash(filepath):
"""Compute the hash of a file.
Args:
filepath (str): Path to the file.
"""
hasher = hashlib.md5()
with open(filepath, 'rb') as f:
hasher.update(f.read())
return hasher.hexdigest()


def save_file_checking_file_changed(filepath, content, encoding='utf-8'):
"""Save a file checking if the content has changed.
Expand All @@ -112,9 +104,9 @@ def save_file_checking_file_changed(filepath, content, encoding='utf-8'):
f.write(content)
return True

pre_hash = filehash(filepath)
with open(MDPO_FILECMP_TEMPFILE, 'w') as temp_f, open(filepath) as f:
temp_f.write(f.read())
with open(filepath, 'w', encoding=encoding) as f:
f.write(content)
post_hash = filehash(filepath)

return pre_hash != post_hash
return filecmp.cmp(MDPO_FILECMP_TEMPFILE, filepath)
15 changes: 7 additions & 8 deletions mdpo/md.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ def escape_links_titles(text, link_start_string='[', link_end_string=']'):

for match in re.findall(regex, text):
original_string = match[0] + match[1]
target_string = match[0] + '"%s"' % (
match[1][1:-1].replace('"', '\\"')
)
escaped_title = match[1][1:-1].replace('"', '\\"')
target_string = f'{match[0]}"{escaped_title}"'
text = text.replace(original_string, target_string)
return text

Expand Down Expand Up @@ -418,10 +417,10 @@ 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('----> 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)
# print('----> link_references_text_targets', link_references_text_targets)
# print('----> msgid_msgstrs_with_links', msgid_msgstrs_with_links)
# print('----> solutions', solutions)
return solutions
50 changes: 34 additions & 16 deletions mdpo/md2po/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
parse_mdpo_html_command,
)
from mdpo.event import debug_events, raise_skip_event
from mdpo.io import filter_paths, to_glob_or_content
from mdpo.io import (
filter_paths,
save_file_checking_file_changed,
to_glob_or_content,
)
from mdpo.md import parse_link_references
from mdpo.md4c import (
DEFAULT_MD4C_GENERIC_PARSER_EXTENSIONS,
Expand All @@ -21,7 +25,6 @@
mark_not_found_entries_as_obsoletes,
po_escaped_string,
remove_not_found_entries,
save_pofile_checking_file_changed,
)
from mdpo.text import min_not_max_chars_in_a_row, parse_wrapwidth_argument

Expand Down Expand Up @@ -172,7 +175,7 @@ def __init__(self, glob_or_content, **kwargs):
self._include_next_codeblock = False
self._disable_next_codeblock = False

self._saved_files_changed = ( # pragma: no cover
self._saved_files_changed = (
False if kwargs.get('_check_saved_files_changed') else None
)

Expand Down Expand Up @@ -781,16 +784,30 @@ def leave_span(self, span, details):
)
self._codespan_backticks = None
elif span is md4c.SpanType.IMG:
self._current_msgid += '![{}]({}'.format(
self._current_imgspan['text'],
self._current_imgspan['src'],
)
if self._current_imgspan['title']:
self._current_msgid += ' "%s"' % (
self._current_imgspan['title']
# TODO: refactor with getattr? Currently getting next error
# getattr(self, target_varname) += '![{}]({}'.format(
# SyntaxError: cannot assign to function call

if not self._inside_aspan:
self._current_msgid += '![{}]({}'.format(
self._current_imgspan['text'],
self._current_imgspan['src'],
)
title = self._current_imgspan['title']
if title:
self._current_msgid += f' "{title}"'
self._current_msgid += ')'
self._current_imgspan = {}
else:
self._current_aspan_text += '![{}]({}'.format(
self._current_imgspan['text'],
self._current_imgspan['src'],
)
self._current_msgid += ')'
self._current_imgspan = {}
title = self._current_imgspan['title']
if title:
self._current_aspan_text += f' "{title}"'
self._current_aspan_text += ')'
self._current_imgspan = {}
elif span is md4c.SpanType.U:
self._inside_uspan = False

Expand Down Expand Up @@ -968,9 +985,10 @@ def _parse(content):

if save and po_filepath:
if self._saved_files_changed is False: # pragma: no cover
self._saved_files_changed = save_pofile_checking_file_changed(
self.pofile,
self._saved_files_changed = save_file_checking_file_changed(
po_filepath,
str(self.pofile),
encoding=self.pofile.encoding,
)
else:
self.pofile.save(fpath=po_filepath)
Expand Down Expand Up @@ -1034,8 +1052,8 @@ def markdown_to_pofile(
for you. It depends on the use you are going to give to
this library activate this mode (``plaintext=False``) or not.
wrapwidth (int): Wrap width for po file indicated at ``po_filepath``
parameter. Only useful when the ``-w`` option was passed
to xgettext.
parameter. If negative, 0, 'inf' or 'math.inf' the content won't
be wrapped.
mark_not_found_as_obsolete (bool): The strings extracted from markdown
that will not be found inside the provided pofile will be marked
as obsolete.
Expand Down
11 changes: 4 additions & 7 deletions mdpo/md2po/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ def build_parser():
'-w', '--wrapwidth', dest='wrapwidth', metavar='N/inf', type=str,
default='78',
help='Wrap width for po file indicated at \'-po/--po-filepath\''
' parameter. Only useful when the \'-w\' option was passed to'
' xgettext. You can use the values \'0\' and \'inf\' for infinite'
' width.',
' parameter. If negative, \'0\' or \'inf\', the PO file content'
' will not be wrapped.',
)
parser.add_argument(
'-m', '--merge-po-files', '--merge-pofiles',
Expand Down Expand Up @@ -204,12 +203,10 @@ def run(args=[]):
pofile = md2po.extract(**extract_kwargs)

if not opts.quiet:
sys.stdout.write(pofile.__unicode__() + '\n')
sys.stdout.write(f'{pofile}\n')

# pre-commit mode
if ( # pragma: no cover
opts.check_saved_files_changed and md2po._saved_files_changed
):
if opts.check_saved_files_changed and md2po._saved_files_changed:
return (pofile, 1)

return (pofile, 0)
Expand Down
14 changes: 5 additions & 9 deletions mdpo/md2po2md/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ def markdown_to_pofile_to_markdown(

os.makedirs(os.path.abspath(po_dirpath), exist_ok=True)
if os.path.isdir(po_filepath):
po_filepath = (
po_filepath.rstrip(os.sep) + os.sep +
os.path.basename(filepath) + '.po'
po_filepath = os.path.join(
po_filepath.rstrip(os.sep),
f'{os.path.basename(filepath)}.po',
)
if not po_filepath.endswith('.po'):
po_filepath += '.po'
Expand Down Expand Up @@ -139,9 +139,7 @@ def markdown_to_pofile_to_markdown(
po_encoding=po_encoding,
md_encoding=md_encoding,
)
if ( # pragma: no cover
_check_saved_files_changed and _saved_files_changed is False
):
if _check_saved_files_changed and _saved_files_changed is False:
_saved_files_changed = md2po._saved_files_changed

# po2md
Expand All @@ -158,9 +156,7 @@ def markdown_to_pofile_to_markdown(
save=md_filepath,
md_encoding=md_encoding,
)
if ( # pragma: no cover
_check_saved_files_changed and _saved_files_changed is False
):
if _check_saved_files_changed and _saved_files_changed is False:
_saved_files_changed = po2md._saved_files_changed

return _saved_files_changed
4 changes: 1 addition & 3 deletions mdpo/md2po2md/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ def run(args=[]):
_check_saved_files_changed=opts.check_saved_files_changed,
**kwargs,
)
if ( # pragma: no cover
opts.check_saved_files_changed and _saved_files_changed
):
if opts.check_saved_files_changed and _saved_files_changed:
exitcode = 1
return exitcode

Expand Down

0 comments on commit 34af71e

Please sign in to comment.