<commit_before>""" Command line configuration parser """
import sys
import os.path
import ConfigParser


def __parse_options(config_file, section, options):
    """ Parse the section options

    :type config_file: ConfigParser object
    :param config_file: The config file object to use
    :type section: str
    :param section: Which section to read in the configuration file
    :type options: list of dicts
    :param options:
        A list of options to parse. Example list::
        [{
            'key': 'aws_access_key_id',
            'option': 'aws-access-key-id',
            'required': False,
            'type': str
        }]
    :returns: dict
    """
    configuration = {}
    for option in options:
        try:
            if option.get('type') == 'str':
                configuration[option.get('key')] = \
                    config_file.get(section, option.get('option'))
            elif option.get('type') == 'int':
                configuration[option.get('key')] = \
                    config_file.getint(section, option.get('option'))
            elif option.get('type') == 'bool':
                configuration[option.get('key')] = \
                    config_file.getboolean(section, option.get('option'))
            else:
                configuration[option.get('key')] = \
                    config_file.get(section, option.get('option'))
        except ConfigParser.NoOptionError:
            if option.get('required'):
                print 'Missing [{0}] option "{1}" in configuration'.format(
                    section, option.get('option'))
                sys.exit(1)

    return configuration


def parse(config_path):
    """ Parse the configuration file

    :type config_path: str
    :param config_path: Path to the configuration file
    """
    config_path = os.path.expanduser(config_path)

    # Read the configuration file
    config_file = ConfigParser.SafeConfigParser()
    config_file.optionxform = lambda option: option
    config_file.read(config_path)

    #
    # Handle [global]
    #
    if 'global' in config_file.sections():
        global_config = __parse_options(
            config_file,
            'global',
            [
                {
                    'key': 'aws_access_key_id',
                    'option': 'aws-access-key-id',
                    'required': False,
                    'type': 'str'
                },
                {
                    'key': 'aws_secret_access_key',
                    'option': 'aws-secret-access-key-id',
                    'required': False,
                    'type': 'str'
                },
                {
                    'key': 'region',
                    'option': 'region',
                    'required': False,
                    'type': 'str'
                },
                {
                    'key': 'check_interval',
                    'option': 'check-interval',
                    'required': False,
                    'type': 'int'
                },
            ])

    #
    # Handle [logging]
    #
    if 'logging' in config_file.sections():
        logging_config = __parse_options(
            config_file,
            'logging',
            [
                {
                    'key': 'log_level',
                    'option': 'log-level',
                    'required': False,
                    'type': 'str'
                },
                {
                    'key': 'log_file',
                    'option': 'log-file',
                    'required': False,
                    'type': 'str'
                }
            ])

    #
    # Handle [table: ]
    #
    table_config = { 'tables': {} }

    # Find the first table definition
    found_table = False
    for current_section in config_file.sections():
        if current_section.rsplit(':', 1)[0] != 'table':
            continue

        found_table = True
        current_table_name = current_section.rsplit(':', 1)[1].strip()
        table_config['tables'][current_table_name] = __parse_options(
            config_file,
            current_section,
            [
                {
                    'key': 'reads_lower_threshold',
                    'option': 'reads-lower-threshold',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'reads_upper_threshold',
                    'option': 'reads-upper-threshold',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'increase_reads_with',
                    'option': 'increase-reads-with',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'decrease_reads_with',
                    'option': 'decrease-reads-with',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'writes_lower_threshold',
                    'option': 'writes-lower-threshold',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'writes_upper_threshold',
                    'option': 'writes-upper-threshold',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'increase_writes_with',
                    'option': 'increase-writes-with',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'decrease_writes_with',
                    'option': 'decrease-writes-with',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'min_provisioned_reads',
                    'option': 'min-provisioned-reads',
                    'required': False,
                    'type': 'int'
                },<commit_msg>Fixed minor config bug<commit_after>""" Command line configuration parser """
import sys
import os.path
import ConfigParser


def __parse_options(config_file, section, options):
    """ Parse the section options

    :type config_file: ConfigParser object
    :param config_file: The config file object to use
    :type section: str
    :param section: Which section to read in the configuration file
    :type options: list of dicts
    :param options:
        A list of options to parse. Example list::
        [{
            'key': 'aws_access_key_id',
            'option': 'aws-access-key-id',
            'required': False,
            'type': str
        }]
    :returns: dict
    """
    configuration = {}
    for option in options:
        try:
            if option.get('type') == 'str':
                configuration[option.get('key')] = \
                    config_file.get(section, option.get('option'))
            elif option.get('type') == 'int':
                configuration[option.get('key')] = \
                    config_file.getint(section, option.get('option'))
            elif option.get('type') == 'bool':
                configuration[option.get('key')] = \
                    config_file.getboolean(section, option.get('option'))
            else:
                configuration[option.get('key')] = \
                    config_file.get(section, option.get('option'))
        except ConfigParser.NoOptionError:
            if option.get('required'):
                print 'Missing [{0}] option "{1}" in configuration'.format(
                    section, option.get('option'))
                sys.exit(1)

    return configuration


def parse(config_path):
    """ Parse the configuration file

    :type config_path: str
    :param config_path: Path to the configuration file
    """
    config_path = os.path.expanduser(config_path)

    # Read the configuration file
    config_file = ConfigParser.SafeConfigParser()
    config_file.optionxform = lambda option: option
    config_file.read(config_path)

    #
    # Handle [global]
    #
    if 'global' in config_file.sections():
        global_config = __parse_options(
            config_file,
            'global',
            [
                {
                    'key': 'aws_access_key_id',
                    'option': 'aws-access-key-id',
                    'required': False,
                    'type': 'str'
                },
                {
                    'key': 'aws_secret_access_key',
                    'option': 'aws-secret-access-key-id',
                    'required': False,
                    'type': 'str'
                },
                {
                    'key': 'region',
                    'option': 'region',
                    'required': False,
                    'type': 'str'
                },
                {
                    'key': 'check_interval',
                    'option': 'check-interval',
                    'required': False,
                    'type': 'int'
                },
            ])

    #
    # Handle [logging]
    #
    if 'logging' in config_file.sections():
        logging_config = __parse_options(
            config_file,
            'logging',
            [
                {
                    'key': 'log_level',
                    'option': 'log-level',
                    'required': False,
                    'type': 'str'
                },
                {
                    'key': 'log_file',
                    'option': 'log-file',
                    'required': False,
                    'type': 'str'
                }
            ])

    #
    # Handle [table: ]
    #
    table_config = { 'tables': {} }

    # Find the first table definition
    found_table = False
    for current_section in config_file.sections():
        if current_section.rsplit(':', 1)[0] != 'table':
            continue

        found_table = True
        current_table_name = current_section.rsplit(':', 1)[1].strip()
        table_config['tables'][current_table_name] = __parse_options(
            config_file,
            current_section,
            [
                {
                    'key': 'reads_lower_threshold',
                    'option': 'reads-lower-threshold',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'reads_upper_threshold',
                    'option': 'reads-upper-threshold',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'increase_reads_with',
                    'option': 'increase-reads-with',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'decrease_reads_with',
                    'option': 'decrease-reads-with',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'increase_reads_unit',
                    'option': 'increase-reads-unit',
                    'required': True,
                    'type': 'str'
                },
                {
                    'key': 'decrease_reads_unit',
                    'option': 'decrease-reads-unit',
                    'required': True,
                    'type': 'str'
                },
                {
                    'key': 'writes_lower_threshold',
                    'option': 'writes-lower-threshold',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'writes_upper_threshold',
                    'option': 'writes-upper-threshold',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'increase_writes_with',
                    'option': 'increase-writes-with',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'decrease_writes_with',
                    'option': 'decrease-writes-with',
                    'required': False,
                    'type': 'int'
                },
                {
                    'key': 'increase_writes_unit',
                    'option': 'increase-writes-unit',
                    'required': True,
                    'type': 'str'
                },
                {
                    'key': 'decrease_writes_unit',
                    'option': 'decrease-writes-unit',
                    'required': True,
                    'type': 'str'
                },
                {
                    'key': 'min_provisioned_reads',
                    'option': 'min-provisioned-reads',
                    'required': False,
                    'type': 'int'
                },
{'fraction_non_alphanumeric': 0.118298592415968, 'fraction_numerical': 0.0010768402430582262, 'mean_word_length': 1.1093445814406229, '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': '3391450', 'n_tokens_mistral': 3404, 'n_tokens_neox': 3103, 'n_words': 657}