Skip to content
Snippets Groups Projects
Commit 2b9e5d2c authored by kpetersn's avatar kpetersn
Browse files

Handle xml parse error. Added notifyEmail and notifyCombo functions.

parent ae678f82
No related branches found
No related tags found
No related merge requests found
......@@ -7,32 +7,50 @@
import urllib
import xml.etree.ElementTree as ET
import os
import sys
# for time.asctime()
#!import time
# needed for notifyOSX()
import os
osx_command = "/Users/kpetersn/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier"
osx_url = "http://bcda.xray.aps.anl.gov/cgi-bin/ioc_alive.cgi?eng=Peterson"
### Stuff to customize ###
engineer = "Peterson"
# period should match cron job period
period = 60
# needed for notifyOSX()
osx_command = "/Users/kpetersn/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier"
# needed for notifyEmail()
recipient="kmpeters@anl.gov"
def notifyConsole(ioc, state, time_str):
print "[%s] %s is %s" % (time_str, ioc, state)
def notifyEmail(ioc, state, time_str):
subject = "%s is %s" % (ioc, state)
message = "Snapshot time: %s\n%s" % (time_str, eng_url)
email_command = "echo \"%s\" | mailx -s \"%s\" %s" % (message, subject, recipient)
os.system(email_command)
def notifyOSX(ioc, state, time_str):
notify_command = "%s -title \"%s is %s\" -message \"%s\" -open \"%s\"" % (osx_command, ioc, state, time_str, osx_url)
notify_command = "%s -title \"%s is %s\" -message \"%s\" -open \"%s\"" % (osx_command, ioc, state, time_str, eng_url)
os.system(notify_command)
def notifyCombo(ioc, state, time_str):
notifyOSX(ioc, state, time_str)
notifyEmail(ioc, state, time_str)
# set notification function
notify = notifyOSX
#!notify = notifyConsole
#!notify = notifyEmail
notify = notifyOSX
#!notify = notifyCombo
##########################
alive_url = "http://bcda.xray.aps.anl.gov/cgi-bin/alivexml.cgi"
eng_url = "http://bcda.xray.aps.anl.gov/cgi-bin/ioc_alive.cgi?eng=%s" % engineer
hb_period = 10
ioc_list = {}
......@@ -46,30 +64,34 @@ f.close()
# Parse the xml string
#!print "Parsing XML"
root = ET.fromstring(xml_string)
try:
root = ET.fromstring(xml_string)
except ET.ParseError:
print "Invalid XML file"
sys.exit(1)
# Get the date and time
time_elem = root.find('time')
time_str = time_elem.text
# Indent for making ET.tostring() output readable
def _indent(elem, level=0):
'''
Internal method to make the xml file easier to read by humans.
'''
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
_indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
#!def _indent(elem, level=0):
#! '''
#! Internal method to make the xml file easier to read by humans.
#! '''
#! i = "\n" + level*" "
#! if len(elem):
#! if not elem.text or not elem.text.strip():
#! elem.text = i + " "
#! if not elem.tail or not elem.tail.strip():
#! elem.tail = i
#! for elem in elem:
#! _indent(elem, level+1)
#! if not elem.tail or not elem.tail.strip():
#! elem.tail = i
#! else:
#! if level and (not elem.tail or not elem.tail.strip()):
#! elem.tail = i
# Indent for making ET.tostring() output readable
#!_indent(root)
......@@ -116,3 +138,5 @@ iocs.sort()
for ioc in iocs:
notify(ioc, ioc_list[ioc], time_str)
sys.exit(0)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment