Best Python code snippet using localstack_python
events_listener.py
Source:events_listener.py
...51 json.dumps(event["event"]),52 )53def _get_events_tmp_dir():54 return os.path.join(config.TMP_FOLDER, EVENTS_TMP_DIR)55def get_scheduled_rule_func(data):56 def func(*args, **kwargs):57 rule_name = data.get("Name")58 client = aws_stack.connect_to_service("events")59 targets = client.list_targets_by_rule(Rule=rule_name)["Targets"]60 if targets:61 LOG.debug(62 "Notifying %s targets in response to triggered Events rule %s"63 % (len(targets), rule_name)64 )65 for target in targets:66 arn = target.get("Arn")67 event_str = target.get("Input") or "{}"68 event = json.loads(event_str)69 attr = aws_stack.get_events_target_attributes(target)70 try:71 send_event_to_target(arn, event, target_attributes=attr)72 except Exception as e:73 LOG.info(74 f"Unable to send event notification {truncate(event)} to target {target}: {e}"75 )76 return func77def convert_schedule_to_cron(schedule):78 """Convert Events schedule like "cron(0 20 * * ? *)" or "rate(5 minutes)" """79 cron_regex = r"\s*cron\s*\(([^\)]*)\)\s*"80 if re.match(cron_regex, schedule):81 cron = re.sub(cron_regex, r"\1", schedule)82 return cron83 rate_regex = r"\s*rate\s*\(([^\)]*)\)\s*"84 if re.match(rate_regex, schedule):85 rate = re.sub(rate_regex, r"\1", schedule)86 value, unit = re.split(r"\s+", rate.strip())87 if "minute" in unit:88 return "*/%s * * * *" % value89 if "hour" in unit:90 return "* */%s * * *" % value91 if "day" in unit:92 return "* * */%s * *" % value93 raise Exception("Unable to parse events schedule expression: %s" % schedule)94 return schedule95def handle_put_rule(data):96 schedule = data.get("ScheduleExpression")97 enabled = data.get("State") != "DISABLED"98 if schedule:99 job_func = get_scheduled_rule_func(data)100 cron = convert_schedule_to_cron(schedule)101 LOG.debug("Adding new scheduled Events rule with cron schedule %s" % cron)102 job_id = JobScheduler.instance().add_job(job_func, cron, enabled)103 rule_scheduled_jobs = EventsBackend.get().rule_scheduled_jobs104 rule_scheduled_jobs[data["Name"]] = job_id105 return True106def handle_delete_rule(rule_name):107 rule_scheduled_jobs = EventsBackend.get().rule_scheduled_jobs108 job_id = rule_scheduled_jobs.get(rule_name)109 if job_id:110 LOG.debug("Removing scheduled Events: {} | job_id: {}".format(rule_name, job_id))111 JobScheduler.instance().cancel_job(job_id=job_id)112def handle_disable_rule(rule_name):113 rule_scheduled_jobs = EventsBackend.get().rule_scheduled_jobs...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!