def erase(self, pl_ids): if len(pl_ids) > 0: magic_or_static = with_polymorphic(MediaList, "*") pls = Session.query(magic_or_static)\ .filter(MediaList.id.in_(pl_ids))\ .all() for pl in pls: Session.delete(pl) Session.commit() self.dispatch_signame('recpls.listupdate')
def _get_file_info(self, file_obj): try: file_obj = self.parser.parse(file_obj, Session) except Exception: if util.has_identity(file_obj): Session.delete(file_obj) else: Session.expunge(file_obj) log_traceback(level="info") return None return file_obj
def remove_directory(self, path, name): dir_path = os.path.join(path, name) folder = self._get_folder(dir_path) if folder is not None: def stop_watchers(d_obj): for ch_obj in d_obj.child_folders: stop_watchers(ch_obj) self.watcher.stop_watching_dir(d_obj.path) stop_watchers(folder) Session.delete(folder)
def walk_directory(self, walk_root, force=False): walk_root = os.path.join(self.root_path, walk_root) walk_root = walk_root.rstrip("/") walked_folders = [] # cache all medias recorded in database media_dict = {} for m in Session.query(self.OBJECT_CLASS).all(): media_dict[m.get_path()] = m for root, subdirs, files in pathutils.walk(walk_root): root = root.rstrip("/") log.debug('library: update crawling %s' % root) if os.path.basename(root).startswith("."): continue # skip hidden folder folder = self._get_folder(root) if folder is None: folder = LibraryFolder(library_id=self.library_id, path=root, name=os.path.basename(root)) Session.add(folder) self.parse_files(folder, files, media_dict, force) subdirs_obj = self.get_subdirs(folder, subdirs) for sd in subdirs_obj: sd.parent_folder = folder walked_folders.append(root) # clean library Session.query(LibraryFolder) \ .filter(LibraryFolder.library_id == self.library_id) \ .filter(LibraryFolder.path.notin_(walked_folders)) \ .delete(synchronize_session='fetch') if len(media_dict) > 0: db_media_ids = [media_dict[k].m_id for k in media_dict] erased_files = Session.query(self.OBJECT_CLASS) \ .filter(self.OBJECT_CLASS.m_id.in_(db_media_ids)) \ .all() for f in erased_files: Session.delete(f) Session.commit()
def remove_media(self, path, name): file_obj = self._get_file_with_path(os.path.join(path, name)) if file_obj is not None: Session.delete(file_obj)