From bf67d069460b05470aa00a38abb9a319383f70b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bob=20Dr=C3=B6ge?= Date: Tue, 14 Dec 2021 16:21:22 +0100 Subject: [PATCH] allow passing of an event as json file --- eessi_bot_software_layer.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/eessi_bot_software_layer.py b/eessi_bot_software_layer.py index a942961c..020a3164 100644 --- a/eessi_bot_software_layer.py +++ b/eessi_bot_software_layer.py @@ -12,8 +12,12 @@ import datetime import flask import github +import json import os import pprint +import sys +from collections import namedtuple +from requests.structures import CaseInsensitiveDict LOG = os.path.join(os.getenv('HOME'), 'eessi-bot-software-layer.log') @@ -41,6 +45,18 @@ def log_event(request): log(msg_txt) +def read_event_from_json(jsonfile): + """ + Read in event data from a json file. + """ + req = namedtuple('Request', ['headers', 'json']) + with open(jsonfile, 'r') as jf: + event_data = json.load(jf) + req.headers = CaseInsensitiveDict(event_data['headers']) + req.json = event_data['json'] + return req + + def create_app(gh): """ Create Flask app. @@ -66,5 +82,9 @@ def main(): if __name__ == '__main__': - app = main() - app.run() + if len(sys.argv) > 1: + event = read_event_from_json(sys.argv[1]) + log_event(event) + else: + app = main() + app.run()