Esempio n. 1
0
 def __getattr__(self, key: str) -> "ConfigurationEntry":
     """Get an entry from the configuration section"""
     try:
         return self.data[key]
     except KeyError:
         raise exceptions.MissingEntryError(
             f"Configuration section '{self.name}' has no entry '{key}'"
         ) from None
Esempio n. 2
0
    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()