def _delete(self, file_path, associated_files=False): """ Deletes the file and optionally all associated files. file_path: The file to delete associated_files: True to delete all files which differ only by extension, False to leave them """ if not file_path: return # figure out which files we want to delete file_list = [file_path] if associated_files: file_list = file_list + helpers.list_associated_files(file_path, base_name_only=True) if not file_list: self._log(u"There were no files associated with " + file_path + ", not deleting anything", logger.DEBUG) return # delete the file and any other files which we want to delete for cur_file in file_list: if ek.ek(os.path.isfile, cur_file): self._log(u"Deleting file " + cur_file, logger.DEBUG) ek.ek(os.remove, cur_file) # do the library update for synoindex notifiers.synoindex_notifier.deleteFile(cur_file)
def _combined_file_operation(self, file_path, new_path, new_base_name, associated_files=False, action=None): """ Performs a generic operation (move or copy) on a file. Can rename the file as well as change its location, and optionally move associated files too. file_path: The full path of the media file to act on new_path: Destination path where we want to move/copy the file to new_base_name: The base filename (no extension) to use during the copy. Use None to keep the same name. associated_files: Boolean, whether we should copy similarly-named files too action: function that takes an old path and new path and does an operation with them (move/copy) """ if not action: self._log( u"Must provide an action for the combined file operation", logger.ERROR) return file_list = [file_path] if associated_files: file_list = file_list + helpers.list_associated_files( file_path, filter_ext=sickbeard.FILTER_ASSOCIATED_FILES) if not file_list: self._log( u"There were no files associated with " + file_path + ", not moving anything", logger.DEBUG) return # create base name with file_path (media_file without .extension) old_base_name = file_path.rpartition('.')[0] old_base_name_length = len(old_base_name) # deal with all files for cur_file_path in file_list: cur_file_name = ek.ek(os.path.basename, cur_file_path) # get the extension without . cur_extension = cur_file_path[old_base_name_length + 1:] # replace .nfo with .nfo-orig to avoid conflicts if cur_extension == 'nfo': cur_extension = 'nfo-orig' # If new base name then convert name if new_base_name: new_file_name = new_base_name + '.' + cur_extension # if we're not renaming we still want to change extensions sometimes else: new_file_name = helpers.replaceExtension( cur_file_name, cur_extension) new_file_path = ek.ek(os.path.join, new_path, new_file_name) action(cur_file_path, new_file_path)
def _combined_file_operation(self, file_path, new_path, new_base_name, associated_files=False, action=None): """ Performs a generic operation (move or copy) on a file. Can rename the file as well as change its location, and optionally move associated files too. file_path: The full path of the media file to act on new_path: Destination path where we want to move/copy the file to new_base_name: The base filename (no extension) to use during the copy. Use None to keep the same name. associated_files: Boolean, whether we should copy similarly-named files too action: function that takes an old path and new path and does an operation with them (move/copy) """ if not action: self._log(u"Must provide an action for the combined file operation", logger.ERROR) return file_list = [file_path] if associated_files: file_list = file_list + helpers.list_associated_files(file_path, filter_ext=sickbeard.FILTER_ASSOCIATED_FILES) if not file_list: self._log(u"There were no files associated with " + file_path + ", not moving anything", logger.DEBUG) return # create base name with file_path (media_file without .extension) old_base_name = file_path.rpartition('.')[0] old_base_name_length = len(old_base_name) # deal with all files for cur_file_path in file_list: cur_file_name = ek.ek(os.path.basename, cur_file_path) # get the extension without . cur_extension = cur_file_path[old_base_name_length + 1:] cur_file_name = ek.ek(os.path.basename, cur_file_path) # get the extension cur_extension = cur_file_path.rpartition('.')[-1] # replace .nfo with .nfo-orig to avoid conflicts if cur_extension == 'nfo': cur_extension = 'nfo-orig' # If new base name then convert name if new_base_name: new_file_name = new_base_name + '.' + cur_extension # if we're not renaming we still want to change extensions sometimes else: new_file_name = helpers.replaceExtension(cur_file_name, cur_extension) new_file_path = ek.ek(os.path.join, new_path, new_file_name) action(cur_file_path, new_file_path)