From 54a2e8657a6301db3148c48fa4f2d72e0ff5db39 Mon Sep 17 00:00:00 2001 From: Steve Clement Date: Mon, 3 Jun 2019 14:06:19 +0900 Subject: [PATCH] fix: [perms] Added try/except for various permission conditions, also create the output dir if not exist fix: [try/except] Catch Ctrl-c keyboard interrupt fix: [style] isort imports --- .../feed-generator-from-redis/fromredis.py | 11 +++++--- .../feed-generator-from-redis/generator.py | 25 +++++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/examples/feed-generator-from-redis/fromredis.py b/examples/feed-generator-from-redis/fromredis.py index 26b2ee6..47dd20f 100755 --- a/examples/feed-generator-from-redis/fromredis.py +++ b/examples/feed-generator-from-redis/fromredis.py @@ -1,15 +1,15 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import sys -import json import argparse import datetime +import json +import sys import time + import redis import settings - from generator import FeedGenerator @@ -60,7 +60,10 @@ class RedisToMISPFeed: except Exception as error: self.save_error_to_redis(error, data) - beautyful_sleep(5, self.format_last_action()) + try: + beautyful_sleep(5, self.format_last_action()) + except KeyboardInterrupt: + sys.exit(130) def pop(self, key): popped = self.serv.rpop(key) diff --git a/examples/feed-generator-from-redis/generator.py b/examples/feed-generator-from-redis/generator.py index 38a9d54..558de21 100755 --- a/examples/feed-generator-from-redis/generator.py +++ b/examples/feed-generator-from-redis/generator.py @@ -1,10 +1,10 @@ #!/usr/bin/env python3 -import sys +import datetime +import hashlib import json import os -import hashlib -import datetime +import sys import time import uuid @@ -142,9 +142,24 @@ class FeedGenerator: # Manifest def _init_manifest(self): + # check if outputdir exists and try to create it if not + if not os.path.exists(settings.outputdir): + try: + os.makedirs(settings.outputdir) + except PermissionError as error: + print(error) + print("Please fix the above error and try again.") + sys.exit(126) + # create an empty manifest - with open(os.path.join(settings.outputdir, 'manifest.json'), 'w'): - pass + try: + with open(os.path.join(settings.outputdir, 'manifest.json'), 'w'): + pass + except PermissionError as error: + print(error) + print("Please fix the above error and try again.") + sys.exit(126) + # create new event and save manifest self.create_daily_event()