diff --git a/mdpo/__init__.py b/mdpo/__init__.py index 56aeba36..f6ae516b 100644 --- a/mdpo/__init__.py +++ b/mdpo/__init__.py @@ -5,7 +5,7 @@ from mdpo.po2md import pofile_to_markdown -__version__ = '0.3.0' +__version__ = '0.3.1' __version_info__ = tuple([int(i) for i in __version__.split('.')]) __title__ = 'mdpo' __description__ = ('Markdown file translation utilities using pofiles') diff --git a/mdpo/md2po/__main__.py b/mdpo/md2po/__main__.py index 399ec063..7a16cc2a 100755 --- a/mdpo/md2po/__main__.py +++ b/mdpo/md2po/__main__.py @@ -23,9 +23,10 @@ def build_parser(): parser.add_argument('-q', '--quiet', action='store_true', help='Do not print output to STDOUT.') parser.add_argument('glob_or_content', metavar='GLOB_OR_CONTENT', + nargs='*', help='Glob to markdown input files or markdown' - ' content as a string. If not provided,' - ' will be read from STDIN.') + ' content as string. If not provided, will be' + ' read from STDIN.') parser.add_argument('-i', '--ignore', dest='ignore', default=[], help='List of filepaths to ignore if' ' ``GLOB_OR_CONTENT`` argument is a glob,' @@ -102,6 +103,8 @@ def parse_options(args=[]): if not sys.stdin.isatty(): opts.glob_or_content = sys.stdin.read().strip('\n') + elif isinstance(opts.glob_or_content, list): + opts.glob_or_content = opts.glob_or_content[0] if opts.ignore: opts.ignore = parse_list_argument(opts.ignore) opts.extensions = opts.extensions.split(",") diff --git a/mdpo/mdpo2html/__main__.py b/mdpo/mdpo2html/__main__.py index 00f664c9..7a0e259a 100644 --- a/mdpo/mdpo2html/__main__.py +++ b/mdpo/mdpo2html/__main__.py @@ -22,7 +22,9 @@ def build_parser(): parser.add_argument('-q', '--quiet', action='store_true', help='Don\'t print output to STDOUT.') parser.add_argument('filepath_or_content', metavar='FILEPATH_OR_CONTENT', - help='HTML filepath or content to translate.') + nargs='*', + help='HTML filepath or content to translate.' + ' If not provided, will be read from STDIN.') parser.add_argument('-p', '--pofiles', metavar='POFILES', help='Glob matching a set of pofiles from where to' ' extract references to make the replacements' @@ -47,6 +49,8 @@ def parse_options(args): if not sys.stdin.isatty(): opts.filepath_or_content = sys.stdin.read().strip('\n') + elif isinstance(opts.filepath_or_content, list): + opts.filepath_or_content = opts.filepath_or_content[0] if opts.ignore: opts.ignore = parse_list_argument(opts.ignore) diff --git a/mdpo/po2md/__main__.py b/mdpo/po2md/__main__.py index 010f1a2e..ebcb25f3 100644 --- a/mdpo/po2md/__main__.py +++ b/mdpo/po2md/__main__.py @@ -21,7 +21,9 @@ def build_parser(): parser.add_argument('-q', '--quiet', action='store_true', help='Don\'t print output to STDOUT.') parser.add_argument('filepath_or_content', metavar='FILEPATH_OR_CONTENT', - help='Markdown filepath or content to translate.') + nargs='*', + help='Markdown filepath or content to translate.' + ' If not provided, will be read from STDIN.') parser.add_argument('-p', '--pofiles', metavar='POFILES', help='Glob matching a set of pofiles from where to' ' extract references to make the replacements' @@ -46,6 +48,8 @@ def parse_options(args): if not sys.stdin.isatty(): opts.filepath_or_content = sys.stdin.read().strip('\n') + elif isinstance(opts.filepath_or_content, list): + opts.filepath_or_content = opts.filepath_or_content[0] if opts.ignore: opts.ignore = parse_list_argument(opts.ignore) diff --git a/setup.cfg b/setup.cfg index 84bded11..8e8695dd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,10 +1,10 @@ [bumpversion] -current_version = 0.3.0 +current_version = 0.3.1 [bumpversion:file:mdpo/__init__.py] [coverage:report] -exclude_lines = +exclude_lines = if __name__ == .__main__.: except ImportError: if '-h' in args or '--help' in args: diff --git a/test/test_md2po/test_md2po_cli.py b/test/test_md2po/test_md2po_cli.py index dbfe714d..f4add07a 100644 --- a/test/test_md2po/test_md2po_cli.py +++ b/test/test_md2po/test_md2po_cli.py @@ -1,3 +1,4 @@ +import io import os import tempfile @@ -22,6 +23,15 @@ } +def test_stdin(capsys, monkeypatch): + monkeypatch.setattr('sys.stdin', io.StringIO(EXAMPLE['input'])) + pofile, exitcode = run() + out, err = capsys.readouterr() + assert exitcode == 0 + assert pofile.__unicode__() == EXAMPLE['output'] + assert striplastline(out) == EXAMPLE['output'] + + @pytest.mark.parametrize('arg', ['-q', '--quiet']) def test_quiet(capsys, arg): pofile, exitcode = run([EXAMPLE['input'], arg]) diff --git a/test/test_mdpo2html/test_mdpo2html_cli.py b/test/test_mdpo2html/test_mdpo2html_cli.py index e6199e4e..a1d4b222 100644 --- a/test/test_mdpo2html/test_mdpo2html_cli.py +++ b/test/test_mdpo2html/test_mdpo2html_cli.py @@ -1,3 +1,4 @@ +import io import os import tempfile from uuid import uuid4 @@ -24,6 +25,18 @@ } +def test_stdin(capsys, monkeypatch, tmp_file): + monkeypatch.setattr('sys.stdin', io.StringIO(EXAMPLE['html-input'])) + with tmp_file(EXAMPLE['pofile'], ".po") as po_filepath: + + output, exitcode = run(['-p', po_filepath]) + out, err = capsys.readouterr() + + assert exitcode == 0 + assert output == EXAMPLE['html-output'][:-1] # rstrip("\n") + assert out == EXAMPLE['html-output'] + + @pytest.mark.parametrize('arg', ['-q', '--quiet']) def test_quiet(capsys, arg, tmp_file): with tmp_file(EXAMPLE['pofile'], ".po") as po_filepath: @@ -38,32 +51,23 @@ def test_quiet(capsys, arg, tmp_file): @pytest.mark.parametrize('arg', ['-s', '--save']) def test_save(capsys, arg, tmp_file): - with tmp_file(EXAMPLE['pofile'], ".po") as po_filepath: - - output_html_filepath = os.path.join( - tempfile.gettempdir(), uuid4().hex + '.html') - input_html_filepath = os.path.join( - tempfile.gettempdir(), uuid4().hex + '.html') + with tmp_file(EXAMPLE['pofile'], ".po") as po_filepath, \ + tmp_file(EXAMPLE['html-input'], ".html") as html_input_filepath, \ + tmp_file(EXAMPLE['html-output'], ".html") as html_output_filepath: - with open(input_html_filepath, "w") as f: - f.write(EXAMPLE['html-input']) - - output, exitcode = run([input_html_filepath, '-p', po_filepath, - arg, output_html_filepath]) + output, exitcode = run([html_input_filepath, '-p', po_filepath, + arg, html_output_filepath]) out, err = capsys.readouterr() assert exitcode == 0 assert output == EXAMPLE['html-output'] assert out == '' - with open(output_html_filepath, "r") as f: + with open(html_output_filepath, "r") as f: output_html_content = f.read() assert output_html_content == EXAMPLE['html-output'] - os.remove(input_html_filepath) - os.remove(output_html_filepath) - @pytest.mark.parametrize('arg', ['-i', '--ignore']) def test_ignore_files_by_filepath(capsys, arg, tmp_file): diff --git a/test/test_po2md/test_po2md_cli.py b/test/test_po2md/test_po2md_cli.py index b19ec01c..a2482db1 100644 --- a/test/test_po2md/test_po2md_cli.py +++ b/test/test_po2md/test_po2md_cli.py @@ -1,3 +1,4 @@ +import io import os import tempfile from uuid import uuid4 @@ -24,6 +25,18 @@ } +def test_stdin(capsys, monkeypatch, tmp_file): + monkeypatch.setattr('sys.stdin', io.StringIO(EXAMPLE['markdown-input'])) + with tmp_file(EXAMPLE['pofile'], ".po") as po_filepath: + + output, exitcode = run(['-p', po_filepath]) + out, err = capsys.readouterr() + + assert exitcode == 0 + assert output == EXAMPLE['markdown-output'] + assert striplastline(out) == EXAMPLE['markdown-output'] + + @pytest.mark.parametrize('arg', ['-q', '--quiet']) def test_quiet(capsys, arg, tmp_file): with tmp_file(EXAMPLE['pofile'], ".po") as po_filepath: