예제 #1
0
파일: Manager.py 프로젝트: xc0ut/PokeAlarm
 def load_alarms_file(self, file_path, max_attempts):
     log.info("Loading Alarms from the file at {}".format(file_path))
     try:
         with open(file_path, 'r') as f:
             alarm_settings = json.load(f)
         if type(alarm_settings) is not list:
             log.critical("Alarms file must be a list of Alarms objects - [ {...}, {...}, ... {...} ]")
             sys.exit(1)
         self.__alarms = []
         for alarm in alarm_settings:
             if parse_boolean(require_and_remove_key('active', alarm, "Alarm objects in Alarms file.")) is True:
                 _type = require_and_remove_key('type', alarm, "Alarm objects in Alarms file.")
                 self.set_optional_args(str(alarm))
                 if _type == 'discord':
                     from Discord import DiscordAlarm
                     self.__alarms.append(DiscordAlarm(alarm, max_attempts, self.__google_key))
                 elif _type == 'facebook_page':
                     from FacebookPage import FacebookPageAlarm
                     self.__alarms.append(FacebookPageAlarm(alarm))
                 elif _type == 'pushbullet':
                     from Pushbullet import PushbulletAlarm
                     self.__alarms.append(PushbulletAlarm(alarm))
                 elif _type == 'slack':
                     from Slack import SlackAlarm
                     self.__alarms.append(SlackAlarm(alarm, self.__google_key))
                 elif _type == 'telegram':
                     from Telegram import TelegramAlarm
                     self.__alarms.append(TelegramAlarm(alarm))
                 elif _type == 'twilio':
                     from Twilio import TwilioAlarm
                     self.__alarms.append(TwilioAlarm(alarm))
                 elif _type == 'twitter':
                     from Twitter import TwitterAlarm
                     self.__alarms.append(TwitterAlarm(alarm))
                 else:
                     log.error("Alarm type not found: " + alarm['type'])
                     log.error("Please consult the PokeAlarm documentation accepted Alarm Types")
                     sys.exit(1)
             else:
                 log.debug("Alarm not activated: " + alarm['type'] + " because value not set to \"True\"")
         log.info("{} active alarms found.".format(len(self.__alarms)))
         return  # all done
     except ValueError as e:
         log.error("Encountered error while loading Alarms file: {}: {}".format(type(e).__name__, e))
         log.error(
             "PokeAlarm has encountered a 'ValueError' while loading the Alarms file. This typically means your " +
             "file isn't in the correct json format. Try loading your file contents into a json validator.")
     except IOError as e:
         log.error("Encountered error while loading Alarms: {}: {}".format(type(e).__name__, e))
         log.error("PokeAlarm was unable to find a filters file at {}." +
                   "Please check that this file exists and PA has read permissions.").format(file_path)
     except Exception as e:
         log.error("Encountered error while loading Alarms: {}: {}".format(type(e).__name__, e))
     log.debug("Stack trace: \n {}".format(traceback.format_exc()))
     sys.exit(1)
예제 #2
0
 def create_alarms(self, file_path):
     with open(file_path, 'r') as f:
         alarm_settings = json.load(f)
     for alarm in alarm_settings:
         if parse_boolean(alarm['active']) is True:
             if alarm['type'] == 'boxcar':
                 from Boxcar import BoxcarAlarm
                 self.__alarms.append(BoxcarAlarm(alarm))
             elif alarm['type'] == 'discord':
                 from Discord import DiscordAlarm
                 self.__alarms.append(DiscordAlarm(alarm))
             elif alarm['type'] == 'facebook_page':
                 from FacebookPage import FacebookPageAlarm
                 self.__alarms.append(FacebookPageAlarm(alarm))
             elif alarm['type'] == 'pushbullet':
                 from Pushbullet import PushbulletAlarm
                 self.__alarms.append(PushbulletAlarm(alarm))
             elif alarm['type'] == 'pushover':
                 from Pushover import PushoverAlarm
                 self.__alarms.append(PushoverAlarm(alarm))
             elif alarm['type'] == 'slack':
                 from Slack import SlackAlarm
                 self.__alarms.append(SlackAlarm(alarm))
             elif alarm['type'] == 'telegram':
                 from Telegram import TelegramAlarm
                 self.__alarms.append(TelegramAlarm(alarm))
             elif alarm['type'] == 'twilio':
                 from Twilio import TwilioAlarm
                 self.__alarms.append(TwilioAlarm(alarm))
             elif alarm['type'] == 'twitter':
                 from Twitter import TwitterAlarm
                 self.__alarms.append(TwitterAlarm(alarm))
             else:
                 log.info("Alarm type not found: " + alarm['type'])
             self.set_optional_args(str(alarm))
         else:
             log.info("Alarm not activated: " + alarm['type'] +
                      " because value not set to \"True\"")