File: scrub.py

package info (click to toggle)
beets 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,016 kB
  • sloc: python: 46,429; javascript: 8,018; xml: 334; sh: 261; makefile: 125
file content (151 lines) | stat: -rw-r--r-- 5,115 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# This file is part of beets.
# Copyright 2016, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Cleans extraneous metadata from files' tags via a command or
automatically whenever tags are written.
"""

import mediafile
import mutagen

from beets import config, ui, util
from beets.plugins import BeetsPlugin

_MUTAGEN_FORMATS = {
    "asf": "ASF",
    "apev2": "APEv2File",
    "flac": "FLAC",
    "id3": "ID3FileType",
    "mp3": "MP3",
    "mp4": "MP4",
    "oggflac": "OggFLAC",
    "oggspeex": "OggSpeex",
    "oggtheora": "OggTheora",
    "oggvorbis": "OggVorbis",
    "oggopus": "OggOpus",
    "trueaudio": "TrueAudio",
    "wavpack": "WavPack",
    "monkeysaudio": "MonkeysAudio",
    "optimfrog": "OptimFROG",
}


class ScrubPlugin(BeetsPlugin):
    """Removes extraneous metadata from files' tags."""

    def __init__(self):
        super().__init__()
        self.config.add(
            {
                "auto": True,
            }
        )

        if self.config["auto"]:
            self.register_listener("import_task_files", self.import_task_files)

    def commands(self):
        def scrub_func(lib, opts, args):
            # Walk through matching files and remove tags.
            for item in lib.items(args):
                self._log.info("scrubbing: {.filepath}", item)
                self._scrub_item(item, opts.write)

        scrub_cmd = ui.Subcommand("scrub", help="clean audio tags")
        scrub_cmd.parser.add_option(
            "-W",
            "--nowrite",
            dest="write",
            action="store_false",
            default=True,
            help="leave tags empty",
        )
        scrub_cmd.func = scrub_func

        return [scrub_cmd]

    @staticmethod
    def _mutagen_classes():
        """Get a list of file type classes from the Mutagen module."""
        classes = []
        for modname, clsname in _MUTAGEN_FORMATS.items():
            mod = __import__(f"mutagen.{modname}", fromlist=[clsname])
            classes.append(getattr(mod, clsname))
        return classes

    def _scrub(self, path):
        """Remove all tags from a file."""
        for cls in self._mutagen_classes():
            # Try opening the file with this type, but just skip in the
            # event of any error.
            try:
                f = cls(util.syspath(path))
            except Exception:
                continue
            if f.tags is None:
                continue

            # Remove the tag for this type.
            try:
                f.delete()
            except NotImplementedError:
                # Some Mutagen metadata subclasses (namely, ASFTag) do not
                # support .delete(), presumably because it is impossible to
                # remove them. In this case, we just remove all the tags.
                for tag in f.keys():
                    del f[tag]
                f.save()
            except (OSError, mutagen.MutagenError) as exc:
                self._log.error(
                    "could not scrub {}: {}", util.displayable_path(path), exc
                )

    def _scrub_item(self, item, restore):
        """Remove tags from an Item's associated file and, if `restore`
        is enabled, write the database's tags back to the file.
        """
        # Get album art if we need to restore it.
        if restore:
            try:
                mf = mediafile.MediaFile(
                    util.syspath(item.path), config["id3v23"].get(bool)
                )
            except mediafile.UnreadableFileError as exc:
                self._log.error("could not open file to scrub: {}", exc)
                return
            images = mf.images

        # Remove all tags.
        self._scrub(item.path)

        # Restore tags, if enabled.
        if restore:
            self._log.debug("writing new tags after scrub")
            item.try_write()
            if images:
                self._log.debug("restoring art")
                try:
                    mf = mediafile.MediaFile(
                        util.syspath(item.path), config["id3v23"].get(bool)
                    )
                    mf.images = images
                    mf.save()
                except mediafile.UnreadableFileError as exc:
                    self._log.error("could not write tags: {}", exc)

    def import_task_files(self, session, task):
        """Automatically scrub imported files."""
        for item in task.imported_items():
            self._log.debug("auto-scrubbing {.filepath}", item)
            self._scrub_item(item, ui.should_write())