<commit_before># Copyright 2014 Cloudbase Solutions Srl
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import collections
import itertools

import six


_SENTINEL = object()


class _ConfigParser(six.moves.configparser.ConfigParser):
    def getlist(self, section, option):
        value = self.get(section, option)
        values = value.splitlines()
        iters = (map(str.strip, filter(None, value.split(",")))
                 for value in values)
        return list(itertools.chain.from_iterable(iters))

    # Don't lowercase.
    optionxform = str


def _get_default(parser, section, option, default=None):
    try:
        return parser.get(section, option)
    except six.moves.configparser.NoOptionError:
        return default


class _Option(object):
    def __init__(self, option, method='get', default=_SENTINEL):
        self._option = option
        self._method = method
        self._default = default

    def __get__(self, instance, owner=None):
        # pylint: disable=protected-access
        return instance._get_option(self._option,
                                    self._method,
                                    self._default)


class ConfigurationParser(object):
    """A parser class which knows how to parse argus configurations."""

    def __init__(self, filename):
        self._filename = filename
        self._parser = _ConfigParser()
        self._parser.read(self._filename)

    @property
    def argus(self):
        # Get the argus section
        argus = collections.namedtuple('argus',
                                       'resources debug path_to_private_key '
                                       'file_log log_format dns_nameservers '
                                       'output_directory build arch '
                                       'patch_install git_command')
        resources = _get_default(
            self._parser, 'argus', 'resources',
            'https://raw.githubusercontent.com/PCManticore/'
            'argus-ci/master/argus/resources')
        debug = self._parser.getboolean('argus', 'debug')
        path_to_private_key = self._parser.get('argus', 'path_to_private_key')
        file_log = _get_default(self._parser, 'argus', 'file_log')
        log_format = _get_default(self._parser, 'argus', 'log_format')
        dns_nameservers = _get_default(
            self._parser, 'argus', 'dns_nameservers',
            ['8.8.8.8', '8.8.4.4'])
        if not isinstance(dns_nameservers, list):
            # pylint: disable=no-member
            dns_nameservers = dns_nameservers.split(",")
        output_directory = _get_default(self._parser, 'argus', 'output_directory')
        build = _get_default(self._parser, 'argus', 'build', 'msi')
        arch = _get_default(self._parser, 'argus', 'arch', 'x64')
        patch_install = _get_default(self._parser, 'argus', 'patch_install')
        git_command = _get_default(self._parser, 'argus', 'git_command')

        return argus(resources, debug, path_to_private_key,
                     file_log, log_format, dns_nameservers,
                     output_directory, build, arch, patch_install,
                     git_command)

    @property
    def cloudbaseinit(self):
        cloudbaseinit = collections.namedtuple(
            'cloudbaseinit',
            'expected_plugins_count created_user group')

        try:
            expected_plugins_count = self._parser.getint(
                'cloudbaseinit',
                'expected_plugins_count')
        except (six.moves.configparser.NoOptionError, ValueError):
            expected_plugins_count = 13

        group = self._parser.get('cloudbaseinit', 'group')
        created_user = self._parser.get('cloudbaseinit', 'created_user')

        return cloudbaseinit(expected_plugins_count, created_user, group)

    @property
    def openstack(self):
        openstack = collections.namedtuple(
            'openstack',
            'image_ref flavor_ref image_username image_password'
            'image_os_type')
        image_ref = self._parser.get('openstack', 'image_ref')
        flavor_ref = self._parser.get('openstack', 'flavor_ref')
        image_username = self._parser.get('openstack', 'image-username')
        image_password = self._parser.get('openstack', 'image_password')
        image_os_type = self._parser.get('openstack', 'image_os_type')

        return openstack(image_ref, flavor_ref, image_username,
                         image_password, image_os_type)

    @property
    def conf(self):
        conf = collections.namedtuple(
            'conf', 'argus cloudbaseinit openstack')
        return conf(self.argus, self.cloudbaseinit, self.openstack)<commit_msg>fixed type when parsing image_username from config<commit_after># Copyright 2014 Cloudbase Solutions Srl
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import collections
import itertools

import six


_SENTINEL = object()


class _ConfigParser(six.moves.configparser.ConfigParser):
    def getlist(self, section, option):
        value = self.get(section, option)
        values = value.splitlines()
        iters = (map(str.strip, filter(None, value.split(",")))
                 for value in values)
        return list(itertools.chain.from_iterable(iters))

    # Don't lowercase.
    optionxform = str


def _get_default(parser, section, option, default=None):
    try:
        return parser.get(section, option)
    except six.moves.configparser.NoOptionError:
        return default


class _Option(object):
    def __init__(self, option, method='get', default=_SENTINEL):
        self._option = option
        self._method = method
        self._default = default

    def __get__(self, instance, owner=None):
        # pylint: disable=protected-access
        return instance._get_option(self._option,
                                    self._method,
                                    self._default)


class ConfigurationParser(object):
    """A parser class which knows how to parse argus configurations."""

    def __init__(self, filename):
        self._filename = filename
        self._parser = _ConfigParser()
        self._parser.read(self._filename)

    @property
    def argus(self):
        # Get the argus section
        argus = collections.namedtuple('argus',
                                       'resources debug path_to_private_key '
                                       'file_log log_format dns_nameservers '
                                       'output_directory build arch '
                                       'patch_install git_command')
        resources = _get_default(
            self._parser, 'argus', 'resources',
            'https://raw.githubusercontent.com/PCManticore/'
            'argus-ci/master/argus/resources')
        debug = self._parser.getboolean('argus', 'debug')
        path_to_private_key = self._parser.get('argus', 'path_to_private_key')
        file_log = _get_default(self._parser, 'argus', 'file_log')
        log_format = _get_default(self._parser, 'argus', 'log_format')
        dns_nameservers = _get_default(
            self._parser, 'argus', 'dns_nameservers',
            ['8.8.8.8', '8.8.4.4'])
        if not isinstance(dns_nameservers, list):
            # pylint: disable=no-member
            dns_nameservers = dns_nameservers.split(",")
        output_directory = _get_default(self._parser, 'argus', 'output_directory')
        build = _get_default(self._parser, 'argus', 'build', 'msi')
        arch = _get_default(self._parser, 'argus', 'arch', 'x64')
        patch_install = _get_default(self._parser, 'argus', 'patch_install')
        git_command = _get_default(self._parser, 'argus', 'git_command')

        return argus(resources, debug, path_to_private_key,
                     file_log, log_format, dns_nameservers,
                     output_directory, build, arch, patch_install,
                     git_command)

    @property
    def cloudbaseinit(self):
        cloudbaseinit = collections.namedtuple(
            'cloudbaseinit',
            'expected_plugins_count created_user group')

        try:
            expected_plugins_count = self._parser.getint(
                'cloudbaseinit',
                'expected_plugins_count')
        except (six.moves.configparser.NoOptionError, ValueError):
            expected_plugins_count = 13

        group = self._parser.get('cloudbaseinit', 'group')
        created_user = self._parser.get('cloudbaseinit', 'created_user')

        return cloudbaseinit(expected_plugins_count, created_user, group)

    @property
    def openstack(self):
        openstack = collections.namedtuple(
            'openstack',
            'image_ref flavor_ref image_username image_password'
            'image_os_type')
        image_ref = self._parser.get('openstack', 'image_ref')
        flavor_ref = self._parser.get('openstack', 'flavor_ref')
        image_username = self._parser.get('openstack', 'image_username')
        image_password = self._parser.get('openstack', 'image_password')
        image_os_type = self._parser.get('openstack', 'image_os_type')

        return openstack(image_ref, flavor_ref, image_username,
                         image_password, image_os_type)

    @property
    def conf(self):
        conf = collections.namedtuple(
            'conf', 'argus cloudbaseinit openstack')
        return conf(self.argus, self.cloudbaseinit, self.openstack)
{'fraction_non_alphanumeric': 0.09709385421629348, 'fraction_numerical': 0.003811338732729871, 'mean_word_length': 2.4594594594594597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1148690', 'n_tokens_mistral': 2934, 'n_tokens_neox': 2869, 'n_words': 685}