def peewee_database(): from cozy.db.track import Track from cozy.db.book import Book from cozy.db.settings import Settings from cozy.db.storage_blacklist import StorageBlackList from cozy.db.storage import Storage db_path, models, test_db = prepare_db() path_of_test_folder = os.path.dirname(os.path.realpath(__file__)) + '/' with open(path_of_test_folder + 'books.json') as json_file: book_data = json.load(json_file) with open(path_of_test_folder + 'tracks.json') as json_file: track_data = json.load(json_file) Book.insert_many(book_data).execute() for chunk in chunks(track_data, 25): Track.insert_many(chunk).execute() with open(path_of_test_folder + 'storages.json') as json_file: storage_data = json.load(json_file) Storage.insert_many(storage_data).execute() Settings.create(path="", last_played_book=Book.get()) StorageBlackList.create(path="/path/to/replace/test1.mp3") StorageBlackList.create(path="/path/to/not/replace/test2.mp3") print("Provide database...") yield test_db teardown_db(db_path, models, test_db)
def rebase_path(self, old_path: str, new_path: str): with self._db: for element in StorageBlackList.select(): if old_path in element.path: new_file_path = element.path.replace(old_path, new_path) StorageBlackList.update(path=new_file_path).where( StorageBlackList.id == element.id).execute()
def add_book(self, book: Book): book_tracks = [ TrackModel.get_by_id(chapter.id) for chapter in book.chapters ] data = list((t.file, ) for t in book_tracks) chunks = [data[x:x + 500] for x in range(0, len(data), 500)] for chunk in chunks: StorageBlackList.insert_many(chunk, fields=[StorageBlackList.path ]).execute()
def test_rebase_path(): from cozy.model.storage_block_list import StorageBlockList from cozy.db.storage_blacklist import StorageBlackList model = StorageBlockList() model.rebase_path("/path/to/replace", "/replaced/path") assert StorageBlackList.get_by_id(1).path == "/replaced/path/test1.mp3" assert StorageBlackList.get_by_id( 2).path == "/path/to/not/replace/test2.mp3"
def blacklist_book(book): """ Removes a book from the library and adds the path(s) to the track list. """ book_tracks = get_tracks(book) data = list((t.file, ) for t in book_tracks) chunks = [data[x:x + 500] for x in range(0, len(data), 500)] for chunk in chunks: StorageBlackList.insert_many(chunk, fields=[StorageBlackList.path]).execute() ids = list(t.id for t in book_tracks) Track.delete().where(Track.id << ids).execute() book.delete_instance()
def blacklist(self): with self._db: book_tracks = [ TrackModel.get_by_id(chapter.id) for chapter in self._chapters ] data = list((t.file, ) for t in book_tracks) chunks = [data[x:x + 500] for x in range(0, len(data), 500)] for chunk in chunks: StorageBlackList.insert_many(chunk, fields=[StorageBlackList.path ]).execute() ids = list(t.id for t in book_tracks) TrackModel.delete().where(TrackModel.id << ids).execute() self._db_object.delete_instance()
def remove(self): if self._settings.last_played_book and self._settings.last_played_book.id == self._db_object.id: self._settings.last_played_book = None book_tracks = [ TrackModel.get_by_id(chapter.id) for chapter in self.chapters ] data = list((t.file, ) for t in book_tracks) chunks = [data[x:x + 500] for x in range(0, len(data), 500)] for chunk in chunks: StorageBlackList.insert_many(chunk, fields=[StorageBlackList.path ]).execute() ids = list(t.id for t in book_tracks) TrackModel.delete().where(TrackModel.id << ids).execute() self._db_object.delete_instance(recursive=True)
def _init_blacklist(self): """ Init the Storage location list. """ for file in StorageBlackList.select(): self.blacklist_model.append([file.path, file.id]) self.__on_blacklist_selection_changed(None)
def __on_remove_blacklist_clicked(self, widget): """ Remove the selected storage from the db and ui. TODO: Does this trigger a rescan? """ model, pathlist = self.blacklist_tree_view.get_selection().get_selected_rows() if pathlist: ids = [] for path in reversed(pathlist): treeiter = model.get_iter(path) ids.append(self.blacklist_model.get_value(treeiter, 1)) self.blacklist_model.remove(treeiter) StorageBlackList.delete().where(StorageBlackList.id in ids).execute() self.__on_blacklist_selection_changed(self.blacklist_tree_view.get_selection())
def is_blacklisted(path): """ Tests whether a given path is blacklisted. """ if StorageBlackList.select().where(StorageBlackList.path == path).count() > 0: return True else: return False
def rebase_location(ui, oldPath, newPath): """ This gets called when a user changes the location of the audio book folder. Every file in the database updated with the new path. Note: This does not check for the existence of those files. """ trackCount = Track.select().count() currentTrackCount = 0 for track in Track.select(): newFilePath = track.file.replace(oldPath, newPath) Track.update(file=newFilePath).where(Track.id == track.id).execute() StorageBlackList.update(path=newFilePath).where( StorageBlackList.path == track.file).execute() Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.titlebar.update_progress_bar.set_fraction, currentTrackCount / trackCount) currentTrackCount = currentTrackCount + 1 Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT_IDLE, ui.switch_to_playing)
def peewee_database_storage(): from cozy.db.storage import Storage from cozy.db.settings import Settings from cozy.db.storage_blacklist import StorageBlackList db_path, models, test_db = prepare_db() path_of_test_folder = os.path.dirname(os.path.realpath(__file__)) + '/' with open(path_of_test_folder + 'storages.json') as json_file: storage_data = json.load(json_file) Storage.insert_many(storage_data).execute() Settings.create(path="", last_played_book=None) StorageBlackList.create(path="/path/to/replace/test1.mp3") StorageBlackList.create(path="/path/to/not/replace/test2.mp3") print("Provide database...") yield test_db teardown_db(db_path, models, test_db)