mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-19 21:03:14 -07:00
Update beets to 1.4.7
Also updates: - colorama-0.4.1 - jellyfish-0.6.1 - munkres-1.0.12 - musicbrainzngs-0.6 - mutagen-1.41.1 - pyyaml-3.13 - six-1.12.0 - unidecode-1.0.23
This commit is contained in:
parent
05b0fb498f
commit
e854005ae1
193 changed files with 15896 additions and 6384 deletions
|
@ -16,125 +16,148 @@
|
|||
""" Clears tag fields in media files."""
|
||||
|
||||
from __future__ import division, absolute_import, print_function
|
||||
import six
|
||||
|
||||
import re
|
||||
|
||||
from beets.plugins import BeetsPlugin
|
||||
from beets.mediafile import MediaFile
|
||||
from beets.importer import action
|
||||
from beets.ui import Subcommand, decargs, input_yn
|
||||
from beets.util import confit
|
||||
|
||||
__author__ = 'baobab@heresiarch.info'
|
||||
__version__ = '0.10'
|
||||
|
||||
|
||||
class ZeroPlugin(BeetsPlugin):
|
||||
|
||||
_instance = None
|
||||
|
||||
def __init__(self):
|
||||
super(ZeroPlugin, self).__init__()
|
||||
|
||||
# Listeners.
|
||||
self.register_listener('write', self.write_event)
|
||||
self.register_listener('import_task_choice',
|
||||
self.import_task_choice_event)
|
||||
|
||||
self.config.add({
|
||||
'auto': True,
|
||||
'fields': [],
|
||||
'keep_fields': [],
|
||||
'update_database': False,
|
||||
})
|
||||
|
||||
self.patterns = {}
|
||||
self.fields_to_progs = {}
|
||||
self.warned = False
|
||||
|
||||
# We'll only handle `fields` or `keep_fields`, but not both.
|
||||
"""Read the bulk of the config into `self.fields_to_progs`.
|
||||
After construction, `fields_to_progs` contains all the fields that
|
||||
should be zeroed as keys and maps each of those to a list of compiled
|
||||
regexes (progs) as values.
|
||||
A field is zeroed if its value matches one of the associated progs. If
|
||||
progs is empty, then the associated field is always zeroed.
|
||||
"""
|
||||
if self.config['fields'] and self.config['keep_fields']:
|
||||
self._log.warn(u'cannot blacklist and whitelist at the same time')
|
||||
|
||||
self._log.warning(
|
||||
u'cannot blacklist and whitelist at the same time'
|
||||
)
|
||||
# Blacklist mode.
|
||||
if self.config['fields']:
|
||||
self.validate_config('fields')
|
||||
elif self.config['fields']:
|
||||
for field in self.config['fields'].as_str_seq():
|
||||
self.set_pattern(field)
|
||||
|
||||
self._set_pattern(field)
|
||||
# Whitelist mode.
|
||||
elif self.config['keep_fields']:
|
||||
self.validate_config('keep_fields')
|
||||
|
||||
for field in MediaFile.fields():
|
||||
if field in self.config['keep_fields'].as_str_seq():
|
||||
continue
|
||||
self.set_pattern(field)
|
||||
if (field not in self.config['keep_fields'].as_str_seq() and
|
||||
# These fields should always be preserved.
|
||||
field not in ('id', 'path', 'album_id')):
|
||||
self._set_pattern(field)
|
||||
|
||||
# These fields should always be preserved.
|
||||
for key in ('id', 'path', 'album_id'):
|
||||
if key in self.patterns:
|
||||
del self.patterns[key]
|
||||
def commands(self):
|
||||
zero_command = Subcommand('zero', help='set fields to null')
|
||||
|
||||
def validate_config(self, mode):
|
||||
"""Check whether fields in the configuration are valid.
|
||||
def zero_fields(lib, opts, args):
|
||||
if not decargs(args) and not input_yn(
|
||||
u"Remove fields for all items? (Y/n)",
|
||||
True):
|
||||
return
|
||||
for item in lib.items(decargs(args)):
|
||||
self.process_item(item)
|
||||
|
||||
`mode` should either be "fields" or "keep_fields", indicating
|
||||
the section of the configuration to validate.
|
||||
zero_command.func = zero_fields
|
||||
return [zero_command]
|
||||
|
||||
def _set_pattern(self, field):
|
||||
"""Populate `self.fields_to_progs` for a given field.
|
||||
Do some sanity checks then compile the regexes.
|
||||
"""
|
||||
for field in self.config[mode].as_str_seq():
|
||||
if field not in MediaFile.fields():
|
||||
self._log.error(u'invalid field: {0}', field)
|
||||
continue
|
||||
if mode == 'fields' and field in ('id', 'path', 'album_id'):
|
||||
self._log.warn(u'field \'{0}\' ignored, zeroing '
|
||||
u'it would be dangerous', field)
|
||||
continue
|
||||
|
||||
def set_pattern(self, field):
|
||||
"""Set a field in `self.patterns` to a string list corresponding to
|
||||
the configuration, or `True` if the field has no specific
|
||||
configuration.
|
||||
"""
|
||||
try:
|
||||
self.patterns[field] = self.config[field].as_str_seq()
|
||||
except confit.NotFoundError:
|
||||
# Matches everything
|
||||
self.patterns[field] = True
|
||||
if field not in MediaFile.fields():
|
||||
self._log.error(u'invalid field: {0}', field)
|
||||
elif field in ('id', 'path', 'album_id'):
|
||||
self._log.warning(u'field \'{0}\' ignored, zeroing '
|
||||
u'it would be dangerous', field)
|
||||
else:
|
||||
try:
|
||||
for pattern in self.config[field].as_str_seq():
|
||||
prog = re.compile(pattern, re.IGNORECASE)
|
||||
self.fields_to_progs.setdefault(field, []).append(prog)
|
||||
except confit.NotFoundError:
|
||||
# Matches everything
|
||||
self.fields_to_progs[field] = []
|
||||
|
||||
def import_task_choice_event(self, session, task):
|
||||
"""Listen for import_task_choice event."""
|
||||
if task.choice_flag == action.ASIS and not self.warned:
|
||||
self._log.warn(u'cannot zero in \"as-is\" mode')
|
||||
self._log.warning(u'cannot zero in \"as-is\" mode')
|
||||
self.warned = True
|
||||
# TODO request write in as-is mode
|
||||
|
||||
@classmethod
|
||||
def match_patterns(cls, field, patterns):
|
||||
"""Check if field (as string) is matching any of the patterns in
|
||||
the list.
|
||||
"""
|
||||
if patterns is True:
|
||||
return True
|
||||
for p in patterns:
|
||||
if re.search(p, unicode(field), flags=re.IGNORECASE):
|
||||
return True
|
||||
return False
|
||||
|
||||
def write_event(self, item, path, tags):
|
||||
"""Set values in tags to `None` if the key and value are matched
|
||||
by `self.patterns`.
|
||||
"""
|
||||
if not self.patterns:
|
||||
self._log.warn(u'no fields, nothing to do')
|
||||
return
|
||||
if self.config['auto']:
|
||||
self.set_fields(item, tags)
|
||||
|
||||
for field, patterns in self.patterns.items():
|
||||
def set_fields(self, item, tags):
|
||||
"""Set values in `tags` to `None` if the field is in
|
||||
`self.fields_to_progs` and any of the corresponding `progs` matches the
|
||||
field value.
|
||||
Also update the `item` itself if `update_database` is set in the
|
||||
config.
|
||||
"""
|
||||
fields_set = False
|
||||
|
||||
if not self.fields_to_progs:
|
||||
self._log.warning(u'no fields, nothing to do')
|
||||
return False
|
||||
|
||||
for field, progs in self.fields_to_progs.items():
|
||||
if field in tags:
|
||||
value = tags[field]
|
||||
match = self.match_patterns(tags[field], patterns)
|
||||
match = _match_progs(tags[field], progs)
|
||||
else:
|
||||
value = ''
|
||||
match = patterns is True
|
||||
match = not progs
|
||||
|
||||
if match:
|
||||
fields_set = True
|
||||
self._log.debug(u'{0}: {1} -> None', field, value)
|
||||
tags[field] = None
|
||||
if self.config['update_database']:
|
||||
item[field] = None
|
||||
|
||||
return fields_set
|
||||
|
||||
def process_item(self, item):
|
||||
tags = dict(item)
|
||||
|
||||
if self.set_fields(item, tags):
|
||||
item.write(tags=tags)
|
||||
if self.config['update_database']:
|
||||
item.store(fields=tags)
|
||||
|
||||
|
||||
def _match_progs(value, progs):
|
||||
"""Check if `value` (as string) is matching any of the compiled regexes in
|
||||
the `progs` list.
|
||||
"""
|
||||
if not progs:
|
||||
return True
|
||||
for prog in progs:
|
||||
if prog.search(six.text_type(value)):
|
||||
return True
|
||||
return False
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue