def _load_config(config_section: configparser.SectionProxy):
    supplied_keys = set(config_section.keys())
    missing_keys = _REQUIRED_KEYS - supplied_keys
    if missing_keys:
        raise _errors.StreamConfigMissingKeysError(missing_keys)

    unexpected_keys = supplied_keys - (_REQUIRED_KEYS | _OPTIONAL_KEYS)
    if unexpected_keys:
        raise _errors.StreamConfigUnexpectedKeysError(unexpected_keys)

    # Make sure to_emails and to_sms_emails are valid json
    to_emails = config_section[_TO_EMAILS_KEY]
    try:
        json.loads(to_emails)
    except json.decoder.JSONDecodeError as e:
        raise _errors.StreamConfigToEmailsFormatError(to_emails) from e

    to_emails = config_section.get(_TO_SMS_EMAILS_KEY)
    if to_emails:
        try:
            json.loads(to_emails)
        except json.decoder.JSONDecodeError as e:
            raise _errors.StreamConfigToSmsEmailsFormatError(to_emails) from e

    return config_section
    def _test_required_fieds_of_section(section: SectionProxy,
                                        required_fields: List[str]) -> bool:
        """Test that all required fields are present in all sections of the parsed ConfigParser.

        :param section: section of a ConfigPArser to test the fields of
        :param required_fields: fields required in all sections of the ConfigParser
        """
        return len(set(section.keys())
                   & set(required_fields)) >= len(required_fields)
Exemple #3
0
    def get_sasl_settings(section: configparser.SectionProxy) -> Dict:
        has_sasl_entries = any(
            entry.startswith("sasl_") for entry in section.keys())
        if not has_sasl_entries:
            return {}

        def translate(sasl_param_name: str) -> str:
            return sasl_param_name[5:]

        sasl_params = ["sasl_mechanism", "sasl_user", "sasl_password"]

        data = {translate(param): section[param] for param in sasl_params}
        return {"sasl_params": data}
Exemple #4
0
def parse_ini_config_with_cli(parser: argparse.ArgumentParser,
                              ini_config: configparser.SectionProxy,
                              cli_args: Sequence[str]) -> List[str]:
    """Combine the INI file settings with the CLI args, using the CLI args as the override.

    Args:
        parser: the argparser
        ini_config: the section of the parsed INI file
        cli_args: the original cli args

    Returns:
        Updated args mixing INI and CLI, with CLI used as the override
    """

    action_maps = get_parser_actions(parser)
    final_args_list = [action_maps.actions.get(i, i) for i in cli_args]

    def ws_proc(value: str) -> List[str]:
        """Convenience function for stripping newlines from configparser section values
        and splitting whitespace to a list.
        """
        return value.replace("\n", " ").split()

    for k in ini_config.keys():
        arg_key = f"--{k}"

        if arg_key in action_maps.actions.values():
            if arg_key not in final_args_list:

                if k in action_maps.action_types[
                        mutatest.cli.ValidCategoryAction]:
                    values = ws_proc(ini_config[k])
                    final_args_list.extend([arg_key] + values)

                elif k in action_maps.action_types[argparse._StoreTrueAction]:
                    if ini_config.getboolean(k):
                        final_args_list.append(arg_key)

                elif k in action_maps.action_types[argparse._AppendAction]:
                    values = ws_proc(ini_config[k])
                    final_args_list.extend([
                        i for j in list(itertools.product([arg_key], values))
                        for i in j
                    ])

                else:
                    final_args_list.extend([arg_key, ini_config[k]])

    return final_args_list
Exemple #5
0
    def get_ssl_settings(section: configparser.SectionProxy) -> Dict:
        has_ssl_entries = any(
            entry.startswith("ssl_") for entry in section.keys())
        if not has_ssl_entries:
            return {}

        def translate(ssl_param_name: str) -> str:
            return ssl_param_name[4:]

        ssl_params = [
            "ssl_cafile", "ssl_certfile", "ssl_keyfile", "ssl_password"
        ]
        data = {}
        for param in ssl_params:
            assign_if_present(param, cast(Dict[str, Any], section), data,
                              translate)
        return {"ssl_params": data}
Exemple #6
0
 def _is_valid_emotions_section(cls, emotes_section: SectionProxy) -> bool:
     emotions_sections = emotes_section.keys()
     return all(key in emotions_sections
                for key in cls.VALID_EMOTION_SECTIONS)
Exemple #7
0
 def __init__(self,
              section: configparser.SectionProxy,
              name: Optional[T] = None) -> None:
     self.__name = name
     self.__section = section
     self.__unused_keys = set(section.keys())