<commit_before>from __future__ import print_function

import boto
import boto.ec2
from boto.exception import EC2ResponseError
from monocyte.handler import Resource, aws_handler


@aws_handler
class Instance(object):
    VALID_TARGET_STATES = ["terminated", "shutting-down"]

    def __init__(self, region_filter, dry_run=True):
        self.regions = [region for region in boto.ec2.regions() if region_filter(region.name)]
        self.dry_run = dry_run
        self.name = "ec2.instance"
        self.order = 2

    def fetch_unwanted_resources(self):
        for region in self.regions:
            connection = boto.ec2.connect_to_region(region.name)
            resources = connection.get_only_instances() or []
            for resource in resources:
                yield Resource(resource, region.name)

    def to_string(self, resource):
        return "ec2 instance found in {region.name}\n\t" \
               "{id} [{image_id}] - {instance_type}, since {launch_time}" \
               "\n\tdnsname {public_dns_name}, key {key_name}, state {_state}".format(**vars(resource.wrapped))

    def delete(self, resource):
        if resource.wrapped.state in Instance.VALID_TARGET_STATES:
            print("\tstate '{}' is a valid target state ({}), skipping".format(
                resource.wrapped.state, ", ".join(Instance.VALID_TARGET_STATES)))
            return []
        connection = boto.ec2.connect_to_region(resource.region)
        if self.dry_run:
            try:
                connection.terminate_instances([resource.wrapped.id], dry_run=True)
            except EC2ResponseError as e:
                if e.status == 412:  # Precondition Failed
                    print("\tTermination {message}".format(**vars(e)))
                    return [resource.wrapped]
                raise
        else:
            instances = connection.terminate_instances([resource.wrapped.id], dry_run=False)
            print("\tInitiating shutdown sequence for {0}".format(instances))
            return instances


@aws_handler
class Volume(object):
    def __init__(self, region_filter, dry_run=True):
        self.regions = [region for region in boto.ec2.regions() if region_filter(region.name)]
        self.dry_run = dry_run
        self.name = "ec2.volume"
        self.order = 3

    def fetch_unwanted_resources(self):
        for region in self.regions:
            connection = boto.ec2.connect_to_region(region.name)
            resources = connection.get_all_volumes() or []
            for resource in resources:
                yield Resource(resource, region.name)

    def to_string(self, resource):
        return "ebs volume found in {region.name}\n\t" \
               "{id} {status}, since {create_time}".format(**vars(resource.wrapped))

    def delete(self, resource):
        connection = boto.ec2.connect_to_region(resource.region)
        print(vars(resource.wrapped))
        if self.dry_run:
            try:
                connection.delete_volume(resource.wrapped.id, dry_run=True)
            except EC2ResponseError as e:
                if e.status == 412:  # Precondition Failed
                    print("\tTermination {message}".format(**vars(e)))
                    return [resource.wrapped]
                raise
        else:
            print("\tInitiating deletion of EBS volume {0}".format(resource.wrapped.id))
            connection.delete_volume(resource.wrapped.id, dry_run=False)
<commit_msg>make flake8 happy again<commit_after>from __future__ import print_function

import boto
import boto.ec2
from boto.exception import EC2ResponseError
from monocyte.handler import Resource, aws_handler


@aws_handler
class Instance(object):
    VALID_TARGET_STATES = ["terminated", "shutting-down"]

    def __init__(self, region_filter, dry_run=True):
        self.regions = [region for region in boto.ec2.regions() if region_filter(region.name)]
        self.dry_run = dry_run
        self.name = "ec2.instance"
        self.order = 2

    def fetch_unwanted_resources(self):
        for region in self.regions:
            connection = boto.ec2.connect_to_region(region.name)
            resources = connection.get_only_instances() or []
            for resource in resources:
                yield Resource(resource, region.name)

    def to_string(self, resource):
        return "ec2 instance found in {region.name}\n\t" \
               "{id} [{image_id}] - {instance_type}, since {launch_time}" \
               "\n\tdnsname {public_dns_name}, key {key_name}, state {_state}".format(**vars(resource.wrapped))

    def delete(self, resource):
        if resource.wrapped.state in Instance.VALID_TARGET_STATES:
            print("\tstate '{}' is a valid target state ({}), skipping".format(
                resource.wrapped.state, ", ".join(Instance.VALID_TARGET_STATES)))
            return []
        connection = boto.ec2.connect_to_region(resource.region)
        if self.dry_run:
            try:
                connection.terminate_instances([resource.wrapped.id], dry_run=True)
            except EC2ResponseError as e:
                if e.status == 412:  # Precondition Failed
                    print("\tTermination {message}".format(**vars(e)))
                    return [resource.wrapped]
                raise
        else:
            instances = connection.terminate_instances([resource.wrapped.id], dry_run=False)
            print("\tInitiating shutdown sequence for {0}".format(instances))
            return instances


@aws_handler
class Volume(object):
    def __init__(self, region_filter, dry_run=True):
        self.regions = [region for region in boto.ec2.regions() if region_filter(region.name)]
        self.dry_run = dry_run
        self.name = "ec2.volume"
        self.order = 3

    def fetch_unwanted_resources(self):
        for region in self.regions:
            connection = boto.ec2.connect_to_region(region.name)
            resources = connection.get_all_volumes() or []
            for resource in resources:
                yield Resource(resource, region.name)

    def to_string(self, resource):
        return "ebs volume found in {region.name}\n\t" \
               "{id} {status}, since {create_time}".format(**vars(resource.wrapped))

    def delete(self, resource):
        connection = boto.ec2.connect_to_region(resource.region)
        print(vars(resource.wrapped))
        if self.dry_run:
            try:
                connection.delete_volume(resource.wrapped.id, dry_run=True)
            except EC2ResponseError as e:
                if e.status == 412:  # Precondition Failed
                    print("\tTermination {message}".format(**vars(e)))
                    return [resource.wrapped]
                raise
        else:
            print("\tInitiating deletion of EBS volume {0}".format(resource.wrapped.id))
            connection.delete_volume(resource.wrapped.id, dry_run=False)
{'fraction_non_alphanumeric': 0.10517166449369839, 'fraction_numerical': 0.006808633927277995, 'mean_word_length': 2.38265556099951, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6801719', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1918, 'n_words': 431}