コード例 #1
0
ファイル: config.py プロジェクト: uasau/midgard
    def master_section(self) -> "ConfigurationSection":
        """The master section"""
        if self._master_section is None:
            raise exceptions.MissingSectionError(
                f"Configuration {self.name!r} has not defined a master section"
            )
        if self._master_section not in self._sections:
            raise exceptions.MissingSectionError(
                f"Master section {self._master_section!r} does not exist in configuration {self.name!r}"
            )

        return self._sections[self._master_section]
コード例 #2
0
ファイル: config.py プロジェクト: uasau/midgard
    def update(
        self,
        section: str,
        key: str,
        value: str,
        *,
        profile: ProfileName = None,
        source: str = "unknown",
        meta: Optional[Dict[str, str]] = None,
        allow_new: bool = True,
        _update_sections: bool = True,
    ) -> None:
        """Update a configuration section with a configuration entry

        If `allow_new` is False, the configuration entry must already exist. If it is True the update is allowed to
        create a new section and a new entry is necessary.

        The `_update_sections` flag can be used to not update the sections of the configuration, only the
        profiles. This should typically not be done, but is used by some of the other update methods which update the
        sections themselves.

        Args:
            section:    Section to update.
            key:        Key of entry.
            value:      Value of entry.
            profile:    Profile to update.
            source:     Source of the update.
            meta:       Metadata like help text and type hints for the entry.
            allow_new:  Whether to allow the creation of a new section and entry.
        """
        if not allow_new:
            profile_str = "" if profile is None else f"(profile: '{profile}')"
            if section not in self._sections:
                raise exceptions.MissingSectionError(
                    f"Configuration '{self.name}' does not contain section '{section}' {profile_str}"
                )
            if key not in self._sections[section]:
                raise exceptions.MissingEntryError(
                    f"Section '{section}' of configuration '{self.name}' does not contain entry '{key}' {profile_str}"
                )

        # Add entry to profile
        source = source if profile is None else f"{source} ({profile})"
        profile_sections = self._profile_sections.setdefault(profile, dict())
        profile_sections.setdefault(section, ConfigurationSection(section))

        # Record that configuration has been updated
        if key not in profile_sections[
                section] or profile_sections[section][key]._value != value:
            self._update_count += 1

        profile_sections[section][key] = ConfigurationEntry(
            key, value=value, source=source, meta=meta, vars_dict=self.vars)

        # Update sections
        if _update_sections:
            self._set_sections_for_profiles()
コード例 #3
0
ファイル: config.py プロジェクト: uasau/midgard
    def __getitem__(
            self,
            key: str) -> Union["ConfigurationSection", "ConfigurationEntry"]:
        """Get a section or entry from the master section from the configuration"""
        if key in self.section_names:
            return self._sections[key]

        try:
            return self.master_section[key]
        except exceptions.MissingSectionError:
            try:
                return self.fallback_config[key]
            except exceptions.MidgardException:
                raise exceptions.MissingSectionError(
                    f"Configuration {self.name!r} has no section {key!r}"
                ) from None