From c6b9ae09be2f22a003e0f482a70d4c9188a4a9e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar?= Date: Mon, 28 Feb 2022 19:01:25 +0100 Subject: [PATCH] Allow to pass languages with `-l es fr de` to `md2po2md` (#224) * Allow to pass languages with `-l es fr de` to `md2po2md` --- .bumpversion.cfg | 2 +- docs/pre-commit-hooks.rst | 25 +++++-------- docs/tutorial.rst | 8 ++--- mdpo/__init__.py | 2 +- mdpo/md2po2md/__main__.py | 11 ++++-- setup.cfg | 6 ++-- .../test_md2po2md/test_md2po2md_cli.py | 36 +++++++++++++++++-- 7 files changed, 61 insertions(+), 29 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index b605ea6d..9b836033 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.3.84 +current_version = 0.3.85 [bumpversion:file:mdpo/__init__.py] diff --git a/docs/pre-commit-hooks.rst b/docs/pre-commit-hooks.rst index 4bc970fe..bf01a599 100644 --- a/docs/pre-commit-hooks.rst +++ b/docs/pre-commit-hooks.rst @@ -19,7 +19,7 @@ so you don't need to specify them. .. code-block:: yaml - repo: https://github.com/mondeja/mdpo - rev: v0.3.84 + rev: v0.3.85 hooks: - id: md2po args: @@ -32,7 +32,7 @@ so you don't need to specify them. .. code-block:: yaml - repo: https://github.com/mondeja/mdpo - rev: v0.3.84 + rev: v0.3.85 hooks: - id: md2po files: ^README\.md @@ -53,7 +53,7 @@ po2md .. code-block:: yaml - repo: https://github.com/mondeja/mdpo - rev: v0.3.84 + rev: v0.3.85 hooks: - id: po2md args: @@ -68,7 +68,7 @@ po2md .. code-block:: yaml - repo: https://github.com/mondeja/mdpo - rev: v0.3.84 + rev: v0.3.85 hooks: - id: po2md files: ^README\.md @@ -91,14 +91,13 @@ md2po2md .. code-block:: yaml - repo: https://github.com/mondeja/mdpo - rev: v0.3.84 + rev: v0.3.85 hooks: - id: md2po2md args: - README.md - -l - es - - -l - fr - -o - locale/{lang} @@ -108,17 +107,11 @@ md2po2md .. code-block:: yaml - repo: https://github.com/mondeja/mdpo - rev: v0.3.84 + rev: v0.3.85 hooks: - id: md2po2md files: ^README\.md - args: - - -l - - es - - -l - - fr - - -o - - locale/{lang} + args: ['-l', 'es', 'fr', '-o', 'locale/{lang}'] .. seealso:: * :ref:`md2po2md CLI` @@ -133,7 +126,7 @@ mdpo2html .. code-block:: yaml - repo: https://github.com/mondeja/mdpo - rev: v0.3.84 + rev: v0.3.85 hooks: - id: mdpo2html args: @@ -148,7 +141,7 @@ mdpo2html .. code-block:: yaml - repo: https://github.com/mondeja/mdpo - rev: v0.3.84 + rev: v0.3.85 hooks: - id: mdpo2html files: ^README\.html diff --git a/docs/tutorial.rst b/docs/tutorial.rst index d1767a9c..d1626c8c 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -67,9 +67,9 @@ Just use :ref:`md2po2md CLI`: .. code-block:: bash - md2po2md README.md -l es -l fr -o "locale/{lang}" + md2po2md README.md -l es fr -o "locale/{lang}" -Define the languages to translate into using the ``-l`` option. +Define the languages to translate into using the ``--lang``/``-l`` option. You also can use the next snippet to include links for the translations: @@ -94,10 +94,10 @@ This will be the output after the previous command: ├── locale │   ├── es │   | ├── README.md - │   | └── readme.po  + │   | └── readme.po | └── fr │   ├── README.md - │   └── readme.po  + │   └── readme.po └── README.md .. seealso:: diff --git a/mdpo/__init__.py b/mdpo/__init__.py index 28f31f19..06e67b1e 100644 --- a/mdpo/__init__.py +++ b/mdpo/__init__.py @@ -2,7 +2,7 @@ __description__ = ('Markdown files translation using PO files.') __title__ = 'mdpo' -__version__ = '0.3.84' +__version__ = '0.3.85' __all__ = [ '__description__', '__title__', diff --git a/mdpo/md2po2md/__main__.py b/mdpo/md2po2md/__main__.py index a5c3250f..2fd0fe02 100755 --- a/mdpo/md2po2md/__main__.py +++ b/mdpo/md2po2md/__main__.py @@ -6,6 +6,7 @@ """ import argparse +import itertools import sys from mdpo.cli import ( @@ -18,6 +19,7 @@ add_nolocation_option, add_pre_commit_option, add_wrapwidth_argument, + cli_codespan, parse_command_aliases_cli_arguments, ) from mdpo.context import environ @@ -40,9 +42,12 @@ def build_parser(): ' If not provided, will be read from STDIN.', ) parser.add_argument( - '-l', '--lang', dest='langs', default=[], action='append', + '-l', '--lang', dest='langs', default=[], nargs='*', + action='append', help='Language codes used to create the output directories.' - ' This argument can be passed multiple times.', + ' This argument can be passed multiple times. Also, all' + ' languages can be defined after this argument with' + f" {cli_codespan('-l es_ES fr_FR de_DE')}.", metavar='LANG', required=True, ) @@ -100,6 +105,8 @@ def parse_options(args=[]): sys.exit(1) opts.input_paths_glob = input_paths_glob + opts.langs = set(itertools.chain(*opts.langs)) # flatten + if opts.extensions is None: opts.extensions = DEFAULT_MD4C_GENERIC_PARSER_EXTENSIONS diff --git a/setup.cfg b/setup.cfg index 231bbb73..4d678443 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = mdpo -version = 0.3.84 +version = 0.3.85 description = Markdown files translation using PO files. long_description = file: README.md long_description_content_type = text/markdown @@ -135,7 +135,7 @@ sections = STDLIB,THIRDPARTY,FIRSTPARTY,TESTS,LOCALFOLDER [build_sphinx] project = mdpo -version = 0.3.84 -release = 0.3.84 +version = 0.3.85 +release = 0.3.85 source-dir = docs build-dir = docs/_build diff --git a/test/test_unit/test_md2po2md/test_md2po2md_cli.py b/test/test_unit/test_md2po2md/test_md2po2md_cli.py index c5c2b34b..b0607a9a 100644 --- a/test/test_unit/test_md2po2md/test_md2po2md_cli.py +++ b/test/test_unit/test_md2po2md/test_md2po2md_cli.py @@ -12,6 +12,7 @@ @pytest.mark.parametrize('output_arg', ('-o', '--output')) @pytest.mark.parametrize('langs_arg', ('-l', '--lang')) +@pytest.mark.parametrize('all_langs_in_same_arg', (True, False)) @pytest.mark.parametrize( ( 'langs', @@ -120,11 +121,36 @@ FileNotFoundError, id='no-files-matching-input-glob', ), + pytest.param( + ('es', 'fr', 'de_DE'), + 'README.md', + 'locale/{lang}', + {'README.md': 'Foo\n\nBar\n'}, + { + 'locale/es/README.md.po': ( + '#\nmsgid ""\nmsgstr ""\n\nmsgid "Foo"\nmsgstr ""\n\n' + 'msgid "Bar"\nmsgstr ""\n' + ), + 'locale/es/README.md': 'Foo\n\nBar\n', + 'locale/fr/README.md.po': ( + '#\nmsgid ""\nmsgstr ""\n\nmsgid "Foo"\nmsgstr ""\n\n' + 'msgid "Bar"\nmsgstr ""\n' + ), + 'locale/fr/README.md': 'Foo\n\nBar\n', + 'locale/de_DE/README.md.po': ( + '#\nmsgid ""\nmsgstr ""\n\nmsgid "Foo"\nmsgstr ""\n\n' + 'msgid "Bar"\nmsgstr ""\n' + ), + 'locale/de_DE/README.md': 'Foo\n\nBar\n', + }, + id='README-es,fr-locale/{lang}', + ), ), ) def test_md2po2md_arguments( output_arg, langs_arg, + all_langs_in_same_arg, langs, input_paths_glob, output, @@ -151,8 +177,14 @@ def cleanup(): os.path.join(basedir, input_paths_glob), output_arg, os.path.join(basedir, output), ] - for lang in langs: - cmd.extend([langs_arg, lang]) + + # if all languages are passed in the same `--lang`/`-l` argument + if all_langs_in_same_arg: + cmd.extend([langs_arg, *langs]) + else: + for lang in langs: + cmd.extend([langs_arg, lang]) + cmd.append('--no-location') if hasattr(expected_files_content, '__traceback__'):