Skip to content

Commit

Permalink
Merge pull request #4 from kokarare1212/deepsource-transform-696db435
Browse files Browse the repository at this point in the history
Format code with yapf
  • Loading branch information
kokarare1212 authored Apr 9, 2021
2 parents 3df41f0 + 0d0c4e8 commit a47a5db
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 69 deletions.
29 changes: 10 additions & 19 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,51 @@
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))

# -- Project information -----------------------------------------------------

project = 'librespot-python'
copyright = '2021, kokarare1212'
author = 'kokarare1212'

project = "librespot-python"
copyright = "2021, kokarare1212"
author = "kokarare1212"

# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'recommonmark'
]
extensions = ["recommonmark"]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]

# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
html_theme = "sphinx_rtd_theme"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

html_static_path = ["_static"]

# -- Markdown ----------------------------------------------------------------

source_suffix = ['.rst', '.md']
source_suffix = [".rst", ".md"]

source_parsers = {
'.md': 'recommonmark.parser.CommonMarkParser',
".md": "recommonmark.parser.CommonMarkParser",
}

9 changes: 5 additions & 4 deletions librespot/metadata/AlbumId.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

from librespot.common import Base62, Utils
from librespot.metadata import SpotifyId
import re

from librespot.common import Base62
from librespot.common import Utils
from librespot.metadata import SpotifyId


class AlbumId(SpotifyId.SpotifyId):
_PATTERN = re.compile(r"spotify:album:(.{22})")
Expand All @@ -25,8 +27,7 @@ def from_uri(uri: str) -> AlbumId:

@staticmethod
def from_base62(base62: str) -> AlbumId:
return AlbumId(
Utils.bytes_to_hex(AlbumId._BASE62.decode(base62, 16)))
return AlbumId(Utils.bytes_to_hex(AlbumId._BASE62.decode(base62, 16)))

@staticmethod
def from_hex(hex_str: str) -> AlbumId:
Expand Down
14 changes: 8 additions & 6 deletions librespot/metadata/ArtistId.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import annotations
from librespot.common import Base62, Utils
from librespot.metadata import SpotifyId

import re

from librespot.common import Base62
from librespot.common import Utils
from librespot.metadata import SpotifyId


class ArtistId(SpotifyId.SpotifyId):
_PATTERN = re.compile("spotify:artist:(.{22})")
Expand All @@ -18,15 +21,14 @@ def from_uri(uri: str) -> ArtistId:
if matcher is not None:
artist_id = matcher.group(1)
return ArtistId(
Utils.bytes_to_hex(ArtistId._BASE62.decode(
artist_id, 16)))
Utils.bytes_to_hex(ArtistId._BASE62.decode(artist_id, 16)))
else:
raise TypeError("Not a Spotify artist ID: {}".format(uri))

@staticmethod
def from_base62(base62: str) -> ArtistId:
return ArtistId(
Utils.bytes_to_hex(ArtistId._BASE62.decode(base62, 16)))
return ArtistId(Utils.bytes_to_hex(ArtistId._BASE62.decode(base62,
16)))

@staticmethod
def from_hex(hex_str: str) -> ArtistId:
Expand Down
10 changes: 6 additions & 4 deletions librespot/metadata/ShowId.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import annotations
from librespot.common import Base62, Utils
from librespot.metadata import SpotifyId

import re

from librespot.common import Base62
from librespot.common import Utils
from librespot.metadata import SpotifyId


class ShowId(SpotifyId.SpotifyId):
_PATTERN = re.compile("spotify:show:(.{22})")
Expand All @@ -24,8 +27,7 @@ def from_uri(uri: str) -> ShowId:

@staticmethod
def from_base62(base62: str) -> ShowId:
return ShowId(
Utils.bytes_to_hex(ShowId._BASE62.decode(base62, 16)))
return ShowId(Utils.bytes_to_hex(ShowId._BASE62.decode(base62, 16)))

@staticmethod
def from_hex(hex_str: str) -> ShowId:
Expand Down
11 changes: 6 additions & 5 deletions librespot/metadata/TrackId.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import re

from librespot.common import Utils
from librespot.metadata import SpotifyId
from librespot.metadata.PlayableId import PlayableId
import re


class TrackId(PlayableId, SpotifyId):
Expand All @@ -18,15 +20,14 @@ def from_uri(uri: str) -> TrackId:
if search is not None:
track_id = search.group(1)
return TrackId(
Utils.bytes_to_hex(PlayableId.BASE62.decode(
track_id, 16)))
Utils.bytes_to_hex(PlayableId.BASE62.decode(track_id, 16)))
else:
raise RuntimeError("Not a Spotify track ID: {}".format(uri))

@staticmethod
def from_base62(base62: str) -> TrackId:
return TrackId(
Utils.bytes_to_hex(PlayableId.BASE62.decode(base62, 16)))
return TrackId(Utils.bytes_to_hex(PlayableId.BASE62.decode(base62,
16)))

@staticmethod
def from_hex(hex_str: str) -> TrackId:
Expand Down
23 changes: 16 additions & 7 deletions librespot/player/Player.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from __future__ import annotations

import logging
import sched
import time

from librespot.core.Session import Session
from librespot.player import PlayerConfiguration, StateWrapper
from librespot.player import PlayerConfiguration
from librespot.player import StateWrapper
from librespot.player.metrics import PlaybackMetrics
from librespot.player.mixing import AudioSink
from librespot.player.playback.PlayerSession import PlayerSession
from librespot.player.state.DeviceStateHandler import DeviceStateHandler
from librespot.standard.Closeable import Closeable
import logging
import sched
import time


class Player(Closeable, PlayerSession.Listener, AudioSink.Listener):
Expand All @@ -34,7 +37,8 @@ def __init__(self, conf: PlayerConfiguration, session: Session):
self._init_state()

def _init_state(self):
self._state = StateWrapper.StateWrapper(self._session, self, self._conf)
self._state = StateWrapper.StateWrapper(self._session, self,
self._conf)

class Anonymous(DeviceStateHandler.Listener):
_player: Player = None
Expand All @@ -45,8 +49,13 @@ def __init__(self, player: Player):
def ready(self) -> None:
pass

def command(self, endpoint: DeviceStateHandler.Endpoint, data: DeviceStateHandler.CommandBody) -> None:
self._player._LOGGER.debug("Received command: {}".format(endpoint))
def command(
self,
endpoint: DeviceStateHandler.Endpoint,
data: DeviceStateHandler.CommandBody,
) -> None:
self._player._LOGGER.debug(
"Received command: {}".format(endpoint))

self._deviceStateListener = Anonymous(self)
self._state.add_listener(self._deviceStateListener)
Expand Down
32 changes: 20 additions & 12 deletions librespot/player/StateWrapper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from __future__ import annotations

from librespot.core import Session
from librespot.dealer import DealerClient
from librespot.player import Player, PlayerConfiguration
from librespot.player import Player
from librespot.player import PlayerConfiguration
from librespot.player.state import DeviceStateHandler
from librespot.proto import Connect
from librespot.proto.Player import ContextPlayerOptions, PlayerState, Restrictions, Suppressions
from librespot.proto.Player import ContextPlayerOptions
from librespot.proto.Player import PlayerState
from librespot.proto.Player import Restrictions
from librespot.proto.Player import Suppressions


class StateWrapper(DeviceStateHandler.Listener, DealerClient.MessageListener):
Expand All @@ -21,29 +26,32 @@ def __init__(self, session: Session, player: Player,
self._state = self._init_state()

self._device.add_listener(self)
self._session.dealer().add_message_listener(self, "spotify:user:attributes:update", "hm://playlist/", "hm://collection/collection/" + self._session.username() + "/json")
self._session.dealer().add_message_listener(
self,
"spotify:user:attributes:update",
"hm://playlist/",
"hm://collection/collection/" + self._session.username() + "/json",
)

def _init_state(self) -> PlayerState:
return PlayerState(
playback_speed=1.0,
suppressions=Suppressions(),
context_restrictions=Restrictions(),
options=ContextPlayerOptions(
repeating_context=False,
shuffling_context=False,
repeating_track=False
),
options=ContextPlayerOptions(repeating_context=False,
shuffling_context=False,
repeating_track=False),
position_as_of_timestamp=0,
position=0,
is_playing=False
is_playing=False,
)

def add_listener(self, listener: DeviceStateHandler.Listener):
self._device.add_listener(listener)

def ready(self) -> None:
self._device.update_state(Connect.PutStateReason.NEW_DEVICE, 0, self._state)
self._device.update_state(Connect.PutStateReason.NEW_DEVICE, 0,
self._state)

def on_message(self, uri: str, headers: dict[str, str],
payload: bytes):
def on_message(self, uri: str, headers: dict[str, str], payload: bytes):
pass
4 changes: 3 additions & 1 deletion librespot/player/mixing/AudioSink.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

from librespot.player import PlayerConfiguration


class AudioSink:
def __init__(self, conf: PlayerConfiguration, listener: AudioSink.Listener):
def __init__(self, conf: PlayerConfiguration,
listener: AudioSink.Listener):
pass

class Listener:
Expand Down
37 changes: 26 additions & 11 deletions librespot/player/state/DeviceStateHandler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from __future__ import annotations
from librespot.common import Utils
from librespot.core import Session
from librespot.player import PlayerConfiguration
from librespot.proto import Connect, Player

import base64
import concurrent.futures
import enum
Expand All @@ -11,14 +8,21 @@
import typing
import urllib.parse

from librespot.common import Utils
from librespot.core import Session
from librespot.player import PlayerConfiguration
from librespot.proto import Connect
from librespot.proto import Player


class DeviceStateHandler:
_LOGGER: logging = logging.getLogger(__name__)
_session: Session = None
_deviceInfo: Connect.DeviceInfo = None
_listeners: list[DeviceStateHandler.Listener] = list()
_putState: Connect.PutStateRequest = None
_putStateWorker: concurrent.futures.ThreadPoolExecutor = concurrent.futures.ThreadPoolExecutor()
_putStateWorker: concurrent.futures.ThreadPoolExecutor = (
concurrent.futures.ThreadPoolExecutor())
_connectionId: str = None

def __init__(self, session: Session, player, conf: PlayerConfiguration):
Expand All @@ -29,10 +33,10 @@ def __init__(self, session: Session, player, conf: PlayerConfiguration):
def _update_connection_id(self, newer: str) -> None:
newer = urllib.parse.unquote(newer, "UTF-8")

if self._connectionId is None or \
self._connectionId != newer:
if self._connectionId is None or self._connectionId != newer:
self._connectionId = newer
self._LOGGER.debug("Updated Spotify-Connection-Id: {}".format(self._connectionId))
self._LOGGER.debug("Updated Spotify-Connection-Id: {}".format(
self._connectionId))
self._notify_ready()

def add_listener(self, listener: DeviceStateHandler.Listener):
Expand All @@ -42,7 +46,12 @@ def _notify_ready(self) -> None:
for listener in self._listeners:
listener.ready()

def update_state(self, reason: Connect.PutStateReason, player_time: int, state: Player.PlayerState):
def update_state(
self,
reason: Connect.PutStateReason,
player_time: int,
state: Player.PlayerState,
):
if self._connectionId is None:
raise TypeError()

Expand All @@ -61,7 +70,9 @@ def update_state(self, reason: Connect.PutStateReason, player_time: int, state:
def _put_connect_state(self, req: Connect.PutStateRequest):
self._session.api().put_connect_state(self._connectionId, req)
self._LOGGER.info("Put state. ts: {}, connId: {}, reason: {}".format(
req.client_side_timestamp, Utils.truncate_middle(self._connectionId, 10), req.put_state_reason
req.client_side_timestamp,
Utils.truncate_middle(self._connectionId, 10),
req.put_state_reason,
))

class Endpoint(enum.Enum):
Expand All @@ -76,7 +87,11 @@ class Listener:
def ready(self) -> None:
pass

def command(self, endpoint: DeviceStateHandler.Endpoint, data: DeviceStateHandler.CommandBody) -> None:
def command(
self,
endpoint: DeviceStateHandler.Endpoint,
data: DeviceStateHandler.CommandBody,
) -> None:
pass

def volume_changed(self) -> None:
Expand Down

0 comments on commit a47a5db

Please sign in to comment.