Skip to content

Commit

Permalink
Read input from STDIN in CLI interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Nov 5, 2020
1 parent 5c25ba5 commit 6311f90
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 22 deletions.
2 changes: 1 addition & 1 deletion mdpo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
7 changes: 5 additions & 2 deletions mdpo/md2po/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,'
Expand Down Expand Up @@ -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(",")
Expand Down
6 changes: 5 additions & 1 deletion mdpo/mdpo2html/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion mdpo/po2md/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
10 changes: 10 additions & 0 deletions test/test_md2po/test_md2po_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import os
import tempfile

Expand All @@ -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])
Expand Down
34 changes: 19 additions & 15 deletions test/test_mdpo2html/test_mdpo2html_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import os
import tempfile
from uuid import uuid4
Expand All @@ -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:
Expand All @@ -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):
Expand Down
13 changes: 13 additions & 0 deletions test/test_po2md/test_po2md_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import os
import tempfile
from uuid import uuid4
Expand All @@ -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:
Expand Down

0 comments on commit 6311f90

Please sign in to comment.