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
def encode(self): self.widget.ui.encode_output.appendPlainText('Encoding CD...') opts = self.config.setting['cdripper_flac_opts'] for track in self.album.tracks: if self.discid not in track.metadata['~discids']: self.log.debug( 'discid %s not found in %r', self.discid, track.metadata['~discids']) continue # Track is not part of this disc. self._expected_num_tracks += 1 track_title = track.metadata['title'] track_num = track.metadata['tracknumber'].zfill(2) wav_path = os.path.join(self._tmpdir, 'track%s.cdda.wav' % track_num) flac_name = sanitize_filename(u'%s %s.flac' % (track_num, track_title)) flac_path = os.path.join(self._tmpdir, flac_name) args = opts.split() + ['-o', flac_path, wav_path] process = self._newProcess() process.finished.connect(self.encodeFinished) process.start('/usr/bin/flac', args) self._processes.append(process) self._encode_process_count += 1 self._flac_files.append((flac_path, track))
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
def _replace_char(map, ch, pathsave=False, win_compat=False): try: result = map[ch] if ch != result and pathsave: result = sanitize_filename(result, win_compat=win_compat) return result except KeyError: return ch
def _replace_unicode_simplify_combinations(char, pathsave, win_compat): result = _simplify_combinations.get(char) if result is None: return char elif not pathsave: return result else: return sanitize_filename(result, win_compat=win_compat)
def unicode_simplify_punctuation(string, pathsave=False, win_compat=False): temp = [] for c in string: try: result = _simplify_punctuation[c] if c != result and pathsave: result = sanitize_filename(result, win_compat=win_compat) except KeyError: result = c temp.append(result) return ''.join(temp)
def _script_to_filename(self, format, file_metadata, settings): metadata = Metadata() metadata.copy(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) # replace incompatible characters if settings["windows_compatible_filenames"] or sys.platform == "win32": filename = replace_win32_incompat(filename) if settings["ascii_filenames"]: if isinstance(filename, unicode): filename = unaccent(filename) filename = replace_non_ascii(filename) return 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
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)
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
"\uFF61": ".", # HALFWIDTH IDEOGRAPHIC FULL STOP (compat) "\uFF64": ",", # HALFWIDTH IDEOGRAPHIC COMMA (compat) "\u2212": "-", # MINUS SIGN (from ‹character-fallback›) "\u2215": "/", # DIVISION SLASH (from ‹character-fallback›) "\u2216": "\\", # SET MINUS (from ‹character-fallback›) "\u2223": "|", # DIVIDES (from ‹character-fallback›) "\u2225": "||", # PARALLEL TO (from ‹character-fallback›) "\u226A": "<<", # MUCH LESS-THAN "\u226B": ">>", # MUCH GREATER-THAN "\u2985": "((", # LEFT WHITE PARENTHESIS "\u2986": "))", # RIGHT WHITE PARENTHESIS "\u200B": "", # Zero Width Space } _re_simplify_punctuation = _re_any(_simplify_punctuation.keys()) _pathsave_simplify_punctuation = { k: sanitize_filename(v) for k, v in _simplify_punctuation.items() } def unicode_simplify_punctuation(string, pathsave=False): punctuation = _pathsave_simplify_punctuation if pathsave else _simplify_punctuation return _re_simplify_punctuation.sub(lambda m: punctuation[m.group(0)], string) _simplify_combinations = { "\u00C6": "AE", # LATIN CAPITAL LETTER AE (from ‹character-fallback›) "\u00D0": "D", # LATIN CAPITAL LETTER ETH "\u00D8": "OE", # LATIN CAPITAL LETTER O WITH STROKE (see https://en.wikipedia.org/wiki/%C3%98)
def test_keep_backslashes(self): self.assertEqual(util.sanitize_filename("AC\\DC"), "AC\\DC")
def test_win_compat(self): self.assertEqual(util.sanitize_filename("AC\\/DC", win_compat=True), "AC__DC")
def test_custom_replacement(self): self.assertEqual(util.sanitize_filename("AC/DC", "|"), "AC|DC")
def test_replace_slashes(self): self.assertEqual(util.sanitize_filename("AC/DC"), "AC_DC")
"\uFF60": "))", # FULLWIDTH RIGHT WHITE PARENTHESIS (compat)(from ‹character-fallback›) "\uFF61": ".", # HALFWIDTH IDEOGRAPHIC FULL STOP (compat) "\uFF64": ",", # HALFWIDTH IDEOGRAPHIC COMMA (compat) "\u2212": "-", # MINUS SIGN (from ‹character-fallback›) "\u2215": "/", # DIVISION SLASH (from ‹character-fallback›) "\u2216": "\\", # SET MINUS (from ‹character-fallback›) "\u2223": "|", # DIVIDES (from ‹character-fallback›) "\u2225": "||", # PARALLEL TO (from ‹character-fallback›) "\u226A": "<<", # MUCH LESS-THAN "\u226B": ">>", # MUCH GREATER-THAN "\u2985": "((", # LEFT WHITE PARENTHESIS "\u2986": "))", # RIGHT WHITE PARENTHESIS "\u200B": "", # Zero Width Space } _re_simplify_punctuation = _re_any(_simplify_punctuation.keys()) _pathsave_simplify_punctuation = {k: sanitize_filename(v) for k, v in _simplify_punctuation.items()} def unicode_simplify_punctuation(string, pathsave=False): punctuation = _pathsave_simplify_punctuation if pathsave else _simplify_punctuation return _re_simplify_punctuation.sub(lambda m: punctuation[m.group(0)], string) _simplify_combinations = { "\u00C6": "AE", # LATIN CAPITAL LETTER AE (from ‹character-fallback›) "\u00D0": "D", # LATIN CAPITAL LETTER ETH "\u00D8": "OE", # LATIN CAPITAL LETTER O WITH STROKE (see https://en.wikipedia.org/wiki/%C3%98) "\u00DE": "TH", # LATIN CAPITAL LETTER THORN "\u00DF": "ss", # LATIN SMALL LETTER SHARP S (from ‹character-fallback›) "\u00E6": "ae", # LATIN SMALL LETTER AE (from ‹character-fallback›) "\u00F0": "d", # LATIN SMALL LETTER ETH