# This file is part of beets.
# Copyright 2015, Blemjhoo Tezoulbr <baobab@heresiarch.info>.
#
# 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.

""" Clears tag fields in media files."""

from __future__ import (division, absolute_import, print_function,
                        unicode_literals)

import re
from beets.plugins import BeetsPlugin
from beets.mediafile import MediaFile
from beets.importer import action
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({
            'fields': [],
            'keep_fields': [],
            'update_database': False,
        })

        self.patterns = {}
        self.warned = False

        if self.config['fields']:
            self.validate_config('fields')
            for field in self.config['fields'].as_str_seq():
                self.set_pattern(field)

        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)

            # These fields should be preserved
            for key in ('id', 'path', 'album_id'):
                if key in self.patterns:
                    del self.patterns[key]

    def validate_config(self, mode):
        """Check if fields written in config are correct."""
        if self.config['fields'] and self.config['keep_fields']:
            self._log.warn(u'cannot blacklist and whitelist at the same time')
        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

    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.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

        for field, patterns in self.patterns.items():
            if field in tags:
                value = tags[field]
                match = self.match_patterns(tags[field], patterns)
            else:
                value = ''
                match = patterns is True

            if match:
                self._log.debug(u'{0}: {1} -> None', field, value)
                tags[field] = None
                if self.config['update_database']:
                    item[field] = None

@@ -1534,24 +1534,264 @@
 ed = False%0A%0A
+        # We'll only handle %60fields%60 or %60keep_fields%60, but not both.%0A        if self.config%5B'fields'%5D and self.config%5B'keep_fields'%5D:%0A            self._log.warn(u'cannot blacklist and whitelist at the same time')%0A%0A        # Blacklist mode.%0A
         if s
@@ -1804,32 +1804,32 @@
 nfig%5B'fields'%5D:%0A
-
             self
@@ -1949,32 +1949,58 @@
 pattern(field)%0A%0A
+        # Whitelist mode.%0A
         elif sel
@@ -2291,16 +2291,23 @@
  should 
+always 
 be prese
@@ -2310,16 +2310,17 @@
 reserved
+.
 %0A       
@@ -2506,18 +2506,23 @@
 eck 
-if
+whether
  fields 
 writ
@@ -2521,185 +2521,176 @@
 lds 
-written in config are correct.%22%22%22%0A        if self.config%5B'fields'%5D and self.config%5B'keep_fields'%5D:%0A            self._log.warn(u'cannot blacklist and whitelist at the same time')
+in the configuration are valid.%0A%0A        %60mode%60 should either be %22fields%22 or %22keep_fields%22, indicating%0A        the section of the configuration to validate.%0A        %22%22%22
 %0A   

Check for conflicts only once (#1641)