Example #1
0
    def set_value(self, value):
        # Check if the type of the new value matches with the type of the default value, try safe_casting:
        if (isinstance(self._default_value, pl.Path)):
            # When it's a pl.Path, then the exportable FILE_SEP_TEXT should be replaced:
            value = str(value).replace(f"{CU.tfs.dic['FILE_SEP_TEXT'].value}", f"{os.sep}")
        if (isinstance(value, str)):
            value = CU.with_consistent_linesep(value)

        # The callback can do some extra checking of the value while its for instance still string, and order of doing the safe cast and calling the callback used to be the other way around, but the pathlib library uses other windows-lineseparator when casting back to string, but then it's not possible to omit an explanation
        if (self._callback_on_set is not None):
            processed_value = self._callback_on_set(value)
        else:
            processed_value = value

        if processed_value is not None:
            self._value = CU.safe_cast(processed_value, type(self._default_value), "")
        else:
            # The previous _value is left unchanged.
            toast(f"Value for setting \"{self.name}\" is not valid{os.linesep}Original value was left unchanged.")
Example #2
0
    async def async_add_lineup_entry(self, name_new_lineup_entry):
        """
        Actual process to add a lineup_entry.
        :param name_new_lineup_entry:
        :return:
        """
        # Omit the provided explanation-text in case it was not omitted:
        name_new_lineup_entry = str(
            CU.with_consistent_linesep(name_new_lineup_entry)
        ).replace(
            f"{CU.tfs.dic['EXPLANATION_PLAYLIST_SONG_NAME'].value}{os.linesep}",
            "")

        # Check the name_new_lineup_entry by means of a regular expression:
        # Only allow names entirely consisting of alphanumeric characters, dashes and underscores
        if re.match("^[\w\d_-]+$", str(name_new_lineup_entry)):
            filename_lineup_entry = f"{str(name_new_lineup_entry)}.json"
            if len(
                    list(
                        pl.Path(CU.tfs.dic['tf_workspace_path'].value /
                                CU.tfs.dic['PLAYLISTS_DIR_NAME'].value).glob(
                                    filename_lineup_entry))) > 0:
                toast(f"{name_new_lineup_entry} already exists")
            else:
                file_path = pl.Path(CU.tfs.dic['tf_workspace_path'].value /
                                    CU.tfs.dic['PLAYLISTS_DIR_NAME'].value /
                                    filename_lineup_entry)
                with open(str(file_path), "w") as json_file:
                    json_file.write("")

                # TODO: async option doesn't work in combination with asynckivy.start() error is TypeError: '_asyncio.Future' object is not callable
                # async with open(str(file_path), 'w') as json_file:
                #     await json_file.write("")

                toast(f"{name_new_lineup_entry} added")
        else:
            toast(
                f"Name cannot be empty nor contain{os.linesep}non-alphanumeric characters except for \"-_\")"
            )
        await asynckivy.sleep(0)
Example #3
0
    async def async_rename_help(self, help_rowview, new_name_help):
        """
        Actual process to rename a help.
        :param help_rowview:
        :param new_name_help:
        :return:
        """
        # Omit the provided explanation-text in case it was not omitted:
        new_name_help = str(CU.with_consistent_linesep(new_name_help)).replace(
            f"{CU.tfs.dic['EXPLANATION_PLAYLIST_SONG_NAME'].value}{os.linesep}",
            "")

        # Check the new_name_help by means of a regular expression:
        # Only allow names entirely consisting of alphanumeric characters, dashes and underscores
        help_to_rename = help_rowview.help_entry_obj

        if re.match("^[\w\d_-]+$", str(new_name_help)):
            filename_help = f"{str(new_name_help)}.{help_to_rename.file_path.suffix}"
            if len(
                    list(
                        pl.Path(CU.tfs.dic['tf_workspace_path'].value /
                                CU.tfs.dic['RAW_MIDI_DIR_NAME'].value).glob(
                                    filename_help))) > 0:
                toast(f"{new_name_help} already exists")

            elif help_to_rename.file_path.exists():
                old_name = str(help_to_rename.file_path.stem)
                file_path = pl.Path(help_to_rename.file_path.parents[0] /
                                    filename_help)
                pl.Path(help_to_rename.file_path).rename(file_path)
                toast(f"{old_name} renamed to {new_name_help}")
            else:
                toast(f"Help {help_to_rename.file_path.stem} not found")
        else:
            toast(
                f"Name cannot be empty nor contain{os.linesep}non-alphanumeric characters except for \"-_\")"
            )
        await asynckivy.sleep(0)