Skip to content

Commit

Permalink
Format project files with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbende committed Mar 26, 2024
1 parent ad26c04 commit 6415008
Show file tree
Hide file tree
Showing 55 changed files with 459 additions and 293 deletions.
5 changes: 1 addition & 4 deletions cozy/app_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ def open_library(self):
self.app_view_model.view = View.LIBRARY_FILTER

def _connect_search_button(self):
self.headerbar.search_button.connect(
"notify::active",
self.search_view.on_state_changed
)
self.headerbar.search_button.connect("notify::active", self.search_view.on_state_changed)

def _on_open_view(self, event, data):
if event == OpenView.AUTHOR:
Expand Down
6 changes: 5 additions & 1 deletion cozy/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ def handle_exception(self, _):
return

try:
reporter.exception("uncaught", exc_value, "\n".join(format_exception(exc_type, exc_value, exc_traceback)))
reporter.exception(
"uncaught",
exc_value,
"\n".join(format_exception(exc_type, exc_value, exc_traceback)),
)
finally:
sys.excepthook(exc_type, exc_value, exc_traceback)

Expand Down
4 changes: 3 additions & 1 deletion cozy/architecture/event_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def emit_event(self, event, message=None):
function(event, message)

def emit_event_main_thread(self, event: str, message=None):
GLib.MainContext.default().invoke_full(GLib.PRIORITY_DEFAULT_IDLE, self.emit_event, (event, message))
GLib.MainContext.default().invoke_full(
GLib.PRIORITY_DEFAULT_IDLE, self.emit_event, (event, message)
)

def add_listener(self, function: Callable[[str, object], None]):
self._listeners.append(function)
Expand Down
6 changes: 4 additions & 2 deletions cozy/architecture/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

log = logging.getLogger("timing")


def timing(f):
@functools.wraps(f)
def wrap(*args):
time1 = time.perf_counter()
ret = f(*args)
time2 = time.perf_counter()
log.info('%s function took %.3f ms', f.__name__, (time2-time1)*1000.0)
log.info('%s function took %.3f ms', f.__name__, (time2 - time1) * 1000.0)

return ret
return wrap

return wrap
2 changes: 1 addition & 1 deletion cozy/architecture/singleton.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Singleton(type):
_instances = {}

def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]

24 changes: 15 additions & 9 deletions cozy/control/artwork_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def delete_artwork_cache(self):
cache_dir = os.path.join(get_cache_dir(), "artwork")

import shutil

if os.path.exists(cache_dir):
shutil.rmtree(cache_dir)

Expand All @@ -56,7 +57,7 @@ def delete_artwork_cache(self):

def _on_importer_event(self, event, data):
if event == "scan" and data == ScanStatus.STARTED:
self.delete_artwork_cache()
self.delete_artwork_cache()

def _create_artwork_cache(self, book, pixbuf, size):
"""
Expand Down Expand Up @@ -97,7 +98,10 @@ def get_album_art_path(self, book, size):
try:
uuid = query.first().uuid
except Exception:
reporter.error("artwork_cache", "load_pixbuf_from_cache: query exists but query.first().uuid crashed.")
reporter.error(
"artwork_cache",
"load_pixbuf_from_cache: query exists but query.first().uuid crashed.",
)
return None
else:
return None
Expand Down Expand Up @@ -179,12 +183,10 @@ def _resize_pixbuf(self, pixbuf, size):
if size > 0:
if pixbuf.get_height() > pixbuf.get_width():
width = int(pixbuf.get_width() / (pixbuf.get_height() / size))
resized_pixbuf = pixbuf.scale_simple(
width, size, GdkPixbuf.InterpType.BILINEAR)
resized_pixbuf = pixbuf.scale_simple(width, size, GdkPixbuf.InterpType.BILINEAR)
else:
height = int(pixbuf.get_height() / (pixbuf.get_width() / size))
resized_pixbuf = pixbuf.scale_simple(
size, height, GdkPixbuf.InterpType.BILINEAR)
resized_pixbuf = pixbuf.scale_simple(size, height, GdkPixbuf.InterpType.BILINEAR)

return resized_pixbuf

Expand All @@ -200,8 +202,13 @@ def _load_pixbuf_from_file(self, book):
try:
directory = os.path.dirname(os.path.normpath(book.chapters[0].file))

cover_files = [f for f in os.listdir(directory)
if f.lower().endswith('.png') or f.lower().endswith(".jpg") or f.lower().endswith(".gif")]
cover_files = [
f
for f in os.listdir(directory)
if f.lower().endswith('.png')
or f.lower().endswith(".jpg")
or f.lower().endswith(".gif")
]
except Exception as e:
log.warning("Could not open audiobook directory and look for cover files: %s", e)
for elem in (x for x in cover_files if os.path.splitext(x.lower())[0] == "cover"):
Expand All @@ -227,4 +234,3 @@ def _load_pixbuf_from_file(self, book):
def _on_app_setting_changed(self, event: str, data):
if event == "prefer-external-cover":
self.delete_artwork_cache()

40 changes: 34 additions & 6 deletions cozy/control/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,43 @@ def init_db():
_db.start()
else:
_db.create_tables(
[Track, Book, Settings, ArtworkCache, Storage, StorageBlackList, OfflineCache, TrackToFile, File])
[
Track,
Book,
Settings,
ArtworkCache,
Storage,
StorageBlackList,
OfflineCache,
TrackToFile,
File,
]
)
_db.stop()
_db.start()

while not _db.table_exists("settings"):
time.sleep(0.01)

_db.bind([Book, Track, Settings, ArtworkCache, StorageBlackList, OfflineCache, Storage, TrackToFile, File],
bind_refs=False,
bind_backrefs=False)
_db.bind(
[
Book,
Track,
Settings,
ArtworkCache,
StorageBlackList,
OfflineCache,
Storage,
TrackToFile,
File,
],
bind_refs=False,
bind_backrefs=False,
)

_db.register_collation(collate_natural)

if (Settings.select().count() == 0):
if Settings.select().count() == 0:
Settings.create(path="", last_played_book=None)

# TODO: Properly handle errors within the database
Expand Down Expand Up @@ -77,7 +100,12 @@ def get_tracks(book):
:param book: the book object
:return: all tracks belonging to the book object
"""
return Track.select().join(Book).where(Book.id == book.id).order_by(Track.disk, Track.number, Track.name)
return (
Track.select()
.join(Book)
.where(Book.id == book.id)
.order_by(Track.disk, Track.number, Track.name)
)


def get_track_for_playback(book):
Expand Down
47 changes: 15 additions & 32 deletions cozy/control/db_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def __update_db_2(db):

playback_speed = FloatField(default=1.0)

migrate(
migrator.add_column('book', 'playback_speed', playback_speed),
)
migrate(migrator.add_column('book', 'playback_speed', playback_speed))

Settings.update(version=2).execute()

Expand All @@ -59,9 +57,7 @@ def __update_db_4(db):

last_played = IntegerField(default=0)

migrate(
migrator.add_column('book', 'last_played', last_played),
)
migrate(migrator.add_column('book', 'last_played', last_played))

Settings.update(version=4).execute()

Expand All @@ -84,17 +80,19 @@ def __update_db_6(db):
migrate(
migrator.add_column('storage', 'external', external),
migrator.add_column('book', 'offline', offline),
migrator.add_column('book', 'downloaded', downloaded)
migrator.add_column('book', 'downloaded', downloaded),
)

Settings.update(version=6).execute()

import shutil

shutil.rmtree(get_cache_dir())


def __update_db_7(db):
from cozy.control.artwork_cache import ArtworkCache

artwork_cache = ArtworkCache()
artwork_cache.delete_artwork_cache()
Settings.update(version=7).execute()
Expand All @@ -105,9 +103,7 @@ def __update_db_8(db):

migrator: SqliteMigrator = SqliteMigrator(db)

migrate(
migrator.drop_column("track", "crc32")
)
migrate(migrator.drop_column("track", "crc32"))

Settings.update(version=8).execute()

Expand Down Expand Up @@ -157,15 +153,11 @@ def _update_db_9(db):

if "cached_file" not in models["offlinecache"]._meta.sorted_field_names:
log.info("Rename in OfflineCache: file to cached_file...")
migrate(
migrator.rename_column("offlinecache", "file", "cached_file"),
)
migrate(migrator.rename_column("offlinecache", "file", "cached_file"))

if "original_file" not in models["offlinecache"]._meta.sorted_field_names:
log.info("Add in OfflineCache: original_file_id...")
migrate(
migrator.add_column("offlinecache", "original_file_id", field)
)
migrate(migrator.add_column("offlinecache", "original_file_id", field))

db.stop()
db.start()
Expand All @@ -184,25 +176,17 @@ def _update_db_9(db):

if "file" in models["track"]._meta.sorted_field_names:
log.info("Drop in Track: file...")
migrate(
migrator.drop_column("track", "file")
)
migrate(migrator.drop_column("track", "file"))

if "modified" in models["track"]._meta.sorted_field_names:
log.info("Drop in Track: modified...")
migrate(
migrator.drop_column("track", "modified")
)
migrate(migrator.drop_column("track", "modified"))

if "track_id" in models["offlinecache"]._meta.sorted_field_names:
log.info("Drop in OfflineCache: track_id...")
migrate(
migrator.drop_column("offlinecache", "track_id")
)
migrate(migrator.drop_column("offlinecache", "track_id"))

migrate(
migrator.add_not_null("offlinecache", "original_file_id")
)
migrate(migrator.add_not_null("offlinecache", "original_file_id"))

db.stop()
db.start()
Expand All @@ -224,9 +208,7 @@ def _update_db_10(db):

if "track" in models["offlinecache"]._meta.sorted_field_names:
log.info("Drop in OfflineCache: track_id...")
migrate(
migrator.drop_column("offlinecache", "track_id")
)
migrate(migrator.drop_column("offlinecache", "track_id"))

db.stop()
db.start()
Expand Down Expand Up @@ -282,6 +264,7 @@ def update_db():
_restore_db(backup_dir_name)

from cozy.ui.db_migration_failed_view import DBMigrationFailedView

DBMigrationFailedView().present()
exit(1)

Expand All @@ -296,6 +279,7 @@ def update_db():
_restore_db(backup_dir_name)

from cozy.ui.db_migration_failed_view import DBMigrationFailedView

DBMigrationFailedView().present()
exit(1)

Expand Down Expand Up @@ -351,4 +335,3 @@ def _restore_db(backup_dir_name: str):
if os.path.exists(wal_path_backup):
log.info("Copying wal file")
shutil.copyfile(wal_path_backup, wal_path)

Loading

0 comments on commit 6415008

Please sign in to comment.