Last active
August 29, 2015 14:03
-
-
Save om-henners/afe1c1b006746895e75c to your computer and use it in GitHub Desktop.
Example of using a with statement to wrap your script so that it will notify with an email on success or failure. The error is then re-raised, so whatever is running the original tool will see it. The mail section was taken from the snipped provided at http://gis.stackexchange.com/a/103394/150
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smtplib, os | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEBase import MIMEBase | |
from email.MIMEText import MIMEText | |
from email import Encoders | |
import traceback | |
import textwrap | |
from datetime import datetime | |
from StringIO import StringIO | |
def mail(to, subject, text, gmail_user, gmail_pwd): | |
msg = MIMEMultipart() | |
msg['From'] = gmail_user | |
msg['To'] = to | |
msg['Subject'] = subject | |
# Attaches a text message to the e-mail | |
msg.attach(MIMEText(text)) | |
mailServer =smtplib.SMTP("smtp.gmail.com", 587) | |
mailServer.ehlo() | |
mailServer.starttls() | |
mailServer.ehlo() | |
mailServer.login(gmail_user, gmail_pwd) | |
mailServer.sendmail(gmail_user, to, msg.as_string()) | |
mailServer.close() | |
class ScriptWrapper(object): | |
def __init__(self, tool_name, msg_to, gmail_user, gmail_pwd): | |
""" | |
Wrap a tool to send an email on success or failure | |
""" | |
self.tool_name = tool_name | |
self.msg_to = msg_to | |
self.gmail_user = gmail_user | |
self.gmail_pwd = gmail_pwd | |
def __enter__(self): | |
return #We don't pass back any variables to the with | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
#When the with is ended it comes through here. The exc_type, exc_val, and exc_tb variables are only populated on an error | |
if exc_type: #error in execution | |
tb = StringIO() | |
traceback.print_tb(exc_tb, file=tb) | |
text = textwrap.dedent("""\ | |
Tool '{tool_name}' at {time} failed with the following error: | |
{traceback} | |
{error_type}: {error} | |
""").format( | |
tool_name=self.tool_name, | |
time=datetime.now().isoformat(), | |
traceback=tb.getvalue(), | |
error=exc_val, | |
error_type=str(exc_type.__name__) | |
) | |
subject = "{tool_name} failed with error {error}".format( | |
tool_name=self.tool_name, | |
error=exc_val | |
) | |
else: #tool succeeded | |
text = textwrap.dedent("""\ | |
Tool '{tool_name}' suceeded at {time}. | |
""").format( | |
tool_name=self.tool_name, | |
time=datetime.now().isoformat() | |
) | |
subject = "{tool_name} succeeded".format(tool_name=self.tool_name) | |
mail(self.msg_to, subject, text, self.gmail_user, self.gmail_pwd) | |
return False #Still want to throw the error so ArcGIS shows it |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arcpy | |
import script_mail_wrapper | |
def main(): | |
#Your tool | |
pass | |
if __name__ == "__main__": | |
gmail_user = arcpy.GetParameterAsText(0) | |
gmail_pwd = arcpy.GetParameterAsText(1) | |
#wraps your tool to send an email on success or failure | |
with script_mail_wrapper.ScriptWrapper("Tool Name", "[email protected]", gmail_user, gmail_pwd): | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment