<commit_before>import configparser

from .plugin import TestRailPlugin
from .testrail_api import APIClient


def pytest_addoption(parser):
    group = parser.getgroup('testrail')
    group.addoption(
        '--testrail',
        action='store',
        help='Create and update testruns with TestRail')
    group.addoption(
        '--no-ssl-cert-check',
        action='store_true',
        default=False,
        required=False,
        help='Do not check for valid SSL certificate on TestRail host'
    )
    group.addoption(
        '--tr_name',
        action='store',
        default=None,
        required=False,
        help='Name given to testrun, that appears in TestRail'
    )


def pytest_configure(config):
    if config.option.testrail:
        cfg_file = read_config_file(config.getoption("--testrail"))
        client = APIClient(cfg_file.get('API', 'url'))
        client.user = cfg_file.get('API', 'email')
        client.password = cfg_file.get('API', 'password', raw=True)
        ssl_cert_check = True
        tr_name = config.getoption('--tr_name')

        if config.getoption('--no-ssl-cert-check') is True:
            ssl_cert_check = False

        config.pluginmanager.register(
            TestRailPlugin(
                client=client,
                assign_user_id=cfg_file.get('TESTRUN', 'assignedto_id'),
                project_id=cfg_file.get('TESTRUN', 'project_id'),
                suite_id=cfg_file.get('TESTRUN', 'suite_id'),
                cert_check=ssl_cert_check,
                tr_name=tr_name
            )
        )


def read_config_file(configfile):
    config = configparser.ConfigParser()
    config.read(configfile)
    return config<commit_msg>allow config options to be specified either by flag or via cfg file<commit_after>import os
import configparser

from .plugin import TestRailPlugin
from .testrail_api import APIClient


def pytest_addoption(parser):
    group = parser.getgroup('testrail')
    group.addoption(
        '--testrail',
        action='store_true',
        help='Create and update testruns with TestRail')
    group.addoption(
        '--tr-config',
        action='store',
        default='testrail.cfg',
        help='Path to the config file containing information about the TestRail server (defaults to testrail.cfg)')
    group.addoption(
        '--tr-url',
        action='store',
        help='TestRail address you use to access TestRail with your web browser')
    group.addoption(
        '--tr-email',
        action='store',
        help='Email for the account on the TestRail server')
    group.addoption(
        '--tr-password',
        action='store',
        help='Password for the account on the TestRail server')
    group.addoption(
        '--tr-testrun-assignedto-id',
        action='store',
        help='ID of the user assigned to the test run')
    group.addoption(
        '--tr-testrun-project-id',
        action='store',
        help='ID of the project the test run is in')
    group.addoption(
        '--tr-testrun-suite-id',
        action='store',
        help='ID of the test suite containing the test cases')
    group.addoption(
        '--tr-no-ssl-cert-check',
        action='store_false',
        help='Do not check for valid SSL certificate on TestRail host'
    )
    group.addoption(
        '--tr-testrun-name',
        action='store',
        default=None,
        required=False,
        help='Name given to testrun, that appears in TestRail'
    )


def pytest_configure(config):
    if config.getoption('--testrail'):
        cfg_file_path = config.getoption('--tr-config')
        config_manager = ConfigManager(cfg_file_path, config)
        client = APIClient(config_manager.getoption('tr-url', 'url', 'API'),
                           config_manager.getoption('tr-email', 'email', 'API'),
                           config_manager.getoption('tr-password', 'password', 'API'))

        config.pluginmanager.register(
            TestRailPlugin(
                client=client,
                assign_user_id=config_manager.getoption('tr-testrun-assignedto-id', 'assignedto_id', 'TESTRUN'),
                project_id=config_manager.getoption('tr-testrun-project-id', 'project_id', 'TESTRUN'),
                suite_id=config_manager.getoption('tr-testrun-suite-id', 'suite_id', 'TESTRUN'),
                cert_check=config_manager.getoption('tr-no-ssl-cert-check', 'no_ssl_cert_check', 'API', default=True),
                tr_name=config_manager.getoption('tr-testrun-name', 'name', 'TESTRUN')
            )
        )


class ConfigManager(object):
    def __init__(self, cfg_file_path, config):
        '''
        Handles retrieving configuration values. Config options set in flags are given preferance over options set in the
        config file.

        :param cfg_file_path: Path to the config file containing information about the TestRail server.
        :type cfg_file_path: str or None
        :param config: Config object containing commandline flag options.
        :type config: _pytest.config.Config
        '''
        self.cfg_file = None
        if os.path.isfile(cfg_file_path) or os.path.islink(cfg_file_path):
            self.cfg_file = configparser.ConfigParser()
            self.cfg_file.read(cfg_file_path)

        self.config = config

    def getoption(self, flag, cfg_name, section=None, default=None):
        value = self.config.getoption('--{}'.format(flag))
        if value is not None:
            return value
        if section is None or self.cfg_file is None:
            return None
        return self.cfg_file.get(section, cfg_name, fallback=None)
{'fraction_non_alphanumeric': 0.11117040199217361, 'fraction_numerical': 0.0, 'mean_word_length': 2.4328449328449326, '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': '7559986', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1610, 'n_words': 379}