<commit_before>#!/usr/bin/env python
from docopt import docopt
import sys

from cloudmesh.cm_mongo import cm_mongo
from cloudmesh.config.cm_config import cm_config
from prettytable import PrettyTable

from cloudmesh_common.logger import LOGGER
from tabulate import tabulate

log = LOGGER(__file__)

def shell_command_flavor(arguments):
    """
    Usage:
        flavor 
        flavor <cm_cloud>... [--refresh]
	flavor -h | --help
        flavor --version

   Options:
       -h                   help message
       --refresh            refresh flavors of IaaS
 
    Arguments:
        cm_cloud    Name of the IaaS cloud e.g. india_openstack_grizzly.
    
    Description:
       flavor command provides list of available flavors. Flavor describes
       virtual hardware configurations such as size of memory, disk, cpu cores.

    Result:

    Examples:
        $ flavor india_openstack_grizzly
        
    """

    #log.info(arguments)
    cloud_names = arguments['<cm_cloud>']
    # clouds in c.flavors treats None value as a ALL clouds
    if not cloud_names:
        cloud_names = None
    config = cm_config()
    username = config.username()
    c = cm_mongo()
    c.activate(cm_user_id=username)
    if arguments['--refresh']:
        c.refresh(cm_user_id=username, names=cloud_names, types=['flavors'])
    flavors_dict = c.flavors(cm_user_id=username, clouds=cloud_names)

    your_keys = [
        'id',
        'name',
        'vcpus',
        'ram',
        'disk',
        'cm_refresh',
    ]

    flavors = _select_flavors(flavors_dict, your_keys)

    _display(flavors)
    
def _display(json_data, headers="firstrow", tablefmt="orgtbl"):
    table = tabulate(json_data, headers, tablefmt)
    try:
        separator = table.split("\n")[1].replace("|", "+")
    except:
        separator = "-" * 50
    print separator
    print table
    print separator

def _select_flavors(data, selected_keys, env=[]):
    '''
    {"india_openstack_havana": {"1": 
        {"cm_cloud_type": "openstack", 
        "disk": 0,
        "name": "m1.tiny", 
        "links": [{"href":
            "http://172.16.31.10:8774/v1.1/8bc7e259464944b3bf4d8b050d1ab935/flavors/1",
            "rel": "self"}, {"href": "http://172.16.31.10
            :8774/8bc7e259464944b3bf4d8b050d1ab935/flavors/1", "rel": "bookmark"}],
        "ram": 512, 
        "cm_type_version": "havana", 
        "id": "1",
        "OS-FLV-DISABLED:disabled": false, 
        "cm_id": "india_openstack_havana-flavors-m1-tiny", 
        "vcpus": 1, 
        "cm_refresh": "2014-06-14T17-21-25Z", 
        "swap": "", 
        "os-flavor-access:is_public": true, 
        "rxtx_factor": 1.0, 
        "cm_kind": "flavors"}}}
    '''
    headers = ["cm_cloud"] + selected_keys
    flavors = [headers]
    for cm_cloud, _id in data.iteritems():
        for flavor_name, v in _id.iteritems():
            values = [cm_cloud]
            for k in selected_keys:
                try:
                    values.append(v[k])
                except:
                    values.append(0)
            flavors.append(values)
    return flavors

def main():
    arguments = docopt(shell_command_flavor.__doc__)
    shell_command_flavor(arguments)
        
if __name__ == "__main__":
    #print sys.argv
    main()<commit_msg>variable name capitalized like others<commit_after>#!/usr/bin/env python
from docopt import docopt
import sys

from cloudmesh.cm_mongo import cm_mongo
from cloudmesh.config.cm_config import cm_config
from prettytable import PrettyTable

from cloudmesh_common.logger import LOGGER
from tabulate import tabulate

log = LOGGER(__file__)

def shell_command_flavor(arguments):
    """
    Usage:
        flavor 
        flavor CLOUD... [--refresh]
	flavor -h | --help
        flavor --version

   Options:
       -h                   help message
       --refresh            refresh flavors of IaaS
 
    Arguments:
        CLOUD    Name of the IaaS cloud e.g. india_openstack_grizzly.
    
    Description:
       flavor command provides list of available flavors. Flavor describes
       virtual hardware configurations such as size of memory, disk, cpu cores.

    Result:

    Examples:
        $ flavor india_openstack_grizzly
        
    """

    #log.info(arguments)
    cloud_names = arguments['CLOUD']
    # clouds in c.flavors treats None value as a ALL clouds
    if not cloud_names:
        cloud_names = None
    config = cm_config()
    username = config.username()
    c = cm_mongo()
    c.activate(cm_user_id=username)
    if arguments['--refresh']:
        c.refresh(cm_user_id=username, names=cloud_names, types=['flavors'])
    flavors_dict = c.flavors(cm_user_id=username, clouds=cloud_names)

    your_keys = [
        'id',
        'name',
        'vcpus',
        'ram',
        'disk',
        'cm_refresh',
    ]

    flavors = _select_flavors(flavors_dict, your_keys)

    _display(flavors)
    
def _display(json_data, headers="firstrow", tablefmt="orgtbl"):
    table = tabulate(json_data, headers, tablefmt)
    try:
        separator = table.split("\n")[1].replace("|", "+")
    except:
        separator = "-" * 50
    print separator
    print table
    print separator

def _select_flavors(data, selected_keys, env=[]):
    '''
    {"india_openstack_havana": {"1": 
        {"cm_cloud_type": "openstack", 
        "disk": 0,
        "name": "m1.tiny", 
        "links": [{"href":
            "http://149.165.146.57:8774/v1.1/8bc7e259464944b3bf4d8b050d1ab935/flavors/1",
            "rel": "self"}, {"href": "http://172.16.31.10
            :8774/8bc7e259464944b3bf4d8b050d1ab935/flavors/1", "rel": "bookmark"}],
        "ram": 512, 
        "cm_type_version": "havana", 
        "id": "1",
        "OS-FLV-DISABLED:disabled": false, 
        "cm_id": "india_openstack_havana-flavors-m1-tiny", 
        "vcpus": 1, 
        "cm_refresh": "2014-06-14T17-21-25Z", 
        "swap": "", 
        "os-flavor-access:is_public": true, 
        "rxtx_factor": 1.0, 
        "cm_kind": "flavors"}}}
    '''
    headers = ["CLOUD"] + selected_keys
    flavors = [headers]
    for cm_cloud, _id in data.iteritems():
        for flavor_name, v in _id.iteritems():
            values = [cm_cloud]
            for k in selected_keys:
                try:
                    values.append(v[k])
                except:
                    values.append(0)
            flavors.append(values)
    return flavors

def main():
    arguments = docopt(shell_command_flavor.__doc__)
    shell_command_flavor(arguments)
        
if __name__ == "__main__":
    #print sys.argv
    main()
{'fraction_non_alphanumeric': 0.12138728323699421, 'fraction_numerical': 0.031031335564344387, 'mean_word_length': 2.27930174563591, 'pattern_counts': {'":': 44, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '2199044', 'n_tokens_mistral': 2320, 'n_tokens_neox': 2177, 'n_words': 465}