Adding a bunch of flake8 extensions and working through the errors

This commit is contained in:
Sumner Evans
2020-02-22 17:03:37 -07:00
parent 91c2f408b3
commit 2a0c480d4b
30 changed files with 1979 additions and 618 deletions

View File

@@ -1,15 +1,14 @@
from datetime import datetime
from typing import Set
from typing import Any, Callable, Set
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gio, Gtk, GObject, Gdk, GLib, Pango
from gi.repository import Gdk, Gio, GLib, GObject, Gtk, Pango
from . import albums, artists, browse, playlists, player_controls
from sublime.state_manager import ApplicationState
from sublime.cache_manager import CacheManager, SearchResult
from sublime.server.api_objects import Child
from sublime.ui import util
from sublime.state_manager import ApplicationState
from sublime.ui import (
albums, artists, browse, player_controls, playlists, util)
from sublime.ui.common import SpinnerImage
@@ -43,7 +42,7 @@ class MainWindow(Gtk.ApplicationWindow):
self.set_default_size(1150, 768)
# Create the stack
self.stack = self.create_stack(
self.stack = self._create_stack(
Albums=albums.AlbumsPanel(),
Artists=artists.ArtistsPanel(),
Browse=browse.BrowsePanel(),
@@ -52,7 +51,7 @@ class MainWindow(Gtk.ApplicationWindow):
self.stack.set_transition_type(
Gtk.StackTransitionType.SLIDE_LEFT_RIGHT)
self.titlebar = self.create_headerbar(self.stack)
self.titlebar = self._create_headerbar(self.stack)
self.set_titlebar(self.titlebar)
self.player_controls = player_controls.PlayerControls()
@@ -70,9 +69,9 @@ class MainWindow(Gtk.ApplicationWindow):
flowbox.pack_start(self.player_controls, False, True, 0)
self.add(flowbox)
self.connect('button-release-event', self.on_button_release)
self.connect('button-release-event', self._on_button_release)
def update(self, state: ApplicationState, force=False):
def update(self, state: ApplicationState, force: bool = False):
# Update the Connected to label on the popup menu.
if state.config.current_server >= 0:
server_name = state.config.servers[
@@ -91,7 +90,7 @@ class MainWindow(Gtk.ApplicationWindow):
self.player_controls.update(state)
def create_stack(self, **kwargs):
def _create_stack(self, **kwargs: Gtk.Widget) -> Gtk.Stack:
stack = Gtk.Stack()
for name, child in kwargs.items():
child.connect(
@@ -105,7 +104,7 @@ class MainWindow(Gtk.ApplicationWindow):
stack.add_titled(child, name.lower(), name)
return stack
def create_headerbar(self, stack):
def _create_headerbar(self, stack: Gtk.Stack) -> Gtk.HeaderBar:
"""
Configure the header bar for the window.
"""
@@ -116,18 +115,19 @@ class MainWindow(Gtk.ApplicationWindow):
# Search
self.search_entry = Gtk.SearchEntry(
placeholder_text='Search everything...')
self.search_entry.connect('focus-in-event', self.on_search_entry_focus)
self.search_entry.connect(
'button-press-event', self.on_search_entry_button_press)
'focus-in-event', self._on_search_entry_focus)
self.search_entry.connect(
'focus-out-event', self.on_search_entry_loose_focus)
self.search_entry.connect('changed', self.on_search_entry_changed)
'button-press-event', self._on_search_entry_button_press)
self.search_entry.connect(
'stop-search', self.on_search_entry_stop_search)
'focus-out-event', self._on_search_entry_loose_focus)
self.search_entry.connect('changed', self._on_search_entry_changed)
self.search_entry.connect(
'stop-search', self._on_search_entry_stop_search)
header.pack_start(self.search_entry)
# Search popup
self.create_search_popup()
self._create_search_popup()
# Stack switcher
switcher = Gtk.StackSwitcher(stack=stack)
@@ -136,8 +136,8 @@ class MainWindow(Gtk.ApplicationWindow):
# Menu button
menu_button = Gtk.MenuButton()
menu_button.set_use_popover(True)
menu_button.set_popover(self.create_menu())
menu_button.connect('clicked', self.on_menu_clicked)
menu_button.set_popover(self._create_menu())
menu_button.connect('clicked', self._on_menu_clicked)
self.menu.set_relative_to(menu_button)
icon = Gio.ThemedIcon(name='open-menu-symbolic')
@@ -148,7 +148,7 @@ class MainWindow(Gtk.ApplicationWindow):
return header
def create_label(self, text, *args, **kwargs):
def _create_label(self, text: str, *args, **kwargs) -> Gtk.Label:
label = Gtk.Label(
use_markup=True,
halign=Gtk.Align.START,
@@ -160,10 +160,10 @@ class MainWindow(Gtk.ApplicationWindow):
label.get_style_context().add_class('search-result-row')
return label
def create_menu(self):
def _create_menu(self) -> Gtk.PopoverMenu:
self.menu = Gtk.PopoverMenu()
self.connected_to_label = self.create_label(
self.connected_to_label = self._create_label(
'', name='connected-to-label')
self.connected_to_label.set_markup(
f'<span style="italic">Not Connected to a Server</span>')
@@ -187,7 +187,7 @@ class MainWindow(Gtk.ApplicationWindow):
return self.menu
def create_search_popup(self):
def _create_search_popup(self) -> Gtk.PopoverMenu:
self.search_popup = Gtk.PopoverMenu(modal=False)
results_scrollbox = Gtk.ScrolledWindow(
@@ -195,8 +195,8 @@ class MainWindow(Gtk.ApplicationWindow):
min_content_height=750,
)
def make_search_result_header(text):
label = self.create_label(text)
def make_search_result_header(text: str) -> Gtk.Label:
label = self._create_label(text)
label.get_style_context().add_class('search-result-header')
return label
@@ -238,22 +238,22 @@ class MainWindow(Gtk.ApplicationWindow):
# Event Listeners
# =========================================================================
def on_button_release(self, win, event):
if not self.event_in_widgets(
def _on_button_release(self, win: Any, event: Gdk.EventButton) -> bool:
if not self._event_in_widgets(
event,
self.search_entry,
self.search_popup,
):
self.hide_search()
self._hide_search()
if not self.event_in_widgets(
if not self._event_in_widgets(
event,
self.player_controls.device_button,
self.player_controls.device_popover,
):
self.player_controls.device_popover.popdown()
if not self.event_in_widgets(
if not self._event_in_widgets(
event,
self.player_controls.play_queue_button,
self.player_controls.play_queue_popover,
@@ -262,25 +262,25 @@ class MainWindow(Gtk.ApplicationWindow):
return False
def on_menu_clicked(self, button):
def _on_menu_clicked(self, *args):
self.menu.popup()
self.menu.show_all()
def on_search_entry_focus(self, entry, event):
self.show_search()
def _on_search_entry_focus(self, *args):
self._show_search()
def on_search_entry_button_press(self, *args):
self.show_search()
def _on_search_entry_button_press(self, *args):
self._show_search()
def on_search_entry_loose_focus(self, entry, event):
self.hide_search()
def _on_search_entry_loose_focus(self, *args):
self._hide_search()
search_idx = 0
latest_returned_search_idx = 0
last_search_change_time = datetime.now()
searches: Set[SearchResult] = set()
searches: Set[CacheManager.Result] = set()
def on_search_entry_changed(self, entry):
def _on_search_entry_changed(self, entry: Gtk.Entry):
now = datetime.now()
if (now - self.last_search_change_time).seconds < 0.5:
while len(self.searches) > 0:
@@ -293,8 +293,11 @@ class MainWindow(Gtk.ApplicationWindow):
self.search_popup.show_all()
self.search_popup.popup()
def create_search_callback(idx):
def search_result_calback(result, is_last_in_batch):
def create_search_callback(idx: int) -> Callable[..., Any]:
def search_result_calback(
result: SearchResult,
is_last_in_batch: bool,
):
# Ignore slow returned searches.
if idx < self.latest_returned_search_idx:
return
@@ -302,10 +305,10 @@ class MainWindow(Gtk.ApplicationWindow):
# If all results are back, the stop the loading indicator.
if is_last_in_batch:
if idx == self.search_idx - 1:
self.set_search_loading(False)
self._set_search_loading(False)
self.latest_returned_search_idx = idx
self.update_search_results(result)
self._update_search_results(result)
return lambda *a: GLib.idle_add(search_result_calback, *a)
@@ -313,27 +316,27 @@ class MainWindow(Gtk.ApplicationWindow):
CacheManager.search(
entry.get_text(),
search_callback=create_search_callback(self.search_idx),
before_download=lambda: self.set_search_loading(True),
before_download=lambda: self._set_search_loading(True),
))
self.search_idx += 1
def on_search_entry_stop_search(self, entry):
def _on_search_entry_stop_search(self, entry: Any):
self.search_popup.popdown()
# Helper Functions
# =========================================================================
def show_search(self):
def _show_search(self):
self.search_entry.set_size_request(300, -1)
self.search_popup.show_all()
self.search_results_loading.hide()
self.search_popup.popup()
def hide_search(self):
def _hide_search(self):
self.search_popup.popdown()
self.search_entry.set_size_request(-1, -1)
def set_search_loading(self, loading_state):
def _set_search_loading(self, loading_state: bool):
if loading_state:
self.search_results_loading.start()
self.search_results_loading.show_all()
@@ -341,24 +344,24 @@ class MainWindow(Gtk.ApplicationWindow):
self.search_results_loading.stop()
self.search_results_loading.hide()
def remove_all_from_widget(self, widget):
def _remove_all_from_widget(self, widget: Gtk.Widget):
for c in widget.get_children():
widget.remove(c)
def create_search_result_row(
self,
text,
action_name,
value,
artwork_future,
):
def on_search_row_button_press(btn, event):
def _create_search_result_row(
self,
text: str,
action_name: str,
value: Any,
artwork_future: CacheManager.Result,
) -> Gtk.Button:
def on_search_row_button_press(*args):
if action_name == 'song':
goto_action_name, goto_id = 'album', value.albumId
else:
goto_action_name, goto_id = action_name, value.id
self.emit('go-to', goto_action_name, goto_id)
self.hide_search()
self._hide_search()
row = Gtk.Button(relief=Gtk.ReliefStyle.NONE)
row.connect('button-press-event', on_search_row_button_press)
@@ -366,10 +369,10 @@ class MainWindow(Gtk.ApplicationWindow):
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
image = SpinnerImage(image_name='search-artwork', image_size=30)
box.add(image)
box.add(self.create_label(text))
box.add(self._create_label(text))
row.add(box)
def image_callback(f):
def image_callback(f: CacheManager.Result):
image.set_loading(False)
image.set_from_file(f.result())
@@ -378,10 +381,10 @@ class MainWindow(Gtk.ApplicationWindow):
return row
def update_search_results(self, search_results):
def _update_search_results(self, search_results: SearchResult):
# Songs
if search_results.song is not None:
self.remove_all_from_widget(self.song_results)
self._remove_all_from_widget(self.song_results)
for song in search_results.song or []:
label_text = util.dot_join(
f'<b>{util.esc(song.title)}</b>',
@@ -390,54 +393,53 @@ class MainWindow(Gtk.ApplicationWindow):
cover_art_future = CacheManager.get_cover_art_filename(
song.coverArt, size=50)
self.song_results.add(
self.create_search_result_row(
self._create_search_result_row(
label_text, 'song', song, cover_art_future))
self.song_results.show_all()
# Albums
if search_results.album is not None:
self.remove_all_from_widget(self.album_results)
self._remove_all_from_widget(self.album_results)
for album in search_results.album or []:
name = album.title if type(album) == Child else album.name
label_text = util.dot_join(
f'<b>{util.esc(name)}</b>',
f'<b>{util.esc(album.name)}</b>',
util.esc(album.artist),
)
cover_art_future = CacheManager.get_cover_art_filename(
album.coverArt, size=50)
self.album_results.add(
self.create_search_result_row(
self._create_search_result_row(
label_text, 'album', album, cover_art_future))
self.album_results.show_all()
# Artists
if search_results.artist is not None:
self.remove_all_from_widget(self.artist_results)
self._remove_all_from_widget(self.artist_results)
for artist in search_results.artist or []:
label_text = util.esc(artist.name)
cover_art_future = CacheManager.get_artist_artwork(artist)
self.artist_results.add(
self.create_search_result_row(
self._create_search_result_row(
label_text, 'artist', artist, cover_art_future))
self.artist_results.show_all()
# Playlists
if search_results.playlist is not None:
self.remove_all_from_widget(self.playlist_results)
self._remove_all_from_widget(self.playlist_results)
for playlist in search_results.playlist or []:
label_text = util.esc(playlist.name)
cover_art_future = CacheManager.get_cover_art_filename(
playlist.coverArt)
self.playlist_results.add(
self.create_search_result_row(
self._create_search_result_row(
label_text, 'playlist', playlist, cover_art_future))
self.playlist_results.show_all()
def event_in_widgets(self, event, *widgets):
def _event_in_widgets(self, event: Gdk.EventButton, *widgets) -> bool:
for widget in widgets:
if not widget.is_visible():
continue