<commit_before>#!/usr/bin/env python
"""
_configuration_

Util class to read and access the cirrus.conf file
that defines how the various commands operate.

Commands should access this by doing

from cirrus.configuration import load_configuration
conf = load_configuration()

"""
import os
import gitconfig
import subprocess
import ConfigParser


class Configuration(dict):
    """
    _Configuration_

    """
    def __init__(self, config_file):
        super(Configuration, self).__init__(self)
        self.config_file = config_file
        self.parser = None

    def load(self):
        """
        _load_from_file_

        Reread the cirrus config file

        """
        self.parser = ConfigParser.RawConfigParser()
        self.parser.read(self.config_file)
        for section in self.parser.sections():
            self.setdefault(section, {})
            for option in self.parser.options(section):
                self[section].setdefault(
                    option,
                    self.parser.get(section, option)
                )

    def has_section(self, section):
        return section in self

    def get_param(self, section, param, default=None):
        """
        _get_param_

        convienience param getter with section, param name and
        optional default to avoid key errors
        """
        if section not in self:
            raise KeyError('section {0} not found'.format(section))
        return self[section].get(param, default)

    def package_version(self):
        return self.get('package', {}).get('version')

    def package_name(self):
        return self.get('package', {}).get('name')

    def organisation_name(self):
        return self.get('package', {}).get('organization')

    def release_notes_format(self):
        return self.get('package', {}).get('release_notes_format', 'plaintext')

    def pypi_url(self):
        return self.get('pypi', {}).get('pypi_url')

    def pypi_config(self):
        """
        get the details for uploading to pypi

        """
        return self.get('pypi', {})

    def gitflow_branch_name(self):
        return self.get('gitflow', {}).get('develop_branch', 'develop')

    def gitflow_master_name(self):
        return self.get('gitflow', {}).get('master_branch', 'master')

    def gitflow_feature_prefix(self):
        return self.get('gitflow', {}).get('feature_branch_prefix', 'feature/')

    def gitflow_release_prefix(self):
        return self.get('gitflow', {}).get('release_branch_prefix', 'release/')

    def test_where(self, suite):
        return self.get('test-{0}'.format(suite), {}).get('where')

    def venv_name(self):
        return self.get('build', {}).get('virtualenv_name', 'venv')

    def quality_rcfile(self):
        return self.get('quality', {}).get('rcfile')

    def quality_threshold(self):
        return float(self.get('quality', {}).get('threshold'))

    def release_notes(self):
        """
        returns the release notes file and release
        notes sentinel from the config
        """
        return (
            self.get('package', {}).get('release_notes_file'),
            self.get('package', {}).get('release_notes_sentinel')
        )

    def version_file(self):
        """
        returns the version file and version attr from the configuration
        """
        return (
            self.get('package', {}).get('version_file'),
            self.get('package', {}).get('version_attribute', '__version__')
        )

    def update_package_version(self, new_version):
        """
        _update_package_version_

        Update the version in the configuration field
        """
        self['package']['version'] = new_version
        self.parser.set('package', 'version', new_version)
        with open(self.config_file, 'w') as handle:
            self.parser.write(handle)


def _repo_directory():
    command = ['git', 'rev-parse', '--show-toplevel']
    process = subprocess.Popen([command], stdout=subprocess.PIPE)
    outp, err = process.communicate()
    return outp.strip()


def load_configuration(package_dir=None):
    """
    _load_configuration_

    Load the cirrus.conf file and parse it into a nested dictionary
    like Configuration instance.

    :params package_dir: Location of cirrus managed package if not pwd
    :returns: Configuration instance

    """
    dirname = os.getcwd()
    if package_dir is not None:
        dirname = package_dir

    config_path = os.path.join(dirname, 'cirrus.conf')
    if not os.path.exists(config_path):
        repo_dir = _repo_directory()
        config_path = os.path.join(repo_dir, 'cirrus.conf')

    if not os.path.exists(config_path):
        msg = "Couldnt find ./cirrus.conf, are you in a package directory?"
        raise RuntimeError(msg)

    config_instance = Configuration(config_path)
    config_instance.load()
    return config_instance


def get_github_auth():
    """
    _get_git_auth_

    Pull in github auth user & token from gitconfig
    """
    gitconfig_file = os.path.join(os.environ['HOME'], '.gitconfig')
    config = gitconfig.config(gitconfig_file)
    github_user = config.get('cirrus', 'github-user')
    github_token = config.get('cirrus', 'github-token')
    return github_user, github_token


def get_pypi_auth():
    """
    _pypi_auth_

    Get pypi credentials from gitconfig

    """
    gitconfig_file = os.path.join(os.environ['HOME'], '.gitconfig')
    config = gitconfig.config(gitconfig_file)
    pypi_user = config.get('cirrus', 'pypi-user')
    pypi_ssh_user = config.get('cirrus', 'pypi-ssh-user')
    pypi_key = config.get('cirrus', 'pypi-ssh-key')
    pypi_token = config.get('cirrus', 'pypi-token')
    return {
        'username': pypi_user,
        'ssh_username': pypi_ssh_user,
        'ssh_key': pypi_key,
        'token': pypi_token
    }


def get_buildserver_auth():
    """
    _buildserver_auth_

    Get buildserver credentials from gitconfig

    """
    gitconfig_file = os.path.join(os.environ['HOME'], '.gitconfig')
    config = gitconfig.config(gitconfig_file)
    return (config.get('cirrus', 'buildserver-user'),
            config.get('cirrus', 'buildserver-token'),)


def get_chef_auth():
    """
    get chef auth info from gitconfig

    """
    gitconfig_file = os.path.join(os.environ['HOME'], '.gitconfig')
    config = gitconfig.config(gitconfig_file)
    return {
        'chef_server': config.get('cirrus', 'chef-server'),
        'chef_username': config.get('cirrus', 'chef-username'),
        'chef_keyfile': config.get('cirrus', 'chef-keyfile'),
        'chef_client_user': config.get('cirrus', 'chef-client-user'),
        'chef_client_keyfile': config.get('cirrus', 'chef-client-keyfile')
    }


if __name__ == '__main__':
    load_configuration()
<commit_msg>bugfix double [] around command<commit_after>#!/usr/bin/env python
"""
_configuration_

Util class to read and access the cirrus.conf file
that defines how the various commands operate.

Commands should access this by doing

from cirrus.configuration import load_configuration
conf = load_configuration()

"""
import os
import gitconfig
import subprocess
import ConfigParser


class Configuration(dict):
    """
    _Configuration_

    """
    def __init__(self, config_file):
        super(Configuration, self).__init__(self)
        self.config_file = config_file
        self.parser = None

    def load(self):
        """
        _load_from_file_

        Reread the cirrus config file

        """
        self.parser = ConfigParser.RawConfigParser()
        self.parser.read(self.config_file)
        for section in self.parser.sections():
            self.setdefault(section, {})
            for option in self.parser.options(section):
                self[section].setdefault(
                    option,
                    self.parser.get(section, option)
                )

    def has_section(self, section):
        return section in self

    def get_param(self, section, param, default=None):
        """
        _get_param_

        convienience param getter with section, param name and
        optional default to avoid key errors
        """
        if section not in self:
            raise KeyError('section {0} not found'.format(section))
        return self[section].get(param, default)

    def package_version(self):
        return self.get('package', {}).get('version')

    def package_name(self):
        return self.get('package', {}).get('name')

    def organisation_name(self):
        return self.get('package', {}).get('organization')

    def release_notes_format(self):
        return self.get('package', {}).get('release_notes_format', 'plaintext')

    def pypi_url(self):
        return self.get('pypi', {}).get('pypi_url')

    def pypi_config(self):
        """
        get the details for uploading to pypi

        """
        return self.get('pypi', {})

    def gitflow_branch_name(self):
        return self.get('gitflow', {}).get('develop_branch', 'develop')

    def gitflow_master_name(self):
        return self.get('gitflow', {}).get('master_branch', 'master')

    def gitflow_feature_prefix(self):
        return self.get('gitflow', {}).get('feature_branch_prefix', 'feature/')

    def gitflow_release_prefix(self):
        return self.get('gitflow', {}).get('release_branch_prefix', 'release/')

    def test_where(self, suite):
        return self.get('test-{0}'.format(suite), {}).get('where')

    def venv_name(self):
        return self.get('build', {}).get('virtualenv_name', 'venv')

    def quality_rcfile(self):
        return self.get('quality', {}).get('rcfile')

    def quality_threshold(self):
        return float(self.get('quality', {}).get('threshold'))

    def release_notes(self):
        """
        returns the release notes file and release
        notes sentinel from the config
        """
        return (
            self.get('package', {}).get('release_notes_file'),
            self.get('package', {}).get('release_notes_sentinel')
        )

    def version_file(self):
        """
        returns the version file and version attr from the configuration
        """
        return (
            self.get('package', {}).get('version_file'),
            self.get('package', {}).get('version_attribute', '__version__')
        )

    def update_package_version(self, new_version):
        """
        _update_package_version_

        Update the version in the configuration field
        """
        self['package']['version'] = new_version
        self.parser.set('package', 'version', new_version)
        with open(self.config_file, 'w') as handle:
            self.parser.write(handle)


def _repo_directory():
    command = ['git', 'rev-parse', '--show-toplevel']
    process = subprocess.Popen(command, stdout=subprocess.PIPE)
    outp, err = process.communicate()
    return outp.strip()


def load_configuration(package_dir=None):
    """
    _load_configuration_

    Load the cirrus.conf file and parse it into a nested dictionary
    like Configuration instance.

    :params package_dir: Location of cirrus managed package if not pwd
    :returns: Configuration instance

    """
    dirname = os.getcwd()
    if package_dir is not None:
        dirname = package_dir

    config_path = os.path.join(dirname, 'cirrus.conf')
    if not os.path.exists(config_path):
        repo_dir = _repo_directory()
        config_path = os.path.join(repo_dir, 'cirrus.conf')

    if not os.path.exists(config_path):
        msg = "Couldnt find ./cirrus.conf, are you in a package directory?"
        raise RuntimeError(msg)

    config_instance = Configuration(config_path)
    config_instance.load()
    return config_instance


def get_github_auth():
    """
    _get_git_auth_

    Pull in github auth user & token from gitconfig
    """
    gitconfig_file = os.path.join(os.environ['HOME'], '.gitconfig')
    config = gitconfig.config(gitconfig_file)
    github_user = config.get('cirrus', 'github-user')
    github_token = config.get('cirrus', 'github-token')
    return github_user, github_token


def get_pypi_auth():
    """
    _pypi_auth_

    Get pypi credentials from gitconfig

    """
    gitconfig_file = os.path.join(os.environ['HOME'], '.gitconfig')
    config = gitconfig.config(gitconfig_file)
    pypi_user = config.get('cirrus', 'pypi-user')
    pypi_ssh_user = config.get('cirrus', 'pypi-ssh-user')
    pypi_key = config.get('cirrus', 'pypi-ssh-key')
    pypi_token = config.get('cirrus', 'pypi-token')
    return {
        'username': pypi_user,
        'ssh_username': pypi_ssh_user,
        'ssh_key': pypi_key,
        'token': pypi_token
    }


def get_buildserver_auth():
    """
    _buildserver_auth_

    Get buildserver credentials from gitconfig

    """
    gitconfig_file = os.path.join(os.environ['HOME'], '.gitconfig')
    config = gitconfig.config(gitconfig_file)
    return (config.get('cirrus', 'buildserver-user'),
            config.get('cirrus', 'buildserver-token'),)


def get_chef_auth():
    """
    get chef auth info from gitconfig

    """
    gitconfig_file = os.path.join(os.environ['HOME'], '.gitconfig')
    config = gitconfig.config(gitconfig_file)
    return {
        'chef_server': config.get('cirrus', 'chef-server'),
        'chef_username': config.get('cirrus', 'chef-username'),
        'chef_keyfile': config.get('cirrus', 'chef-keyfile'),
        'chef_client_user': config.get('cirrus', 'chef-client-user'),
        'chef_client_keyfile': config.get('cirrus', 'chef-client-keyfile')
    }


if __name__ == '__main__':
    load_configuration()

{'fraction_non_alphanumeric': 0.13060141509433962, 'fraction_numerical': 0.00029481132075471697, 'mean_word_length': 2.86691365061271, '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': '93488', 'n_tokens_mistral': 4160, 'n_tokens_neox': 3991, 'n_words': 905}