import importlib.util

from ec2mc import consts

def main(instance, new_ip):
    """pass along instance info to handler under config's ip_handlers"""
    if consts.USE_HANDLER is False:
        return
    if 'IpHandler' not in instance['tags']:
        return

    handler_base = instance['tags']['IpHandler']
    handler_path = consts.IP_HANDLER_DIR / handler_base
    if not handler_path.is_file():
        print(f"  {handler_base} not found from config's ip_handlers.")
        return

    handler = _load_script(handler_path)
    if handler is not None:
        handler.main(instance['name'], new_ip)


def _load_script(script_path):
    """load python script"""
    try:
        spec = importlib.util.spec_from_file_location("handler", script_path)
        handler = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(handler)
        return handler
    except ImportError as e:
        handler_base = script_path.name
        print(f"  {e.name} package required by {handler_base} not found.")
        print(f"    Install with \"python -m pip install {e.name}\".")
    return None

import importlib.util

from ec2mc import consts

def main(instance, new_ip):
    """pass along instance info to handler under config's ip_handlers"""
    if consts.USE_HANDLER is False:
        return
    if 'IpHandler' not in instance['tags']:
        return

    handler_base = instance['tags']['IpHandler']
    handler_path = consts.IP_HANDLER_DIR / handler_base
    if not handler_path.is_file():
        print(f"  {handler_base} not found from config's ip_handlers.")
        return

    handler = _load_script(handler_path)
    if handler is not None:
        handler.main(instance['name'], new_ip)


def _load_script(script_path):
    """load python script"""
    try:
        spec = importlib.util.spec_from_file_location("handler", script_path)
        handler = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(handler)
        return handler
    except ImportError as e:
        handler_base = script_path.name
        print(f"  {e.name} package required by {handler_base} not found.")
        print(f"    Install with \"python -m pip install {e.name}\".")
    return None

pass along instance info to handler under config's ip_handlers load python script