def load_filter_file(self, file_path): try: log.info("Loading Filters from file at {}".format(file_path)) with open(file_path, 'r') as f: filters = json.load(f) if type(filters) is not dict: log.critical("Filters file's must be a JSON object: { \"pokemon\":{...},... }") # Load in the Pokemon Section self.__pokemon_settings = load_pokemon_section( require_and_remove_key('pokemon', filters, "Filters file.")) # Load in the Pokestop Section self.__pokestop_settings = load_pokestop_section( require_and_remove_key('pokestops', filters, "Filters file.")) # Load in the Gym Section self.__gym_settings = load_gym_section( require_and_remove_key('gyms', filters, "Filters file.")) return except ValueError as e: log.error("Encountered error while loading Filters: {}: {}".format(type(e).__name__, e)) log.error( "PokeAlarm has encountered a 'ValueError' while loading the Filters 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 Filters: {}: {}".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 Filters: {}: {}".format(type(e).__name__, e)) log.debug("Stack trace: \n {}".format(traceback.format_exc())) sys.exit(1)
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)
def parse_alarms_file(manager, filename): try: filepath = utils.get_path(filename) log.info("Loading Alarms from file at {}".format(filepath)) with open(filepath, 'r') as f: alarm_settings = json.load(f, object_pairs_hook=OrderedDict) if type(alarm_settings) is not OrderedDict: log.critical("Alarms file must be an object of Alarms objects " + "- { 'alarm1': {...}, ... 'alarm5': {...} }") sys.exit(1) except ValueError as e: log.error("Encountered error while loading Alarms:" " {}: {}".format(type(e).__name__, e)) log.error( "PokeAlarm has encountered a 'ValueError' while loading the " "Alarms file. This typically means the file isn't in the " "correct json format. Try loading the file contents into a " "json validator.") log.debug("Stack trace: \n {}".format(traceback.format_exc())) sys.exit(1) try: for name, alarm in alarm_settings.iteritems(): active = parse_boolean(require_and_remove_key( 'active', alarm, "Alarm objects in file.")) if active: manager.add_alarm(name, alarm) else: log.debug("%s alarm ignored: active is set to 'false'", name) 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)
def parse_alarms_file(manager, filename): try: filepath = utils.get_path(filename) log.info("Loading Alarms from file at {}".format(filepath)) with open(filepath, 'r') as f: alarm_settings = json.load(f, object_pairs_hook=OrderedDict) if type(alarm_settings) is not OrderedDict: log.critical("Alarms file must be an object of Alarms objects " + "- { 'alarm1': {...}, ... 'alarm5': {...} }") sys.exit(1) except ValueError as e: log.error("Encountered error while loading Alarms:" " {}: {}".format(type(e).__name__, e)) log.error("PokeAlarm has encountered a 'ValueError' while loading the " "Alarms file. This typically means the file isn't in the " "correct json format. Try loading the file contents into a " "json validator.") log.debug("Stack trace: \n {}".format(traceback.format_exc())) sys.exit(1) try: for name, alarm in alarm_settings.iteritems(): active = parse_boolean( require_and_remove_key('active', alarm, "Alarm objects in file.")) if active: manager.add_alarm(name, alarm) else: log.debug("%s alarm ignored: active is set to 'false'", name) 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)
def load_pokestop_section(settings): log.info("Setting Pokestop filters...") # Set the defaults settings for "True" default_true = {"min_dist": 0.0, "max_dist": float('inf')} stop = { "enabled": bool(parse_boolean(require_and_remove_key('enabled', settings, 'Pokestops')) or False), "filters": create_multi_filter('Pokestops --> filters', PokestopFilter, settings.pop('filters', "False"), default_true) } reject_leftover_parameters(settings, "Pokestops section of Filters file.") for filt in stop['filters']: log.debug("Between {} and {} away.".format( get_dist_as_str(filt.min_dist), get_dist_as_str(filt.max_dist))) if stop['enabled'] is False: log.info("Pokestop notifications will NOT be sent - Enabled is False.") return stop
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 dict: log.critical( "Alarms file must be an object of Alarms objects " + "- { 'alarm1': {...}, ... 'alarm5': {...} }") sys.exit(1) self.__alarms = {} for name, alarm in alarm_settings.iteritems(): if parse_boolean( require_and_remove_key( 'active', alarm, "Alarm objects in file.")) is True: self.set_optional_args(str(alarm)) self.__alarms[name] = Alarms.alarm_factory( alarm, max_attempts, self.__google_key) else: log.debug("Alarm not activated: {}".format(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".format(file_path) + " exists and PA has read permissions.") 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)