Ejemplo n.º 1
0
    def _get_func_or_section_configs(
            self, section_path_str: str) -> Optional[ActiveFunctionConfig]:
        """
        This get method is used to get only the config for the section path, without handling
        multiple levels of config and overriding. To get the active config for a function,
        use regular get method.

        Args:
            section_path_str:

        Returns:

        """
        if self.section is None:
            raise ConfigManagerNotLoadedException(
                'call .load() on ConfigManager before .get()')

        if section_path_str is None:
            section_path_str = self.section.name

        section_path = SectionPath(section_path_str)

        # Goes into nested sections, until it pulls the final config or section
        config_or_section: ConfigSectionOrConfig = _get_from_nested_obj_by_section_path(
            self, section_path)
        conf = _get_config_from_config_or_section(config_or_section)

        # Now update stored config as loading may have happened during _get_config_from_config_or_section
        # Want to keep the active config once it is loaded
        # But if it is a section, then don't want to overwrite with config
        if not isinstance(config_or_section, ConfigSection):
            _set_in_nested_obj_by_section_path(self, section_path, conf)

        return conf
Ejemplo n.º 2
0
 def refresh(
         self,
         section_path_str: str) -> Tuple[ConfigBase, bool, Dict[str, Any]]:
     config_obj = self._get_project_config_or_local_config_by_section_path(
         section_path_str)
     if config_obj is None:
         raise ConfigManagerNotLoadedException('no config to refresh')
     would_refresh = self._determine_and_track_if_config_would_be_refreshed(
         config_obj, section_path_str)
     if would_refresh:
         updates = config_obj.refresh()
     else:
         updates = {}
     return config_obj, would_refresh, updates
Ejemplo n.º 3
0
    def get(self, section_path_str: str) -> Optional[ActiveFunctionConfig]:
        """
        Handles config inheritance to get the active config for a section or function

        Args:
            section_path_str:

        Returns:

        """
        config = self._get_func_or_section_configs(section_path_str)

        if self.section is None:
            raise ConfigManagerNotLoadedException(
                'call .load() on ConfigManager before .get()')

        # First override for function defaults is global project config
        section_configs = [self.section.config]

        # Get configs, in order of highest level to lowest level. Will go from project to highest section,
        # down to lowest section.
        section_path = SectionPath(section_path_str)
        full_section = ''
        for section in section_path[:
                                    -1]:  # skip the last section or function for special handling at end
            full_section += section  # rebuilding full section path str
            section_configs.append(
                self._get_func_or_section_configs(full_section))
            full_section += '.'

        # Last item of section_path may be another section, or the function/Pipeline itself. If it's a section,
        # must add config for override, but if is function, it is already the base config so should not update.
        full_section += section_path[-1]
        if not self._is_function_or_pipeline_path(full_section):
            # if is a section, not function/pipeline
            section_configs.append(
                self._get_func_or_section_configs(full_section))

        if config:
            # Override configs. Default config is base config, then gets updated by project, then high
            # level sections to low level sections
            [
                config.update(section_config)
                for section_config in section_configs
            ]

            # Last, override with local config
            config.update(self.local_config)

        return config
Ejemplo n.º 4
0
    def _set_func_or_section_config(self,
                                    section_path_str: str,
                                    value=None,
                                    allow_create: bool = True) -> None:
        if self.section is None:
            raise ConfigManagerNotLoadedException(
                'call .load() on ConfigManager before .set()')

        if section_path_str is None:
            section_path_str = self.section.name

        section_path = SectionPath(section_path_str)

        if allow_create:
            self._set_func_or_config_with_create(section_path, value)
        else:
            self._set_func_or_config_no_create(section_path, value)
Ejemplo n.º 5
0
    def update(self,
               d_: dict = None,
               section_path_str: str = None,
               pyfileconf_persist: bool = True,
               **kwargs) -> Tuple[ConfigBase, bool]:
        """

        :param d_:
        :param section_path_str:
        :param pyfileconf_persist:
        :param kwargs:
        :return: new config, whether config was updated
        """
        config_obj = self._get_project_config_or_local_config_by_section_path(
            section_path_str)
        if config_obj is None:
            raise ConfigManagerNotLoadedException('no config to update')
        would_update = self._determine_and_track_if_config_would_be_updated(
            config_obj, section_path_str, d_, **kwargs)
        if would_update:
            config_obj.update(d_,
                              pyfileconf_persist=pyfileconf_persist,
                              **kwargs)
        return config_obj, would_update
Ejemplo n.º 6
0
 def _get_config(self, section_path_str: str) -> ActiveFunctionConfig:
     config = self._config.get(section_path_str)
     if config is None:
         raise ConfigManagerNotLoadedException('no config to get')
     return config
Ejemplo n.º 7
0
 def update(self, d: dict = None, **kwargs):
     if self.config is None:
         raise ConfigManagerNotLoadedException('no config in ConfigSection')
     if d is not None:
         self.config.update(d)
     self.config.update(kwargs)
Ejemplo n.º 8
0
 def pop(self, key: str, section_path_str: str = None) -> Any:
     config_obj = self._get_project_config_or_local_config_by_section_path(
         section_path_str)
     if config_obj is None:
         raise ConfigManagerNotLoadedException('no config to pop')
     return config_obj.pop(key)