Exemple #1
0
    def __init__(self,
                 defaults=None,
                 dict_type=_default_dict,
                 allow_no_value=False,
                 *,
                 delimiters=('=', ':'),
                 comment_prefixes=('#', ';'),
                 inline_comment_prefixes=None,
                 strict=True,
                 empty_lines_in_values=True,
                 default_section=DEFAULTSECT,
                 interpolation=_UNSET,
                 converters=_UNSET):
        # replacement of ConfigParser.__init__, do not call super-class constructor
        self._dict = dict_type
        self._defaults = dict_type()
        self._sections = dict_type()
        self._proxies = dict_type()
        self._cache = dict()

        self._comment_prefixes = tuple(comment_prefixes or ())
        self._inline_comment_prefixes = tuple(inline_comment_prefixes or ())
        self._strict = strict
        self._allow_no_value = allow_no_value
        self._empty_lines_in_values = empty_lines_in_values
        self.default_section = default_section

        self._converters = ConverterMapping(self)
        if (converters is not _UNSET):
            self._converters.update(converters)

        self._proxies[default_section] = SectionProxy(self, default_section)

        if defaults:
            for key, value in defaults.items():
                self._defaults[self.optionxform(key)] = value

        self._delimiters = tuple(delimiters)
        if delimiters == ('=', ':'):
            self._optcre = self.OPTCRE_NV if allow_no_value else self.OPTCRE
        else:
            d = "|".join(re_escape(d) for d in delimiters)
            if allow_no_value:
                self._optcre = re_compile(self._OPT_NV_TMPL.format(delim=d),
                                          RE_VERBOSE)
            else:
                self._optcre = re_compile(self._OPT_TMPL.format(delim=d),
                                          RE_VERBOSE)

        if (interpolation is None): self._interpolation = Interpolation()
        elif (interpolation is _UNSET):
            self._interpolation = ExtendedInterpolation()
        else:
            self._interpolation = interpolation
Exemple #2
0
    def load_config(cls, config_file=None):
        """
        Load the configuration file
        """
        config = LenientConfigParser(interpolation=Interpolation())

        files = [
            "/etc/inmanta.cfg",
            os.path.expanduser("~/.inmanta.cfg"), ".inmanta", ".inmanta.cfg"
        ]
        if config_file is not None:
            files.append(config_file)

        config.read(files)
        cls.__instance = config
Exemple #3
0
    def load_config(
        cls,
        min_c_config_file: Optional[str] = None,
        config_dir: Optional[str] = None,
        main_cfg_file: str = "/etc/inmanta/inmanta.cfg",
    ) -> None:
        """
        Load the configuration file
        """

        cfg_files_in_config_dir: List[str]
        if config_dir and os.path.isdir(config_dir):
            cfg_files_in_config_dir = sorted([
                os.path.join(config_dir, f) for f in os.listdir(config_dir)
                if f.endswith(".cfg")
            ])
        else:
            cfg_files_in_config_dir = []

        local_dot_inmanta_cfg_files: List[str] = [
            os.path.expanduser("~/.inmanta.cfg"), ".inmanta", ".inmanta.cfg"
        ]

        # Files with a higher index in the list, override config options defined by files with a lower index
        files: List[str]
        if min_c_config_file is not None:
            files = [
                main_cfg_file
            ] + cfg_files_in_config_dir + local_dot_inmanta_cfg_files + [
                min_c_config_file
            ]
        else:
            files = [main_cfg_file
                     ] + cfg_files_in_config_dir + local_dot_inmanta_cfg_files

        config = LenientConfigParser(interpolation=Interpolation())
        config.read(files)
        cls.__instance = config
        cls._config_dir = config_dir