'Renames an episode file and all related files to the location and filename as specified
in the naming settings.'

def rename(self):

    if (not os.path.isfile(self.location)):
        sickrage.srCore.srLogger.warning(((u"Can't    perform    rename    on    " + self.location) + u"    when    it    doesn't    exist,    skipping"))
        return
    proper_path = self.proper_path()
    absolute_proper_path = os.path.join(self.show.location, proper_path)
    (absolute_current_path_no_ext, file_ext) = os.path.splitext(self.location)
    absolute_current_path_no_ext_length = len(absolute_current_path_no_ext)
    related_subs = []
    current_path = absolute_current_path_no_ext
    if absolute_current_path_no_ext.startswith(self.show.location):
        current_path = absolute_current_path_no_ext[len(self.show.location):]
    sickrage.srCore.srLogger.debug((((u'Renaming/moving    episode    from    the    base    path    ' + self.location) + u'    to    ') + absolute_proper_path))
    if (proper_path == current_path):
        sickrage.srCore.srLogger.debug((((str(self.indexerid) + u':    File    ') + self.location) + u'    is    already    named    correctly,    skipping'))
        return
    related_files = PostProcessor(self.location).list_associated_files(self.location, base_name_only=True, subfolders=True)
    if (self.show.subtitles and (sickrage.srCore.srConfig.SUBTITLES_DIR != u'')):
        related_subs = PostProcessor(self.location).list_associated_files(sickrage.srCore.srConfig.SUBTITLES_DIR, subtitles_only=True, subfolders=True)
        absolute_proper_subs_path = os.path.join(sickrage.srCore.srConfig.SUBTITLES_DIR, self.formatted_filename())
    sickrage.srCore.srLogger.debug((((u'Files    associated    to    ' + self.location) + u':    ') + str(related_files)))
    result = self.rename_ep_file(self.location, absolute_proper_path, absolute_current_path_no_ext_length)
    for cur_related_file in related_files:
        cur_related_dir = os.path.dirname(os.path.abspath(cur_related_file))
        subfolder = cur_related_dir.replace(os.path.dirname(os.path.abspath(self.location)), u'')
        proper_related_dir = os.path.dirname(os.path.abspath((absolute_proper_path + file_ext)))
        proper_related_path = absolute_proper_path.replace(proper_related_dir, (proper_related_dir + subfolder))
        cur_result = self.rename_ep_file(cur_related_file, proper_related_path, (absolute_current_path_no_ext_length + len(subfolder)))
        if (not cur_result):
            sickrage.srCore.srLogger.error(((str(self.indexerid) + u':    Unable    to    rename    file    ') + cur_related_file))
    for cur_related_sub in related_subs:
        absolute_proper_subs_path = os.path.join(sickrage.srCore.srConfig.SUBTITLES_DIR, self.formatted_filename())
        cur_result = self.rename_ep_file(cur_related_sub, absolute_proper_subs_path, absolute_current_path_no_ext_length)
        if (not cur_result):
            sickrage.srCore.srLogger.error(((str(self.indexerid) + u':    Unable    to    rename    file    ') + cur_related_sub))
    with self.lock:
        if result:
            self.location = (absolute_proper_path + file_ext)
            for relEp in self.relatedEps:
                relEp.location = (absolute_proper_path + file_ext)
    for curEp in ([self] + self.relatedEps):
        curEp.checkForMetaFiles()
    with self.lock:
        for relEp in ([self] + self.relatedEps):
            relEp.saveToDB()
