def load_file(track): """ Loads a given track into the player. :param track: track to be loaded """ global __current_track global __player if get_gst_player_state() == Gst.State.PLAYING: save_current_track_position() save_current_book_position(__current_track) __current_track = track emit_event("stop") __player.set_state(Gst.State.NULL) init() if cozy.control.filesystem_monitor.FilesystemMonitor().is_track_online( track): path = track.file else: path = OfflineCache().get_cached_path(track) if not path: path = track.file __player.set_property("uri", "file://" + path) __player.set_state(Gst.State.PAUSED) save_current_book_position(__current_track) Settings.update(last_played_book=__current_track.book).execute() Book.update(last_played=int(time.time())).where( Book.id == __current_track.book.id).execute() emit_event("track-changed", track)
def __update_db_3(db): current_path = Settings.get().path db.create_tables([Storage]) Storage.create(path=current_path, default=True) Settings.update(path="NOT_USED").execute() Settings.update(version=3).execute()
def next_track(): """ Play the next track of the current book. Stops playback if there isn't any. """ global __current_track global __play_next album_tracks = get_tracks(get_current_track().book) current = get_current_track() index = list(album_tracks).index(current) next_track = None if index + 1 < len(album_tracks): next_track = album_tracks[index + 1] play_pause(None) save_current_track_position(0) if next_track: save_current_book_position(next_track) save_current_track_position(0, next_track) if __play_next: play_pause(next_track) else: load_file(next_track) __play_next = True else: stop() save_current_book_position(current, -1) unload() Settings.update(last_played_book=None).execute() emit_event("stop")
def test_setting_last_played_book_to_none_updates_in_settings_object_and_database(peewee_database): from cozy.model.settings import Settings from cozy.db.settings import Settings as SettingsModel settings = Settings() settings.last_played_book = None assert settings.last_played_book == None assert SettingsModel.get().last_played_book == None
def __update_db_2(db): migrator = SqliteMigrator(db) playback_speed = FloatField(default=1.0) migrate( migrator.add_column('book', 'playback_speed', playback_speed), ) Settings.update(version=2).execute()
def __update_db_4(db): migrator = SqliteMigrator(db) last_played = IntegerField(default=0) migrate( migrator.add_column('book', 'last_played', last_played), ) Settings.update(version=4).execute()
def __update_db_8(db): db.execute_sql('UPDATE track SET modified=0 WHERE crc32=1') migrator: SqliteMigrator = SqliteMigrator(db) migrate( migrator.drop_column("track", "crc32") ) Settings.update(version=8).execute()
def clean_books(): """ Remove all books that have no tracks """ for book in Book.select(): if not get_track_for_playback(book): Book.update(position=0).where(Book.id == book.id).execute() if Track.select().where(Track.book == book).count() < 1: if Settings.get().last_played_book.id == book.id: Settings.update(last_played_book=None).execute() book.delete_instance()
def test_deleted_book_removed_from_last_played_book_if_necessary( peewee_database): from cozy.model.book import Book from cozy.model.settings import Settings settings = Settings() inject.clear_and_configure(lambda binder: binder.bind( SqliteDatabase, peewee_database) and binder.bind(Settings, settings)) book = Book(peewee_database, 1) settings.last_played_book = book.db_object book._on_chapter_event("chapter-deleted", book.chapters[0]) assert settings.last_played_book == None
def load_last_book(): """ Load the last played book into the player. """ global __current_track global __player last_book = Settings.get().last_played_book if last_book and last_book.position != 0: query = Track.select().where(Track.id == last_book.position) if query.exists(): last_track = query.get() if last_track: __player.set_state(Gst.State.NULL) if cozy.control.filesystem_monitor.FilesystemMonitor( ).is_track_online(last_track): path = last_track.file else: path = OfflineCache().get_cached_path(last_track) if not path: return __player.set_property("uri", "file://" + path) __player.set_state(Gst.State.PAUSED) __current_track = last_track Book.update(last_played=int(time.time())).where( Book.id == last_book.id).execute() emit_event("track-changed", last_track)
def test_last_played_book_returns_correct_value(peewee_database): from cozy.model.settings import Settings from cozy.db.book import Book settings = Settings() assert settings.last_played_book == Book.get()
def configure_inject(self, binder): binder.bind_to_provider(SqliteDatabase, get_db) binder.bind("MainWindow", self.main_window) binder.bind_to_constructor( Gio.Settings, lambda: Gio.Settings("com.github.geigi.cozy")) binder.bind_to_constructor(ApplicationSettings, lambda: ApplicationSettings()) binder.bind_to_constructor(Settings, lambda: Settings()) binder.bind_to_constructor("FilesystemMonitor", lambda: FilesystemMonitor()) binder.bind_to_constructor(OfflineCache, lambda: OfflineCache()) binder.bind_to_constructor(Player, lambda: Player()) binder.bind_to_constructor(Library, lambda: Library()) binder.bind_to_constructor(LibraryViewModel, lambda: LibraryViewModel()) binder.bind_to_constructor(SearchViewModel, lambda: SearchViewModel()) binder.bind_to_constructor(UISettings, lambda: UISettings()) binder.bind_to_constructor(StorageBlockList, lambda: StorageBlockList()) binder.bind_to_constructor(Files, lambda: Files()) binder.bind_to_constructor(BookDetailViewModel, lambda: BookDetailViewModel()) binder.bind_to_constructor(PlaybackControlViewModel, lambda: PlaybackControlViewModel()) binder.bind_to_constructor(HeaderbarViewModel, lambda: HeaderbarViewModel()) binder.bind_to_constructor(PlaybackSpeedViewModel, lambda: PlaybackSpeedViewModel()) binder.bind_to_constructor(SleepTimerViewModel, lambda: SleepTimerViewModel())
def test_external_storage_locations_contain_only_external_storages(peewee_database): from cozy.model.settings import Settings from cozy.db.storage import Storage settings = Settings() storage_locations = Storage.select().where(Storage.external == True) assert len(settings.external_storage_locations) == len(storage_locations) assert all([storage.external for storage in settings.external_storage_locations])
def setup_inject(peewee_database): inject.clear() inject.configure(lambda binder: (binder.bind(SqliteDatabase, peewee_database), binder.bind_to_constructor(Settings, lambda: Settings()). bind_to_constructor(ApplicationSettings, lambda: ApplicationSettingsMock()))) yield inject.clear()
def __update_db_6(db): migrator = SqliteMigrator(db) db.create_tables([OfflineCache]) external = BooleanField(default=False) offline = BooleanField(default=False) downloaded = BooleanField(default=False) migrate( migrator.add_column('storage', 'external', external), migrator.add_column('book', 'offline', offline), migrator.add_column('book', 'downloaded', downloaded) ) Settings.update(version=6).execute() import shutil shutil.rmtree(get_cache_dir())
def init_db(): tmp_db = None _connect_db(_db) if Settings.table_exists(): update_db() else: tmp_db = SqliteDatabase(os.path.join(get_data_dir(), "cozy.db")) if PeeweeVersion[0] == '2': tmp_db.create_tables([ Track, Book, Settings, ArtworkCache, Storage, StorageBlackList, OfflineCache ], True) else: with tmp_db.connection_context(): tmp_db.create_tables([ Track, Book, Settings, ArtworkCache, Storage, StorageBlackList, OfflineCache ]) # this is necessary to ensure that the tables have indeed been created if tmp_db: if PeeweeVersion[0] == '2': while not Settings.table_exists(): time.sleep(0.01) else: while not tmp_db.table_exists("settings"): time.sleep(0.01) _connect_db(_db) if PeeweeVersion[0] == '3': _db.bind([ Book, Track, Settings, ArtworkCache, StorageBlackList, OfflineCache, Storage ], bind_refs=False, bind_backrefs=False) if (Settings.select().count() == 0): Settings.create(path="", last_played_book=None)
def test_fetching_non_existent_last_played_book_returns_none(peewee_database): from cozy.model.settings import Settings from cozy.db.settings import Settings as SettingsModel db_object = SettingsModel.get() db_object.last_played_book = 437878782 db_object.save(only=db_object.dirty_fields) settings = Settings() assert settings.last_played_book is None
def setup_inject(peewee_database): inject.clear_and_configure( lambda binder: binder.bind(SqliteDatabase, peewee_database). bind_to_constructor("FilesystemMonitor", MagicMock( )).bind_to_constructor(GstPlayer, MagicMock()).bind_to_constructor( ApplicationSettings, MagicMock()).bind_to_constructor( Library, lambda: Library()).bind_to_constructor( Settings, lambda: Settings())) yield inject.clear()
def test_ensure_default_storage_present_does_nothing_if_default_is_present(peewee_database): from cozy.model.settings import Settings from cozy.db.storage import Storage settings = Settings() settings._load_all_storage_locations() settings._ensure_default_storage_present() assert not Storage.get(1).default assert Storage.get(2).default
def test_storage_locations_contains_every_storage_location_from_db(peewee_database): from cozy.model.settings import Settings from cozy.db.storage import Storage settings = Settings() storage_locations = Storage.select() assert len(settings.storage_locations) == len(storage_locations) assert [storage.path for storage in settings.storage_locations] == [storage.path for storage in storage_locations] assert [storage.location_type for storage in settings.storage_locations] == [storage.location_type for storage in storage_locations] assert [storage.default for storage in settings.storage_locations] == [storage.default for storage in storage_locations] assert [storage.external for storage in settings.storage_locations] == [storage.external for storage in storage_locations]
def test_ensure_default_storage_present_adds_default_if_not_present(peewee_database): from cozy.model.settings import Settings from cozy.db.storage import Storage Storage.update(default=False).where(Storage.id == 2).execute() settings = Settings() settings._load_all_storage_locations() settings._ensure_default_storage_present() assert Storage.get(1).default assert not Storage.get(2).default
def configure_inject(binder): binder.bind_to_provider(SqliteDatabase, get_db) binder.bind_to_constructor( Gio.Settings, lambda: Gio.Settings("com.github.geigi.cozy")) binder.bind_to_constructor(ApplicationSettings, lambda: ApplicationSettings()) binder.bind_to_constructor(Settings, lambda: Settings()) binder.bind_to_constructor("FilesystemMonitor", lambda: FilesystemMonitor()) binder.bind_to_constructor(Library, lambda: Library()) binder.bind_to_constructor(LibraryViewModel, lambda: LibraryViewModel()) binder.bind_to_constructor(SearchViewModel, lambda: SearchViewModel()) binder.bind_to_constructor(UISettings, lambda: UISettings()) binder.bind_to_constructor(StorageBlockList, lambda: StorageBlockList()) binder.bind_to_constructor(Files, lambda: Files())
def load_last_book(self): if Settings.get().last_played_book: self.update_track_ui() self.update_ui_time(self.progress_scale) cur_m, cur_s = player.get_current_duration_ui() self.__set_progress_scale_value(cur_m * 60 + cur_s) pos = int(player.get_current_track().position) if tools.get_glib_settings().get_boolean("replay"): log.info("Replaying the previous 30 seconds.") amount = 30 * 1000000000 if (pos < amount): pos = 0 else: pos = pos - amount self.__set_progress_scale_value( int(pos / 1000000000 / self.ui.speed.get_speed()))
def update_db(): db = get_sqlite_database() # First test for version 1 try: next(c for c in db.get_columns("settings") if c.name == "version") except Exception as e: if len(db.get_tables()) == 0: data_dir = get_data_dir() if os.path.exists(os.path.join(data_dir, "cozy.db")): os.remove(os.path.join(data_dir, "cozy.db")) os.remove(os.path.join(data_dir, "cozy.db-shm")) os.remove(os.path.join(data_dir, "cozy.db-wal")) __update_db_1(db) version = Settings.get().version # then for version 2 and so on if version < 2: __update_db_2(db) if version < 3: __update_db_3(db) if version < 4: __update_db_4(db) if version < 5: __update_db_5(db) if version < 6: __update_db_6(db) if version < 7: __update_db_7(db) if version < 8: __update_db_8(db)
def __update_db_7(db): import cozy.control.artwork_cache as artwork_cache artwork_cache.delete_artwork_cache() Settings.update(version=7).execute()
def __update_db_5(db): db.create_tables([StorageBlackList]) Settings.update(version=5).execute()