<commit_before># -*- coding: utf-8 -*-
'''
Connection module for Amazon CloudWatch

.. versionadded:: 2014.7.0

:configuration: This module accepts explicit credentials but can also utilize
    IAM roles assigned to the instance trough Instance Profiles. Dynamic
    credentials are then automatically obtained from AWS API and no further
    configuration is necessary. More Information available at::

       http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html

    If IAM roles are not used you need to specify them either in a pillar or
    in the minion's config file::

        cloudwatch.keyid: <KEY>
        cloudwatch.key: <KEY>

    A region may also be specified in the configuration::

        cloudwatch.region: us-east-1

    If a region is not specified, the default is us-east-1.

    It's also possible to specify key, keyid and region via a profile, either
    as a passed in dict, or as a string to pull from pillars or minion config:

        myprofile:
            keyid: <KEY>
            key: <KEY>
            region: us-east-1

:depends: boto
'''

# Import Python libs
import logging
import json
import yaml

import salt.utils.odict as odict

log = logging.getLogger(__name__)

# Import third party libs
try:
    import boto
    import boto.ec2.cloudwatch
    import boto.ec2.cloudwatch.listelement
    import boto.ec2.cloudwatch.dimension
    logging.getLogger('boto').setLevel(logging.CRITICAL)
    HAS_BOTO = True
except ImportError:
    HAS_BOTO = False

from salt._compat import string_types


def __virtual__():
    '''
    Only load if boto libraries exist.
    '''
    if not HAS_BOTO:
        return False
    return True


def get_alarm(name, region=None, key=None, keyid=None, profile=None):
    '''
    Get alarm details. Also can be used to check to see if an alarm exists.

    CLI example::

        salt myminion boto_cloudwatch.get_alarm myalarm region=us-east-1
    '''
    conn = _get_conn(region, key, keyid, profile)
    if not conn:
        return None
    alarms = conn.describe_alarms(alarm_names=[name])
    if len(alarms) == 0:
        return None
    if len(alarms) > 1:
        log.error("multiple alarms matched name '{0}'".format(name))
    return _metric_alarm_to_dict(alarms[0])


def _safe_dump(data):
    ###########################################
    # this presenter magic makes yaml.safe_dump
    # work with the objects returned from
    # boto.describe_alarms()
    ###########################################
    def ordered_dict_presenter(dumper, data):
        return dumper.represent_dict(data.items())

    yaml.add_representer(odict.OrderedDict, ordered_dict_presenter,
                         Dumper=yaml.dumper.SafeDumper)

    def boto_listelement_presenter(dumper, data):
        return dumper.represent_list(list(data))

    yaml.add_representer(boto.ec2.cloudwatch.listelement.ListElement,
                         boto_listelement_presenter,
                         Dumper=yaml.dumper.SafeDumper)

    def dimension_presenter(dumper, data):<commit_msg>Making salt.modules.boto_cloudwatch python3 compatible<commit_after># -*- coding: utf-8 -*-
'''
Connection module for Amazon CloudWatch

.. versionadded:: 2014.7.0

:configuration: This module accepts explicit credentials but can also utilize
    IAM roles assigned to the instance trough Instance Profiles. Dynamic
    credentials are then automatically obtained from AWS API and no further
    configuration is necessary. More Information available at::

       http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html

    If IAM roles are not used you need to specify them either in a pillar or
    in the minion's config file::

        cloudwatch.keyid: <KEY>
        cloudwatch.key: <KEY>

    A region may also be specified in the configuration::

        cloudwatch.region: us-east-1

    If a region is not specified, the default is us-east-1.

    It's also possible to specify key, keyid and region via a profile, either
    as a passed in dict, or as a string to pull from pillars or minion config:

        myprofile:
            keyid: <KEY>
            key: <KEY>
            region: us-east-1

:depends: boto
'''
from __future__ import absolute_import

# Import Python libs
import logging
import json
import yaml

import salt.utils.odict as odict

log = logging.getLogger(__name__)

# Import third party libs
try:
    import boto
    import boto.ec2.cloudwatch
    import boto.ec2.cloudwatch.listelement
    import boto.ec2.cloudwatch.dimension
    logging.getLogger('boto').setLevel(logging.CRITICAL)
    HAS_BOTO = True
except ImportError:
    HAS_BOTO = False

from salt._compat import string_types


def __virtual__():
    '''
    Only load if boto libraries exist.
    '''
    if not HAS_BOTO:
        return False
    return True


def get_alarm(name, region=None, key=None, keyid=None, profile=None):
    '''
    Get alarm details. Also can be used to check to see if an alarm exists.

    CLI example::

        salt myminion boto_cloudwatch.get_alarm myalarm region=us-east-1
    '''
    conn = _get_conn(region, key, keyid, profile)
    if not conn:
        return None
    alarms = conn.describe_alarms(alarm_names=[name])
    if len(alarms) == 0:
        return None
    if len(alarms) > 1:
        log.error("multiple alarms matched name '{0}'".format(name))
    return _metric_alarm_to_dict(alarms[0])


def _safe_dump(data):
    ###########################################
    # this presenter magic makes yaml.safe_dump
    # work with the objects returned from
    # boto.describe_alarms()
    ###########################################
    def ordered_dict_presenter(dumper, data):
        return dumper.represent_dict(list(data.items()))

    yaml.add_representer(odict.OrderedDict, ordered_dict_presenter,
                         Dumper=yaml.dumper.SafeDumper)

    def boto_listelement_presenter(dumper, data):
        return dumper.represent_list(list(data))

    yaml.add_representer(boto.ec2.cloudwatch.listelement.ListElement,
                         boto_listelement_presenter,
                         Dumper=yaml.dumper.SafeDumper)

    def dimension_presenter(dumper, data):
{'fraction_non_alphanumeric': 0.10631443298969072, 'fraction_numerical': 0.006926546391752577, 'mean_word_length': 3.0058064516129033, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '1532898', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1789, 'n_words': 556}