#!/usr/bin/env python
from __future__ import print_function, absolute_import, division

import boto3
import logging
import sys
import threading
from pprint import pformat

from .spotnik import Spotnik


def handler(*_):
    formatter = logging.Formatter(fmt="%(asctime)-15s %(levelname)s - %(name)s - %(message)s")
    for handler in logging.getLogger().handlers:
        handler.setFormatter(formatter)

    main()


def main():
    logger = logging.getLogger('spotnik')
    logger.setLevel(logging.INFO)

    ec2_client = boto3.client('ec2', region_name='eu-west-1')
    aws_region_names = [endpoint['RegionName'] for endpoint in ec2_client.describe_regions()['Regions']]
    for region_name in aws_region_names:
        logger.info("Starting thread for AWS region %s", region_name)
        regional_thread = threading.Thread(target=run_regional_thread, args=(region_name,))
        regional_thread.start()


def run_regional_thread(region_name):
    logger = logging.getLogger("spotnik." + region_name)
    spotnik_asgs = Spotnik.get_spotnik_asgs(region_name)
    logger.info("Found %d spotnik ASGs", len(spotnik_asgs))

    for asg in spotnik_asgs:
        asg_thread = threading.Thread(target=run_asg_thread, args=(region_name, asg))
        asg_thread.start()


def run_asg_thread(region_name, asg):
    logger = logging.getLogger("spotnik.%s.%s" % (region_name, asg['AutoScalingGroupName']))
    spotnik = Spotnik(region_name, asg, logger=logger)

    logger.info("Processing ASG with this config: \n%s", pformat(asg))
    spot_request, spot_instance_id = spotnik.get_pending_spot_resources()
    if spot_instance_id:
        logger.info("Instance %r is ready to be attached to ASG", spot_instance_id)
        spotnik.attach_spot_instance(spot_instance_id, spot_request)
        spotnik.untag_spot_request(spot_request)
    elif spot_request:
        logger.info("ASG has pending spot request %r.", spot_request['SpotInstanceRequestId'])
        # Amazon processing our request, but no instance yet
        return
    else:
        spotnik.make_spot_request()


if __name__ == "__main__":
    main()
    sys.exit(0)

@@ -419,92 +419,31 @@
 def 
-main():%0A    logger = logging.getLogger('spotnik')%0A    logger.setLevel(logging.INFO)%0A
+get_aws_region_names():
 %0A   
@@ -509,26 +509,14 @@
     
-aws_region_names =
+return
  %5Ben
@@ -594,16 +594,107 @@
 ions'%5D%5D%0A
+%0A%0Adef main():%0A    logger = logging.getLogger('spotnik')%0A    logger.setLevel(logging.INFO)%0A%0A
     for 
@@ -708,16 +708,20 @@
 name in 
+get_
 aws_regi
@@ -728,16 +728,18 @@
 on_names
+()
 :%0A      

Create get_aws_region_names() for better testability