'Manipulates an episode naming pattern and then fills the template in'

def _format_pattern(self, pattern=None, multi=None, anime_type=None):

    if (pattern is None):
        pattern = sickbeard.NAMING_PATTERN
    if (multi is None):
        multi = sickbeard.NAMING_MULTI_EP
    if sickbeard.NAMING_CUSTOM_ANIME:
        if (anime_type is None):
            anime_type = sickbeard.NAMING_ANIME
    else:
        anime_type = 3
    replace_map = self._replace_map()
    result_name = pattern
    if (replace_map['%RG'] and (replace_map['%RG'] != u'SickRage')):
        if (not hasattr(self, u'_release_group')):
            logger.log(((u"Episode    has    no    release    group,    replacing    it    with    '" + replace_map['%RG']) + u"'"), logger.DEBUG)
            self._release_group = replace_map['%RG']
        elif (not self._release_group):
            logger.log(((u"Episode    has    no    release    group,    replacing    it    with    '" + replace_map['%RG']) + u"'"), logger.DEBUG)
            self._release_group = replace_map['%RG']
    if (not replace_map['%RN']):
        if (self.show.air_by_date or self.show.sports):
            result_name = result_name.replace(u'%RN', (u'%S.N.%A.D.%E.N-' + replace_map['%RG']))
            result_name = result_name.replace(u'%rn', (u'%s.n.%A.D.%e.n-' + replace_map['%RG'].lower()))
        elif (anime_type != 3):
            result_name = result_name.replace(u'%RN', (u'%S.N.%AB.%E.N-' + replace_map['%RG']))
            result_name = result_name.replace(u'%rn', (u'%s.n.%ab.%e.n-' + replace_map['%RG'].lower()))
        else:
            result_name = result_name.replace(u'%RN', (u'%S.N.S%0SE%0E.%E.N-' + replace_map['%RG']))
            result_name = result_name.replace(u'%rn', (u'%s.n.s%0se%0e.%e.n-' + replace_map['%RG'].lower()))
    if (not replace_map['%RT']):
        result_name = re.sub(u'([    _.-]*)%RT([    _.-]*)', u'\\2', result_name)
    name_groups = re.split(u'[\\\\/]', result_name)
    for cur_name_group in name_groups:
        season_format = sep = ep_sep = ep_format = None
        season_ep_regex = u'\n                                                                                                                                (?P<pre_sep>[    _.-]*)\n                                                                                                                                ((?:s(?:eason|eries)?\\s*)?%0?S(?![._]?N))\n                                                                                                                                (.*?)\n                                                                                                                                (%0?E(?![._]?N))\n                                                                                                                                (?P<post_sep>[    _.-]*)\n                                                                                                                        '
        ep_only_regex = u'(E?%0?E(?![._]?N))'
        season_ep_match = re.search(season_ep_regex, cur_name_group, (re.I | re.X))
        ep_only_match = re.search(ep_only_regex, cur_name_group, (re.I | re.X))
        if season_ep_match:
            season_format = season_ep_match.group(2)
            ep_sep = season_ep_match.group(3)
            ep_format = season_ep_match.group(4)
            sep = season_ep_match.group(u'pre_sep')
            if (not sep):
                sep = season_ep_match.group(u'post_sep')
            if (not sep):
                sep = u'    '
            if (multi in (NAMING_EXTEND, NAMING_LIMITED_EXTEND, NAMING_LIMITED_EXTEND_E_PREFIXED)):
                ep_sep = u'-'
            regex_used = season_ep_regex
        elif ep_only_match:
            season_format = u''
            ep_sep = u'-'
            ep_format = ep_only_match.group(1)
            sep = u''
            regex_used = ep_only_regex
        else:
            continue
        if ((not ep_sep) or (not ep_format)):
            continue
        ep_string = self._format_string(ep_format.upper(), replace_map)
        for other_ep in self.relatedEps:
            if ((multi in (NAMING_LIMITED_EXTEND, NAMING_LIMITED_EXTEND_E_PREFIXED)) and (other_ep != self.relatedEps[(-1)])):
                continue
            elif (multi == NAMING_DUPLICATE):
                ep_string += (sep + season_format)
            elif (multi == NAMING_SEPARATED_REPEAT):
                ep_string += sep
            ep_string += ep_sep
            if (multi == NAMING_LIMITED_EXTEND_E_PREFIXED):
                ep_string += u'E'
            ep_string += other_ep._format_string(ep_format.upper(), other_ep._replace_map())
        if (anime_type != 3):
            if (self.absolute_number == 0):
                curAbsolute_number = self.episode
            else:
                curAbsolute_number = self.absolute_number
            if (self.season != 0):
                if (anime_type == 1):
                    ep_string += (sep + u'{#:03d}'.format(**{u'#': curAbsolute_number}))
                elif (anime_type == 2):
                    ep_string = u'{#:03d}'.format(**{u'#': curAbsolute_number})
                for relEp in self.relatedEps:
                    if (relEp.absolute_number != 0):
                        ep_string += (u'-' + u'{#:03d}'.format(**{u'#': relEp.absolute_number}))
                    else:
                        ep_string += (u'-' + u'{#:03d}'.format(**{u'#': relEp.episode}))
        regex_replacement = None
        if ((anime_type == 2) and (not ep_only_match)):
            regex_replacement = ((u'\\g<pre_sep>' + ep_string) + u'\\g<post_sep>')
        elif season_ep_match:
            regex_replacement = ((u'\\g<pre_sep>\\g<2>\\g<3>' + ep_string) + u'\\g<post_sep>')
        elif ep_only_match:
            regex_replacement = ep_string
        if regex_replacement:
            cur_name_group_result = re.sub((u'(?i)(?x)' + regex_used), regex_replacement, cur_name_group)
            result_name = result_name.replace(cur_name_group, cur_name_group_result)
    result_name = self._format_string(result_name, replace_map)
    logger.log((((u'formatting    pattern:    ' + pattern) + u'    ->    ') + result_name), logger.DEBUG)
    return result_name
