Merge pull request #1 from altf4/args

Add support for command line arguments
This commit is contained in:
Will Bradley 2014-01-08 01:57:31 -08:00
commit fd518eb4b4
2 changed files with 95 additions and 7 deletions

53
.gitignore vendored Normal file
View File

@ -0,0 +1,53 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
# C extensions
*.so
# Distribution / packaging
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
.tox/
.coverage
.cache
nosetests.xml
coverage.xml
# Translations
*.mo
# Mr Developer
.mr.developer.cfg
.project
.pydevproject
# Rope
.ropeproject
# Django stuff:
*.log
*.pot
# Sphinx documentation
docs/_build/
#deleted files
*~

49
pinger.py Normal file → Executable file
View File

@ -25,11 +25,6 @@
# <http://www.gnu.org/licenses/> # <http://www.gnu.org/licenses/>
# #
# User-editable variables
host = "4.2.2.2" # IP or hostname
ping_frequency = 5 # in seconds
ping_log_max_size = 15
# #
# Dependencies # Dependencies
# #
@ -47,6 +42,10 @@ import re
import signal import signal
# File paths # File paths
import os import os
#Argument parsing
import argparse
#for exit
import sys
# Vars # Vars
startup_active_label = "✓ Start Automatically" startup_active_label = "✓ Start Automatically"
@ -54,6 +53,42 @@ startup_inactive_label = "Start Automatically"
home_path = os.path.expanduser("~") home_path = os.path.expanduser("~")
startup_path = home_path+'/.config/autostart/pinger.desktop' startup_path = home_path+'/.config/autostart/pinger.desktop'
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--target", help="Target to PING against. (IP / Hostname / Domain name). Defaults to 4.2.2.2")
parser.add_argument("-f", "--freq", help="Timeout between pings, in seconds. Defaults to 5")
parser.add_argument("-m", "--maxlog", help="Maximum amount of pings to log. Defaults to 15")
args = parser.parse_args()
#accumulate the arguments for use later
arguments = ""
for arg in sys.argv[1:]:
arguments += arg + " "
# User-editable variables
if args.target:
host = args.target
else:
host = "4.2.2.2" # IP or hostname
print "Using default target IP of 4.2.2.2"
if args.freq:
try:
ping_frequency = int(args.freq)
except ValueError:
sys.stderr.write("Error parsing argument '--freq'\n")
sys.exit(1)
else:
ping_frequency = 5 # in seconds
if args.maxlog:
try:
ping_log_max_size = int(args.maxlog)
except ValueError:
sys.stderr.write("Error parsing argument '--maxlog'\n")
sys.exit(1)
else:
ping_log_max_size = 15
# #
# Main Class # Main Class
# #
@ -83,7 +118,7 @@ class Pinger:
self.ping_log.append(value) self.ping_log.append(value)
self.update_log_menu() self.update_log_menu()
# limit the size of the log # limit the size of the log
if len(self.ping_log) > ping_log_max_size: if len(self.ping_log) >= ping_log_max_size:
# remove the earliest ping, not the latest # remove the earliest ping, not the latest
self.ping_log.pop(0) self.ping_log.pop(0)
@ -103,7 +138,7 @@ class Pinger:
with open(startup_path,'w') as f: with open(startup_path,'w') as f:
f.write("[Desktop Entry]\r\n" f.write("[Desktop Entry]\r\n"
"Type=Application\r\n" "Type=Application\r\n"
"Exec=python "+os.path.abspath( __file__ )+"\r\n" "Exec=python "+os.path.abspath( __file__ )+arguments+"\r\n"
"X-GNOME-Autostart-enabled=true\r\n" "X-GNOME-Autostart-enabled=true\r\n"
"Name=Pinger\r\n" "Name=Pinger\r\n"
"Comment=Pings the internet every few seconds") "Comment=Pings the internet every few seconds")