Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: add option to read from stdin #151

Merged
merged 3 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ install:
- pip install coverage pytest-cov pytest-env pytest-runner
# Code style dependencies
- pip install pycodestyle
- pip install -e .

before_script:
- pycodestyle -v metomi/isodatetime
Expand Down
17 changes: 14 additions & 3 deletions metomi/isodatetime/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@
from .datetimeoper import DateTimeOperator


def main():
"""Implement "isodatetime" command."""
def parse_args():
arg_parser = ArgumentParser(
prog='isodatetime',
formatter_class=RawDescriptionHelpFormatter,
Expand All @@ -184,7 +183,10 @@ def main():
[
["items"],
{
"help": "Time point, duration or recurrence string",
"help": (
"Time point, duration or recurrence string."
" To read from stdin use '-'."
),
"metavar": "ITEM",
"nargs": "*",
},
Expand Down Expand Up @@ -284,6 +286,12 @@ def main():
args = arg_parser.parse_intermixed_args()
else:
args = arg_parser.parse_args()
return args


def main():
"""Implement "isodatetime" command."""
args = parse_args()
if args.version_mode:
print(__version__)
return
Expand All @@ -293,6 +301,9 @@ def main():
calendar_mode=args.calendar,
ref_point_str=args.ref_point_str)

if list(args.items) == ['-']:
args.items = sys.stdin.read().splitlines()

try:
if len(args.items) >= 2:
out = date_time_oper.diff_time_point_strs(
Expand Down
18 changes: 18 additions & 0 deletions metomi/isodatetime/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@


import os
from subprocess import PIPE, Popen # nosec
import sys
import unittest
from unittest.mock import patch

import pytest

import metomi.isodatetime
import metomi.isodatetime.main as isodatetime_main
Expand Down Expand Up @@ -251,5 +253,21 @@ def test_4_bad(self, mock_print):
sys.argv = argv


@pytest.mark.parametrize(
'stdin,args,stdout', [
('2000', [], '2000'),
('2000\n2001', ['--as-total', 'h'], '8784.0')
]
)
def test_pipe(stdin, args, stdout):
"""Test piping args into the command via stdin."""
assert Popen(
['isodatetime', '-'] + args,
stdout=PIPE, stdin=PIPE
).communicate( # nosec
stdin.encode()
)[0].decode().strip() == stdout


if __name__ == '__main__':
unittest.main()