Exemplo n.º 1
0
    def _set_config(self,
                    configfile: Optional[str],
                    config: Optional[FaceswapConfig]) -> dict:
        """ Set the correct configuration for the plugin based on whether a config file
        or pre-loaded config has been passed in.

        Parameters
        ----------
        configfile: str
            Location of custom configuration ``ini`` file. If ``None`` then use the
            default config location
        config: :class:`lib.config.FaceswapConfig`
            Pre-loaded :class:`lib.config.FaceswapConfig`. If passed, then this will be
            used over any configuration on disk. If ``None`` then it is ignored.

        Returns
        -------
        dict
            The configuration in dictionary form for the given from
            :attr:`lib.config.FaceswapConfig.config_dict`
        """
        section = ".".join(self.__module__.split(".")[-2:])
        if config is None:
            retval = Config(section, configfile=configfile).config_dict
        else:
            config.section = section
            retval = config.config_dict
            config.section = None
        logger.debug("Config: %s", retval)
        return retval
Exemplo n.º 2
0
def get_config(plugin_name: str, configfile: Optional[str] = None) -> dict:
    """ Obtain the configuration settings for the writer plugin.

    Parameters
    ----------
    plugin_name: str
        The name of the convert plugin to return configuration settings for
    configfile: str, optional
        The full path to a custom configuration ini file. If ``None`` is passed
        then the file is loaded from the default location. Default: ``None``.

    Returns
    -------
    dict
        The requested configuration dictionary
    """
    return Config(plugin_name, configfile=configfile).config_dict
Exemplo n.º 3
0
def _get_config(plugin_name, configfile=None):
    """ Return the :attr:`lib.config.FaceswapConfig.config_dict` for the requested plugin.

    Parameters
    ----------
    plugin_name: str
        The name of the plugin to retrieve the config for
    configfile: str, optional
        Optional location of custom configuration ``ini`` file. If ``None`` then use the default
        config location. Default: ``None``

    Returns
    -------
    dict
        The configuration in dictionary form for the given plugin_name from
         :attr:`lib.config.FaceswapConfig.config_dict`
    """
    return Config(plugin_name, configfile=configfile).config_dict
Exemplo n.º 4
0
def get_config(plugin_name):
    """ Return the config for the requested model """
    return Config(plugin_name).config_dict
Exemplo n.º 5
0
 def __init__(self):
     self.config = Config(None)
     self.config_dicts = self.get_config_dicts()  # Holds currently saved config
     self.tk_vars = dict()
Exemplo n.º 6
0
class ConfigTools():
    """ Saving and resetting config values and stores selected variables """
    def __init__(self):
        self.config = Config(None)
        self.config_dicts = self.get_config_dicts()  # Holds currently saved config
        self.tk_vars = dict()

    @property
    def sections(self):
        """ Return the sorted unique section names from the configs """
        return sorted(set(plugin.split(".")[0] for plugin in self.config.config.sections()
                          if plugin.split(".")[0] != "writer"))

    @property
    def plugins_dict(self):
        """ Return dict of sections with sorted list of containing plugins """
        return {section: sorted([plugin.split(".")[1] for plugin in self.config.config.sections()
                                 if plugin.split(".")[0] == section])
                for section in self.sections}

    def update_config(self):
        """ Update config with selected values """
        for section, items in self.tk_vars.items():
            for item, value in items.items():
                new_value = str(value.get())
                old_value = self.config.config[section][item]
                if new_value != old_value:
                    logger.trace("Updating config: %s, %s from %s to %s",
                                 section, item, old_value, new_value)
                    self.config.config[section][item] = str(value.get())

    def get_config_dicts(self):
        """ Hold a custom config dict for the config """
        config_dicts = dict()
        for section in self.config.config.sections():
            if section == "writer":
                continue
            default_dict = self.config.defaults[section]
            for key in default_dict.keys():
                if key == "helptext":
                    continue
                default_dict[key]["value"] = self.config.get(section, key)
            config_dicts[section] = default_dict
        return config_dicts

    def reset_config_saved(self, section=None):
        """ Reset config to saved values """
        logger.debug("Resetting to saved config: %s", section)
        sections = [section] if section is not None else [key for key in self.tk_vars.keys()]
        for config_section in sections:
            for item, options in self.config_dicts[config_section].items():
                if item == "helptext":
                    continue
                val = options["value"]
                if val != self.tk_vars[config_section][item].get():
                    self.tk_vars[config_section][item].set(val)
                    logger.debug("Setting %s - %s to saved value %s", config_section, item, val)
        logger.debug("Reset to saved config: %s", section)

    def reset_config_default(self, section=None):
        """ Reset config to default values """
        logger.debug("Resetting to default: %s", section)
        sections = [section] if section is not None else [key for key in self.tk_vars.keys()]
        for config_section in sections:
            for item, options in self.config.defaults[config_section].items():
                if item == "helptext":
                    continue
                default = options["default"]
                if default != self.tk_vars[config_section][item].get():
                    self.tk_vars[config_section][item].set(default)
                    logger.debug("Setting %s - %s to default value %s",
                                 config_section, item, default)
        logger.debug("Reset to default: %s", section)

    def save_config(self, section=None):
        """ Save config """
        logger.debug("Saving %s config", section)
        new_config = ConfigParser(allow_no_value=True)
        for config_section, items in self.config_dicts.items():
            logger.debug("Adding section: '%s')", config_section)
            self.config.insert_config_section(config_section, items["helptext"], config=new_config)
            for item, options in items.items():
                if item == "helptext":
                    continue
                if section is not None and config_section != section:
                    new_opt = options["value"]  # Keep saved item for other sections
                    logger.debug("Retaining option: (item: '%s', value: '%s')", item, new_opt)
                else:
                    new_opt = self.tk_vars[config_section][item].get()
                    logger.debug("Setting option: (item: '%s', value: '%s')", item, new_opt)
                helptext = options["helptext"]
                helptext = self.config.format_help(helptext, is_section=False)
                new_config.set(config_section, helptext)
                new_config.set(config_section, item, str(new_opt))
        self.config.config = new_config
        self.config.save_config()
        print("Saved config: '{}'".format(self.config.configfile))
        # Update config dict to newly saved
        self.config_dicts = self.get_config_dicts()
        logger.debug("Saved config")