Skip to content

Instantly share code, notes, and snippets.

@f4rx
Last active May 30, 2018 09:31
Show Gist options
  • Save f4rx/fc2c13abd1c9541d878d8c39ba641b30 to your computer and use it in GitHub Desktop.
Save f4rx/fc2c13abd1c9541d878d8c39ba641b30 to your computer and use it in GitHub Desktop.
exit
import sys
import json
import os
import yaml
def get_message(notification):
if 'notification_type' not in notification or notification['notification_type'] != 'error':
raise SystemExit()
return notification['message']
def send_message(message):
if os.path.isfile("/tmp/output.yaml"):
raise SystemExit("Output file already exists")
with open('/tmp/output.yml', "w") as outfile:
yaml.dump(message, outfile, default_flow_style=False)
def main():
notification = json.loads(sys.argv[1])
message = get_message(notification)
send_message(message)
if __name__ == '__main__':
main()
import sys
import json
import os
import yaml
def get_message(notification):
# if 'notification_type' not in notification or notification['notification_type'] != 'error':
# sys.exit(0)
return notification['message']
def send_message(message):
if os.path.isfile("/tmp/output.yml"):
sys.exit("Output file already exists")
# or
# print("Output file already exists")
# sys.exit()
with open('/tmp/output.yml', "w") as outfile:
yaml.dump(message, outfile, default_flow_style=False)
def main():
notification = json.loads(sys.argv[1])
message = get_message(notification)
send_message(message)
if __name__ == '__main__':
main()
import sys
import json
import os
import yaml
def get_message(notification):
if 'notification_type' not in notification or notification['notification_type'] != 'error':
return
return notification['message']
def send_message(message):
if os.path.isfile("/tmp/output.yaml"):
return
with open('/tmp/output.yml', "w") as outfile:
yaml.dump(message, outfile, default_flow_style=False)
def main():
notification = json.loads(sys.argv[1])
if not notification:
sys.exit(0)
message = get_message(notification)
if not message:
sys.exit("Output file already exists")
send_message(message)
if __name__ == '__main__':
try:
main()
except SystemExit as e:
print(e)
sys.exit(0)
import sys
import json
import os
import yaml
import logging
def send_message(message):
with open('/tmp/output.yml', "w") as outfile:
yaml.dump(message, outfile, default_flow_style=False)
def is_sent(message):
return os.path.exists('/tmp/output.yml')
def get_notification():
return json.loads(sys.argv[1])
def main():
try:
n = get_notification()
except:
logging.exception("Failed to get notification")
return
if n.get("notification_type") != "error":
logging.info("Non-error notification")
return
msg = n.get("message")
if not msg:
logging.error("Message-less notification")
if not is_sent(msg):
try:
send_message(msg)
except:
logging.exception("falied to send message")
if __name__ == '__main__':
try:
main()
except:
logging.exception("Unhandled exception")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment