<commit_before>#!/usr/bin/env python3
import sys
import argparse
import boto3

# --------------------------------------------------------------------------------
# other functions 
# --------------------------------------------------------------------------------

def getInstanceName(instance):
    for tag in instance.tags:
        if tag['Key'] == 'Name':
            return tag['Value']
    return ''

def printIfMatchOrEmpty(ins, cond, out):
    if cond == '' or ins == cond:
        print(out)

# --------------------------------------------------------------------------------
# sub commands
# --------------------------------------------------------------------------------
def ids(ns):
    ec2 = boto3.resource('ec2')
    for instance in ec2.instances.all():
        printIfMatchOrEmpty(getInstanceName(instance), ns.name, instance.instance_id)

def names(ns):
    ec2 = boto3.resource('ec2')
    for instance in ec2.instances.all():
        name = ''
        for tag in instance.tags:
            if tag['Key'] == 'Name':
                name = tag['Value']
                break
        print(name)

def status(ns):
    ec2 = boto3.resource('ec2')
    for instance in ec2.instances.all():
        name = getInstanceName(instance)
        print("id : {0}, {2} ({1})".format(
            instance.instance_id,
            name,
            instance.state['Name']
            ))

# --------------------------------------------------------------------------------
# arg parse
# --------------------------------------------------------------------------------

parser = argparse.ArgumentParser(description='ec2 utils')
parser.set_defaults(target='')

sub_parsers =  parser.add_subparsers(title='subcommands')

parser_names = sub_parsers.add_parser('names')
parser_names.set_defaults(target='names')

parser_ids = sub_parsers.add_parser('ids')
parser_ids.set_defaults(target='ids')
parser_ids.add_argument('--name')

parser_status = sub_parsers.add_parser('status')
parser_status.set_defaults(target='status')

# --------------------------------------------------------------------------------
# main
# --------------------------------------------------------------------------------
def main():
    namespace = parser.parse_args()
    
    if namespace.target == 'names':
        names(namespace)
    elif namespace.target == 'ids':
        ids(namespace)
    elif namespace.target == 'status':
        status(namespace)

if __name__ == '__main__':
    main()
<commit_msg>add ip_pub subcommand to ec2.py<commit_after>#!/usr/bin/env python3
import sys
import argparse
import boto3

# --------------------------------------------------------------------------------
# arg parse
# --------------------------------------------------------------------------------

parser = argparse.ArgumentParser(description='ec2 utils')
parser.set_defaults(target='')

sub_parsers =  parser.add_subparsers(title='sub commands')

# --------------------------------------------------------------------------------
# other functions 
# --------------------------------------------------------------------------------

def getInstanceName(instance):
    for tag in instance.tags:
        if tag['Key'] == 'Name':
            return tag['Value']
    return None

def printIfMatchOrEmpty(ins, cond, out):
    if cond == None or ins == cond:
        print(out)

# --------------------------------------------------------------------------------
# sub commands
# --------------------------------------------------------------------------------
def ids(ns):
    ec2 = boto3.resource('ec2')
    for instance in ec2.instances.all():
        printIfMatchOrEmpty(getInstanceName(instance), ns.name, instance.instance_id)

parser_ids = sub_parsers.add_parser('ids')
parser_ids.set_defaults(target='ids')
parser_ids.add_argument('--name')

def names(ns):
    ec2 = boto3.resource('ec2')
    for instance in ec2.instances.all():
        name = ''
        for tag in instance.tags:
            if tag['Key'] == 'Name':
                name = tag['Value']
                break
        print(name)

parser_names = sub_parsers.add_parser('names')
parser_names.set_defaults(target='names')

def status(ns):
    ec2 = boto3.resource('ec2')
    for instance in ec2.instances.all():
        name = getInstanceName(instance)
        print("id : {0}, {2} ({1})".format(
            instance.instance_id,
            name,
            instance.state['Name']
            ))

parser_status = sub_parsers.add_parser('status')
parser_status.set_defaults(target='status')

def ip_pub(ns):
    ec2 = boto3.resource('ec2')
    for instance in ec2.instances.all():
        printIfMatchOrEmpty(getInstanceName(instance), ns.name, instance.public_ip_address)

parser_ids = sub_parsers.add_parser('ip_pub')
parser_ids.set_defaults(target='ip_pub')
parser_ids.add_argument('--name')

# --------------------------------------------------------------------------------
# main
# --------------------------------------------------------------------------------
def main():
    namespace = parser.parse_args()
    
    if namespace.target == 'names':
        names(namespace)
    elif namespace.target == 'ids':
        ids(namespace)
    elif namespace.target == 'status':
        status(namespace)
    elif namespace.target == 'ip_pub':
        ip_pub(namespace)
    else:
        parser.print_help()

if __name__ == '__main__':
    main()

{'fraction_non_alphanumeric': 0.34125510582992946, 'fraction_numerical': 0.007612328258447828, 'mean_word_length': 1.3370932754880693, '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': '3640555', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1335, 'n_words': 270}