Exemple #1
0
def upgrade_to_v2_7_0_dev_3(config):
    """Save file naming scripts to dictionary.
    """
    from picard.script import get_file_naming_script_presets
    from picard.script.serializer import (
        FileNamingScript,
        ScriptImportError,
    )
    Option("setting", "file_renaming_scripts", {})
    ListOption("setting", "file_naming_scripts", [])
    TextOption("setting", "file_naming_format", DEFAULT_FILE_NAMING_FORMAT)
    TextOption("setting", "selected_file_naming_script_id", "")
    scripts = {}
    for item in config.setting["file_naming_scripts"]:
        try:
            script_item = FileNamingScript().create_from_yaml(
                item, create_new_id=False)
            scripts[script_item["id"]] = script_item.to_dict()
        except ScriptImportError:
            log.error("Error converting file naming script")
    script_list = set(scripts.keys()) | set(
        map(lambda item: item["id"], get_file_naming_script_presets()))
    if config.setting["selected_file_naming_script_id"] not in script_list:
        script_item = FileNamingScript(
            script=config.setting["file_naming_format"],
            title=_("Primary file naming script"),
            readonly=False,
            deletable=True,
        )
        scripts[script_item["id"]] = script_item.to_dict()
        config.setting["selected_file_naming_script_id"] = script_item["id"]
    config.setting["file_renaming_scripts"] = scripts
    config.setting.remove("file_naming_scripts")
    config.setting.remove("file_naming_format")
Exemple #2
0
 def _get_naming_script(self, config, value):
     if value in config.setting["file_renaming_scripts"]:
         return config.setting["file_renaming_scripts"][value]["title"]
     presets = {x["id"]: x["title"] for x in get_file_naming_script_presets()}
     if value in presets:
         return presets[value]
     return _("Unknown script")
 def get_file_naming_script_name(self, script_id):
     config = get_config()
     scripts = config.setting["file_renaming_scripts"]
     if script_id in scripts:
         return scripts[script_id]["title"]
     presets = {x["id"]: x["title"] for x in get_file_naming_script_presets()}
     if script_id in presets:
         return presets[script_id]
     return _("Unknown script")
Exemple #4
0
    def populate_script_selector(self):
        """Populate the script selection combo box.

        Returns:
            int: The index of the selected script in the combo box.
        """
        if not self.selected_script_id:
            script_item = FileNamingScript(
                script=get_config().setting["file_naming_format"],
                title=_("Primary file naming script"),
                readonly=False,
                deletable=True,
            )
            self.naming_scripts.insert(0, script_item.to_yaml())
            self.selected_script_id = script_item['id']

        self.ui.preset_naming_scripts.blockSignals(True)
        self.ui.preset_naming_scripts.clear()

        def _add_and_check(idx, count, title, item):
            self.ui.preset_naming_scripts.addItem(title, item)
            if item['id'] == self.selected_script_id:
                idx = count
            count += 1
            return idx, count

        idx = 0
        count = 0  # Use separate counter rather than `i` in case ScriptImportError triggers, resulting in an incorrect index count.
        for i in range(len(self.naming_scripts)):
            try:
                script_item = FileNamingScript().create_from_yaml(
                    self.naming_scripts[i], create_new_id=False)
            except ScriptImportError:
                pass
            else:
                self.naming_scripts[i] = script_item.to_yaml(
                )  # Ensure scripts are stored with id codes
                idx, count = _add_and_check(
                    idx, count, self.SCRIPT_TITLE_USER % script_item["title"],
                    script_item)

        for script_item in get_file_naming_script_presets():
            idx, count = _add_and_check(idx, count, script_item['title'],
                                        script_item)

        self.ui.preset_naming_scripts.blockSignals(False)
        self.update_scripts_list()
        return idx
Exemple #5
0
def get_file_naming_script(settings):
    """Retrieve the file naming script.

    Args:
        settings (ConfigSection): Object containing the user settings

    Returns:
        str: The text of the file naming script if available, otherwise None
    """
    from picard.script import get_file_naming_script_presets
    scripts = settings["file_renaming_scripts"]
    selected_id = settings["selected_file_naming_script_id"]
    if selected_id:
        if scripts and selected_id in scripts:
            return scripts[selected_id]["script"]
        for item in get_file_naming_script_presets():
            if item["id"] == selected_id:
                return str(item["script"])
    log.error("Unable to retrieve the file naming script '%s'", selected_id)
    return None