Skip to content

Commit

Permalink
Modifications for #21
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Dec 11, 2016
1 parent 35981e7 commit d736735
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
29 changes: 23 additions & 6 deletions pingparsing/_pingtransmitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class PingTransmitter(object):
.. py:attribute:: waittime
Time [sec] for sending packets.
if the value is ``None``, sending packets time will be the same as
built-in ``ping`` command.
Defaults to 1 [sec].
.. py:attribute:: ping_option
Expand Down Expand Up @@ -77,10 +79,7 @@ def ping(self):
if dp.is_not_empty_string(self.ping_option):
command_list.append(self.ping_option)

if platform.system() == "Windows":
command_list.append("-n {:d}".format(self.waittime))
else:
command_list.append("-q -w {:d}".format(self.waittime))
command_list.append(self.__get_waittime_option())

ping_proc = subprocess.Popen(
" ".join(command_list), shell=True,
Expand All @@ -93,9 +92,17 @@ def __validate_ping_param(self):
if dp.is_empty_string(self.destination_host):
raise ValueError("required destination_host")

if self.waittime <= 0:
if self.waittime is None:
return

waittime = dp.IntegerType(self.waittime).try_convert()
if waittime is None:
raise ValueError("wait time must be an integer: actual={}".format(
self.waittime))

if waittime <= 0:
raise ValueError(
"wait time must be greater than or equal to zero")
"wait time must be greater than zero")

def __get_base_ping_command(self):
command_list = []
Expand All @@ -109,3 +116,13 @@ def __get_base_ping_command(self):
])

return command_list

def __get_waittime_option(self):
waittime = dp.IntegerType(self.waittime).try_convert()
if waittime is None:
return ""

if platform.system() == "Windows":
return "-n {:d}".format(waittime)
else:
return "-q -w {:d}".format(waittime)
1 change: 1 addition & 0 deletions test/test_pingtransmitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_normal(self, transmitter, host, waittime, expected):
["", 1, ValueError],
["localhost", 0, ValueError],
["localhost", -1, ValueError],
["localhost", "a", ValueError],
[None, 1, ValueError],
])
def test_except(self, transmitter, host, waittime, expected):
Expand Down

0 comments on commit d736735

Please sign in to comment.