<commit_before>import logging
import os
import re

from . import errors
from .lib.thetvdb import TheTvDb
from .lib.tvrage import TvRage

log = logging.getLogger('Core')


class TvRenamr(object):
    def __init__(self, working_dir, config, debug=False, dry=False):
        """
        :param working_dir: The working directory.
        :type working_dir: A string.

        :param log_level: The log level to set.
        :type log_level: A string that defaults to info.

        :param log_file: The location to use for the log file
        :type log_file: A string or None.
        """
        self.working_dir = working_dir
        self.dry = dry
        self.debug = debug
        self.config = config

    def remove_part_from_multiple_episodes(self, show_name):
        """
        In episode titles of multiple part episodes that use the format
        (Part n) remove the 'Part ' section so the format is (n).

        :param show_name: The show name to sanitise.
        :type show_name: A string.

        :returns: The show name with sanitised multi-episode section.
        :rtype: A string.
        """
        log.debug('Removing Part from episode name')
        return show_name.replace('(Part ', '(')

    def extract_details_from_file(self, fn, user_regex=None):
        """
        Looks at the file given and extracts from it the show title, it's
        season number and episode number using regular expression magic.
        The default formats accepted are: series.0x00.xxx or series.s0e00.xxx
        A user can specify their own regular expression for a format not
        already covered.

        :param fn: The file name passed in.
        :type fn: String.

        :param user_regex: A user specified regular expression.
        :type user_regex: A string or None.

        :raises UnexpectedFormatException: Raised if the file is in an
        unexpected format.

        :returns: The show name, season number, episode number and last four
        characters (assumed to be the extension) extracted from the file
        passed in.
        :rtype: A dictionary with the keys 'show', 'season', 'episode' and
        'extension'.
        """
        fn = fn.replace("_", ".").replace(" ", ".")
        log.log(22, 'Renaming: %s' % fn)
        regex = self.__build_regex(user_regex)
        log.debug('Renaming using: ' + regex)
        m = re.compile(regex).match(fn)
        if not m:
            raise errors.UnexpectedFormatException(fn)

        credentials = {}
        try:
            credentials.update({'show_name': m.group('show_name').replace('.', ' ').strip()})
        except IndexError:
            pass
        try:
            credentials.update({'season': m.group('season')})
        except IndexError:
            pass
        try:
            credentials.update({'episode': m.group('episode')})
        except IndexError:
            pass
        credentials.update({'extension': os.path.splitext(fn)[1]})
        log.debug('Filename yielded: {0}'.format(
            ', '.join('{0}: {1}'.format(key, value)
            for key, value in credentials.iteritems())<commit_msg>Use dict.items for better compatibility<commit_after>import logging
import os
import re

from . import errors
from .lib.thetvdb import TheTvDb
from .lib.tvrage import TvRage

log = logging.getLogger('Core')


class TvRenamr(object):
    def __init__(self, working_dir, config, debug=False, dry=False):
        """
        :param working_dir: The working directory.
        :type working_dir: A string.

        :param log_level: The log level to set.
        :type log_level: A string that defaults to info.

        :param log_file: The location to use for the log file
        :type log_file: A string or None.
        """
        self.working_dir = working_dir
        self.dry = dry
        self.debug = debug
        self.config = config

    def remove_part_from_multiple_episodes(self, show_name):
        """
        In episode titles of multiple part episodes that use the format
        (Part n) remove the 'Part ' section so the format is (n).

        :param show_name: The show name to sanitise.
        :type show_name: A string.

        :returns: The show name with sanitised multi-episode section.
        :rtype: A string.
        """
        log.debug('Removing Part from episode name')
        return show_name.replace('(Part ', '(')

    def extract_details_from_file(self, fn, user_regex=None):
        """
        Looks at the file given and extracts from it the show title, it's
        season number and episode number using regular expression magic.
        The default formats accepted are: series.0x00.xxx or series.s0e00.xxx
        A user can specify their own regular expression for a format not
        already covered.

        :param fn: The file name passed in.
        :type fn: String.

        :param user_regex: A user specified regular expression.
        :type user_regex: A string or None.

        :raises UnexpectedFormatException: Raised if the file is in an
        unexpected format.

        :returns: The show name, season number, episode number and last four
        characters (assumed to be the extension) extracted from the file
        passed in.
        :rtype: A dictionary with the keys 'show', 'season', 'episode' and
        'extension'.
        """
        fn = fn.replace("_", ".").replace(" ", ".")
        log.log(22, 'Renaming: %s' % fn)
        regex = self.__build_regex(user_regex)
        log.debug('Renaming using: ' + regex)
        m = re.compile(regex).match(fn)
        if not m:
            raise errors.UnexpectedFormatException(fn)

        credentials = {}
        try:
            credentials.update({'show_name': m.group('show_name').replace('.', ' ').strip()})
        except IndexError:
            pass
        try:
            credentials.update({'season': m.group('season')})
        except IndexError:
            pass
        try:
            credentials.update({'episode': m.group('episode')})
        except IndexError:
            pass
        credentials.update({'extension': os.path.splitext(fn)[1]})
        log.debug('Filename yielded: {0}'.format(
            ', '.join('{0}: {1}'.format(key, value)
            for key, value in credentials.items())
{'fraction_non_alphanumeric': 0.0997446536865624, 'fraction_numerical': 0.003830194701563996, 'mean_word_length': 2.25051867219917, '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': '1255049', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1719, 'n_words': 597}