Skip to content

Commit

Permalink
Add --timezone option to the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Jan 31, 2021
1 parent 7889cbc commit bc72852
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
13 changes: 12 additions & 1 deletion pingparsing/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import Any, Dict, Optional, Tuple

import humanreadable as hr
from pytz import timezone
from subprocrunner import CommandError

from .__version__ import __version__
Expand Down Expand Up @@ -161,6 +162,10 @@ def parse_option() -> argparse.Namespace:
default=False,
help="print results for each ICMP packet reply.",
)
group.add_argument(
"--timezone",
help="Time zone for timestamps.",
)
group.add_argument(
"--no-color",
action="store_true",
Expand Down Expand Up @@ -236,6 +241,7 @@ def parse_ping(
timeout: TimeArg,
is_parse_icmp_reply: bool,
timestamp: str,
timezone_name: str,
addopts: str,
) -> Tuple[str, Any]:
if os.path.isfile(dest_or_file):
Expand Down Expand Up @@ -265,7 +271,11 @@ def parse_ping(
if result.stderr:
logger.error(result.stderr)

ping_parser = PingParsing()
if timezone_name:
ping_parser = PingParsing(timezone=timezone(timezone_name))
else:
ping_parser = PingParsing()

stats = ping_parser.parse(ping_result_text)
output = stats.as_dict(include_icmp_replies=is_parse_icmp_reply)

Expand Down Expand Up @@ -375,6 +385,7 @@ def main() -> int:
timeout,
options.icmp_reply,
options.timestamp,
options.timezone,
options.addopts,
)
)
Expand Down
73 changes: 72 additions & 1 deletion test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest
from subprocrunner import SubprocessRunner

from .data import DEBIAN_SUCCESS_0, UBUNTU_SUCCESS_2, WINDOWS7SP1_SUCCESS
from .data import DEBIAN_SUCCESS_0, UBUNTU_SUCCESS_1, UBUNTU_SUCCESS_2, WINDOWS7SP1_SUCCESS


def print_result(stdout, stderr, expected=None):
Expand Down Expand Up @@ -63,6 +63,77 @@ def test_normal_multi(self, tmpdir):
assert parsed_result[tmp_ping_path_deb] == DEBIAN_SUCCESS_0.expected
assert parsed_result[tmp_ping_path_win] == WINDOWS7SP1_SUCCESS.expected

def test_normal_timezone(self, tmpdir):
tmp_ping_file = tmpdir.join("ping_timezone.txt")
tmp_ping_file.write(UBUNTU_SUCCESS_1.value)
tmp_ping_path = str(tmp_ping_file)

runner = SubprocessRunner(
[
sys.executable,
"-m",
"pingparsing",
"--icmp-reply",
"--timezone",
"UTC",
"--no-color",
tmp_ping_path,
]
)
runner.run()
print_result(stdout=runner.stdout, stderr=runner.stderr)
assert runner.returncode == 0
assert json.loads(runner.stdout)[tmp_ping_path] == {
"destination": "google.com",
"packet_transmit": 5,
"packet_receive": 5,
"packet_loss_count": 0,
"packet_loss_rate": 0.0,
"rtt_min": 136.537,
"rtt_avg": 139.174,
"rtt_max": 148.006,
"rtt_mdev": 4.425,
"packet_duplicate_count": 0,
"packet_duplicate_rate": 0.0,
"icmp_replies": [
{
"timestamp": "2018-04-28T15:55:37.003555+00:00",
"icmp_seq": 1,
"ttl": 39,
"time": 148.0,
"duplicate": False,
},
{
"timestamp": "2018-04-28T15:55:37.787175+00:00",
"icmp_seq": 2,
"ttl": 39,
"time": 137.0,
"duplicate": False,
},
{
"timestamp": "2018-04-28T15:55:38.787642+00:00",
"icmp_seq": 3,
"ttl": 39,
"time": 137.0,
"duplicate": False,
},
{
"timestamp": "2018-04-28T15:55:39.787653+00:00",
"icmp_seq": 4,
"ttl": 39,
"time": 136.0,
"duplicate": False,
},
{
"timestamp": "2018-04-28T15:55:40.788365+00:00",
"icmp_seq": 5,
"ttl": 39,
"time": 136.0,
"duplicate": False,
},
],
}


@pytest.mark.xfail(run=False)
class Test_cli_pipe:
Expand Down

0 comments on commit bc72852

Please sign in to comment.