Beispiel #1
0
 def _script_to_filename(self, format, file_metadata, settings=None):
     if settings is None:
         settings = config.setting
     metadata = Metadata()
     if config.setting["clear_existing_tags"]:
         metadata.copy(file_metadata)
     else:
         metadata.copy(self.orig_metadata)
         metadata.update(file_metadata)
     # make sure every metadata can safely be used in a path name
     for name in metadata.keys():
         if isinstance(metadata[name], basestring):
             metadata[name] = sanitize_filename(metadata[name])
     format = format.replace("\t", "").replace("\n", "")
     filename = ScriptParser().eval(format, metadata, self)
     if settings["ascii_filenames"]:
         if isinstance(filename, unicode):
             filename = unaccent(filename)
         filename = replace_non_ascii(filename)
     # replace incompatible characters
     if settings["windows_compatibility"] or sys.platform == "win32":
         filename = replace_win32_incompat(filename)
     # remove null characters
     filename = filename.replace("\x00", "")
     return filename
Beispiel #2
0
 def _script_to_filename(self, naming_format, file_metadata, settings=None):
     if settings is None:
         settings = config.setting
     metadata = Metadata()
     if config.setting["clear_existing_tags"]:
         metadata.copy(file_metadata)
     else:
         metadata.copy(self.orig_metadata)
         metadata.update(file_metadata)
     # make sure every metadata can safely be used in a path name
     for name in metadata.keys():
         if isinstance(metadata[name], str):
             metadata[name] = sanitize_filename(metadata[name])
     naming_format = naming_format.replace("\t", "").replace("\n", "")
     filename = ScriptParser().eval(naming_format, metadata, self)
     if settings["ascii_filenames"]:
         if isinstance(filename, str):
             filename = unaccent(filename)
         filename = replace_non_ascii(filename)
     # replace incompatible characters
     if settings["windows_compatibility"] or sys.platform == "win32":
         filename = replace_win32_incompat(filename)
     # remove null characters
     if isinstance(filename, (bytes, bytearray)):
         filename = filename.replace(b"\x00", "")
     return filename
Beispiel #3
0
 def _make_image_filename(self, filename, dirname, metadata):
     if config.setting["ascii_filenames"]:
         if isinstance(filename, unicode):
             filename = unaccent(filename)
         filename = replace_non_ascii(filename)
     if not filename:
         filename = "cover"
     if not os.path.isabs(filename):
         filename = os.path.join(dirname, filename)
     # replace incompatible characters
     if config.setting["windows_compatibility"] or sys.platform == "win32":
         filename = replace_win32_incompat(filename)
     # remove null characters
     filename = filename.replace("\x00", "")
     return encode_filename(filename)
Beispiel #4
0
 def _make_image_filename(self, filename, dirname, metadata):
     if config.setting["ascii_filenames"]:
         if isinstance(filename, unicode):
             filename = unaccent(filename)
         filename = replace_non_ascii(filename)
     if not filename:
         filename = "cover"
     if not os.path.isabs(filename):
         filename = os.path.join(dirname, filename)
     # replace incompatible characters
     if config.setting["windows_compatibility"] or sys.platform == "win32":
         filename = replace_win32_incompat(filename)
     # remove null characters
     filename = filename.replace("\x00", "")
     return encode_filename(filename)
def script_to_filename(naming_format, metadata, file=None, settings=None):
    if settings is None:
        settings = config.setting
    # make sure every metadata can safely be used in a path name
    for name in metadata:
        values = [sanitize_filename(str(v)) for v in metadata.getall(name)]
        metadata.set(name, values)
    naming_format = naming_format.replace("\t", "").replace("\n", "")
    filename = ScriptParser().eval(naming_format, metadata, file)
    if settings["ascii_filenames"]:
        filename = replace_non_ascii(filename, pathsave=True)
    # replace incompatible characters
    if settings["windows_compatibility"] or sys.platform == "win32":
        filename = replace_win32_incompat(filename)
    # remove null characters
    filename = filename.replace("\x00", "")
    return filename
Beispiel #6
0
def script_to_filename_with_metadata(naming_format,
                                     metadata,
                                     file=None,
                                     settings=None):
    """Creates a valid filename from a script with the given metadata.

    Args:
        naming_format: A string containing the tagger script. The result of
            executing this script will be the filename.
        metadata: A Metadata object. The metadata will not be modified.
        file: A File object (optional)
        settings: The settings. If not set config.setting will be used.

    Returns:
        A tuple with the filename as first element and the updated metadata
        with changes from the script as second.
    """
    if settings is None:
        config = get_config()
        settings = config.setting
    # make sure every metadata can safely be used in a path name
    win_compat = IS_WIN or settings["windows_compatibility"]
    new_metadata = Metadata()
    for name in metadata:
        new_metadata[name] = [
            sanitize_filename(str(v), win_compat=win_compat)
            for v in metadata.getall(name)
        ]
    naming_format = naming_format.replace("\t", "").replace("\n", "")
    filename = ScriptParser().eval(naming_format, new_metadata, file)
    if settings["ascii_filenames"]:
        filename = replace_non_ascii(filename,
                                     pathsave=True,
                                     win_compat=win_compat)
    # replace incompatible characters
    if win_compat:
        filename = replace_win32_incompat(filename)
    # remove null characters
    filename = filename.replace("\x00", "")
    return (filename, new_metadata)
Beispiel #7
0
def script_to_filename(naming_format, metadata, file=None, settings=None):
    if settings is None:
        settings = config.setting
    # make sure every metadata can safely be used in a path name
    win_compat = IS_WIN or settings["windows_compatibility"]
    meta = Metadata()
    for name in metadata:
        meta[name] = [
            sanitize_filename(str(v), win_compat=win_compat)
            for v in metadata.getall(name)
        ]
    naming_format = naming_format.replace("\t", "").replace("\n", "")
    filename = ScriptParser().eval(naming_format, meta, file)
    if settings["ascii_filenames"]:
        filename = replace_non_ascii(filename,
                                     pathsave=True,
                                     win_compat=win_compat)
    # replace incompatible characters
    if win_compat:
        filename = replace_win32_incompat(filename)
    # remove null characters
    filename = filename.replace("\x00", "")
    return filename
Beispiel #8
0
 def _make_image_filename(self, filename, dirname, _metadata):
     metadata = Metadata()
     metadata.copy(_metadata)
     metadata["coverart_maintype"] = self.maintype
     metadata["coverart_comment"] = self.comment
     if self.is_front:
         metadata.add_unique("coverart_types", "front")
     for cover_type in self.types:
         metadata.add_unique("coverart_types", cover_type)
     filename = ScriptParser().eval(filename, metadata)
     if config.setting["ascii_filenames"]:
         filename = replace_non_ascii(filename, pathsave=True)
     if not filename:
         filename = "cover"
     if not os.path.isabs(filename):
         filename = os.path.join(dirname, filename)
     # replace incompatible characters
     if config.setting["windows_compatibility"] or sys.platform == "win32":
         filename = replace_win32_incompat(filename)
     # remove null characters
     if isinstance(filename, bytes):
         filename = filename.replace(b"\x00", "")
     return encode_filename(filename)