<commit_before>import logging
import platform
from pathlib import Path

from flexget import plugin
from flexget.config_schema import one_or_more
from flexget.event import event

log = logging.getLogger('exists')


class FilterExists:
    """
        Reject entries that already exist in given path.

        Example::

          exists: /storage/movies/
    """

    schema = one_or_more({'type': 'string', 'format': 'path'})

    def prepare_config(self, config):
        # If only a single path is passed turn it into a 1 element list
        if isinstance(config, str):
            config = [config]
        return config

    @plugin.priority(-1)
    def on_task_filter(self, task, config):
        if not task.accepted:
            log.debug('No accepted entries, not scanning for existing.')
            return
        log.verbose('Scanning path(s) for existing files.')
        config = self.prepare_config(config)
        filenames = {}
        for folder in config:
            folder = Path(folder).expanduser()
            if not folder.exists():
                raise plugin.PluginWarning('Path %s does not exist' % folder, log)
            for p in folder.walk(errors='ignore'):
                key = p.name
                # windows file system is not case sensitive
                if platform.system() == 'Windows':
                    key = key.lower()
                filenames[key] = p
        for entry in task.accepted:
            # priority is: filename, location (filename only), title
            name = Path(entry.get('filename', entry.get('location', entry['title']))).name
            if platform.system() == 'Windows':
                name = name.lower()
            if name in filenames:
                log.debug('Found %s in %s' % (name, filenames[name]))<commit_msg>[fix] Replace folder.walk with folder.rglob in exist (#2495)<commit_after>import logging
import platform
from pathlib import Path

from flexget import plugin
from flexget.config_schema import one_or_more
from flexget.event import event

log = logging.getLogger('exists')


class FilterExists:
    """
        Reject entries that already exist in given path.

        Example::

          exists: /storage/movies/
    """

    schema = one_or_more({'type': 'string', 'format': 'path'})

    def prepare_config(self, config):
        # If only a single path is passed turn it into a 1 element list
        if isinstance(config, str):
            config = [config]
        return config

    @plugin.priority(-1)
    def on_task_filter(self, task, config):
        if not task.accepted:
            log.debug('No accepted entries, not scanning for existing.')
            return
        log.verbose('Scanning path(s) for existing files.')
        config = self.prepare_config(config)
        filenames = {}
        for folder in config:
            folder = Path(folder).expanduser()
            if not folder.exists():
                raise plugin.PluginWarning('Path %s does not exist' % folder, log)
            for p in folder.rglob('*'):
                if p.is_file():
                    key = p.name
                    # windows file system is not case sensitive
                    if platform.system() == 'Windows':
                        key = key.lower()
                    filenames[key] = p
        for entry in task.accepted:
            # priority is: filename, location (filename only), title
            name = Path(entry.get('filename', entry.get('location', entry['title']))).name
            if platform.system() == 'Windows':
                name = name.lower()
            if name in filenames:
                log.debug('Found %s in %s' % (name, filenames[name]))
{'fraction_non_alphanumeric': 0.10336408030385241, 'fraction_numerical': 0.002170374389582203, 'mean_word_length': 2.0147179067865904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3081481', 'n_tokens_mistral': 983, 'n_tokens_neox': 943, 'n_words': 318}