Format code with yapf

This commit fixes the style issues introduced in 2790f48 according to the output
from yapf.

Details: https://deepsource.io/gh/kokarare1212/librespot-python/transform/04a80a03-ea85-44b3-9159-2580bda68c1c/
This commit is contained in:
deepsource-autofix[bot]
2021-09-12 04:58:42 +00:00
committed by GitHub
parent 2790f484c8
commit 0741dbdd43
15 changed files with 396 additions and 237 deletions

View File

@@ -190,7 +190,6 @@ class AbsChunkedInputStream(io.BytesIO, HaltListener):
buffer.seek(0)
return buffer.read()
def notify_chunk_available(self, index: int) -> None:
self.available_chunks()[index] = True
self.__decoded_length += len(self.buffer()[index])
@@ -250,7 +249,10 @@ class AudioKeyManager(PacketsReceiver, Closeable):
"Couldn't handle packet, cmd: {}, length: {}".format(
packet.cmd, len(packet.payload)))
def get_audio_key(self, gid: bytes, file_id: bytes, retry: bool = True) -> bytes:
def get_audio_key(self,
gid: bytes,
file_id: bytes,
retry: bool = True) -> bytes:
seq: int
with self.__seq_holder_lock:
seq = self.__seq_holder
@@ -294,14 +296,16 @@ class AudioKeyManager(PacketsReceiver, Closeable):
self.__reference_lock.notify_all()
def error(self, code: int) -> None:
self.__audio_key_manager.logger.fatal("Audio key error, code: {}".format(code))
self.__audio_key_manager.logger.fatal(
"Audio key error, code: {}".format(code))
with self.__reference_lock:
self.__reference.put(None)
self.__reference_lock.notify_all()
def wait_response(self) -> bytes:
with self.__reference_lock:
self.__reference_lock.wait(AudioKeyManager.audio_key_request_timeout)
self.__reference_lock.wait(
AudioKeyManager.audio_key_request_timeout)
return self.__reference.get(block=False)
@@ -313,9 +317,11 @@ class CdnFeedHelper:
return random.choice(resp.cdnurl)
@staticmethod
def load_track(session: Session, track: Metadata.Track, file: Metadata.AudioFile,
resp_or_url: typing.Union[StorageResolve.StorageResolveResponse, str],
preload: bool, halt_listener: HaltListener) -> PlayableContentFeeder.LoadedStream:
def load_track(
session: Session, track: Metadata.Track, file: Metadata.AudioFile,
resp_or_url: typing.Union[StorageResolve.StorageResolveResponse,
str], preload: bool,
halt_listener: HaltListener) -> PlayableContentFeeder.LoadedStream:
if type(resp_or_url) is str:
url = resp_or_url
else:
@@ -333,15 +339,14 @@ class CdnFeedHelper:
track,
streamer,
normalization_data,
PlayableContentFeeder.Metrics(
file.file_id, preload, -1 if preload else audio_key_time),
PlayableContentFeeder.Metrics(file.file_id, preload,
-1 if preload else audio_key_time),
)
@staticmethod
def load_episode_external(
session: Session, episode: Metadata.Episode,
halt_listener: HaltListener
) -> PlayableContentFeeder.LoadedStream:
session: Session, episode: Metadata.Episode,
halt_listener: HaltListener) -> PlayableContentFeeder.LoadedStream:
resp = session.client().head(episode.external_url)
if resp.status_code != 200:
@@ -385,8 +390,7 @@ class CdnFeedHelper:
episode,
streamer,
normalization_data,
PlayableContentFeeder.Metrics(
file.file_id, False, audio_key_time),
PlayableContentFeeder.Metrics(file.file_id, False, audio_key_time),
)
@@ -408,7 +412,9 @@ class CdnManager:
raise IOError("Response body is empty!")
return body
def stream_external_episode(self, episode: Metadata.Episode, external_url: str, halt_listener: HaltListener):
def stream_external_episode(self, episode: Metadata.Episode,
external_url: str,
halt_listener: HaltListener):
return CdnManager.Streamer(
self.__session,
StreamId(episode),
@@ -419,7 +425,8 @@ class CdnManager:
halt_listener,
)
def stream_file(self, file: Metadata.AudioFile, key: bytes, url: str, halt_listener: HaltListener):
def stream_file(self, file: Metadata.AudioFile, key: bytes, url: str,
halt_listener: HaltListener):
return CdnManager.Streamer(
self.__session,
StreamId(file),
@@ -442,9 +449,11 @@ class CdnManager:
proto.ParseFromString(body)
if proto.result == StorageResolve.StorageResolveResponse.Result.CDN:
url = random.choice(proto.cdnurl)
self.logger.debug("Fetched CDN url for {}: {}".format(util.bytes_to_hex(file_id), url))
self.logger.debug("Fetched CDN url for {}: {}".format(
util.bytes_to_hex(file_id), url))
return url
raise CdnManager.CdnException("Could not retrieve CDN url! result: {}".format(proto.result))
raise CdnManager.CdnException(
"Could not retrieve CDN url! result: {}".format(proto.result))
class CdnException(Exception):
pass
@@ -463,7 +472,8 @@ class CdnManager:
__expiration: int
url: str
def __init__(self, cdn_manager, file_id: typing.Union[bytes, None], url: str):
def __init__(self, cdn_manager, file_id: typing.Union[bytes, None],
url: str):
self.__cdn_manager: CdnManager = cdn_manager
self.__file_id = file_id
self.set_url(url)
@@ -498,7 +508,8 @@ class CdnManager:
break
if expire_at is None:
self.__expiration = -1
self.__cdn_manager.logger.warning("Invalid __token__ in CDN url: {}".format(url))
self.__cdn_manager.logger.warning(
"Invalid __token__ in CDN url: {}".format(url))
return
self.__expiration = expire_at * 1000
else:
@@ -529,16 +540,18 @@ class CdnManager:
__session: Session
__stream_id: StreamId
def __init__(self, session: Session, stream_id: StreamId, audio_format: SuperAudioFormat,
cdn_url: CdnManager.CdnUrl, cache: CacheManager, audio_decrypt: AudioDecrypt,
halt_listener: HaltListener):
def __init__(self, session: Session, stream_id: StreamId,
audio_format: SuperAudioFormat,
cdn_url: CdnManager.CdnUrl, cache: CacheManager,
audio_decrypt: AudioDecrypt, halt_listener: HaltListener):
self.__session = session
self.__stream_id = stream_id
self.__audio_format = audio_format
self.__audio_decrypt = audio_decrypt
self.__cdn_url = cdn_url
self.halt_listener = halt_listener
response = self.request(range_start=0, range_end=ChannelManager.chunk_size - 1)
response = self.request(range_start=0,
range_end=ChannelManager.chunk_size - 1)
content_range = response.headers.get("Content-Range")
if content_range is None:
raise IOError("Missing Content-Range header!")
@@ -549,7 +562,8 @@ class CdnManager:
self.available = [False for _ in range(self.chunks)]
self.requested = [False for _ in range(self.chunks)]
self.buffer = [b"" for _ in range(self.chunks)]
self.__internal_stream = CdnManager.Streamer.InternalStream(self, False)
self.__internal_stream = CdnManager.Streamer.InternalStream(
self, False)
self.requested[0] = True
self.write_chunk(first_chunk, 0, False)
@@ -557,9 +571,11 @@ class CdnManager:
cached: bool) -> None:
if self.__internal_stream.is_closed():
return
self.__session.logger.debug("Chunk {}/{} completed, cached: {}, stream: {}"
.format(chunk_index + 1, self.chunks, cached, self.describe()))
self.buffer[chunk_index] = self.__audio_decrypt.decrypt_chunk(chunk_index, chunk)
self.__session.logger.debug(
"Chunk {}/{} completed, cached: {}, stream: {}".format(
chunk_index + 1, self.chunks, cached, self.describe()))
self.buffer[chunk_index] = self.__audio_decrypt.decrypt_chunk(
chunk_index, chunk)
self.__internal_stream.notify_chunk_available(chunk_index)
def stream(self) -> AbsChunkedInputStream:
@@ -590,7 +606,9 @@ class CdnManager:
range_end = (chunk + 1) * ChannelManager.chunk_size - 1
response = self.__session.client().get(
self.__cdn_url.url,
headers={"Range": "bytes={}-{}".format(range_start, range_end)},
headers={
"Range": "bytes={}-{}".format(range_start, range_end)
},
)
if response.status_code != 206:
raise IOError(response.status_code)
@@ -650,8 +668,9 @@ class NormalizationData:
self.album_gain_db = album_gain_db
self.album_peak = album_peak
self._LOGGER.debug("Loaded normalization data, track_gain: {}, track_peak: {}, album_gain: {}, album_peak: {}"
.format(track_gain_db, track_peak, album_gain_db, album_peak))
self._LOGGER.debug(
"Loaded normalization data, track_gain: {}, track_peak: {}, album_gain: {}, album_peak: {}"
.format(track_gain_db, track_peak, album_gain_db, album_peak))
@staticmethod
def read(input_stream: io.BytesIO) -> NormalizationData:
@@ -659,11 +678,15 @@ class NormalizationData:
data = input_stream.read(4 * 4)
input_stream.seek(16)
buffer = io.BytesIO(data)
return NormalizationData(struct.unpack("<f", buffer.read(4))[0], struct.unpack("<f", buffer.read(4))[0],
struct.unpack("<f", buffer.read(4))[0], struct.unpack("<f", buffer.read(4))[0])
return NormalizationData(
struct.unpack("<f", buffer.read(4))[0],
struct.unpack("<f", buffer.read(4))[0],
struct.unpack("<f", buffer.read(4))[0],
struct.unpack("<f", buffer.read(4))[0])
def get_factor(self, normalisation_pregain) -> float:
normalisation_factor = float(math.pow(10, (self.track_gain_db + normalisation_pregain) / 20))
normalisation_factor = float(
math.pow(10, (self.track_gain_db + normalisation_pregain) / 20))
if normalisation_factor * self.track_peak > 1:
self._LOGGER \
.warning("Reducing normalisation factor to prevent clipping. Please add negative pregain to avoid.")
@@ -680,19 +703,23 @@ class PlayableContentFeeder:
def __init__(self, session: Session):
self.__session = session
def load(self, playable_id: PlayableId, audio_quality_picker: AudioQualityPicker,
preload: bool, halt_listener: typing.Union[HaltListener, None]):
def load(self, playable_id: PlayableId,
audio_quality_picker: AudioQualityPicker, preload: bool,
halt_listener: typing.Union[HaltListener, None]):
if type(playable_id) is TrackId:
return self.load_track(playable_id, audio_quality_picker, preload, halt_listener)
return self.load_track(playable_id, audio_quality_picker, preload,
halt_listener)
def load_stream(self, file: Metadata.AudioFile, track: Metadata.Track,
episode: Metadata.Episode, preload: bool, halt_lister: HaltListener):
episode: Metadata.Episode, preload: bool,
halt_lister: HaltListener):
if track is None and episode is None:
raise RuntimeError()
response = self.resolve_storage_interactive(file.file_id, preload)
if response.result == StorageResolve.StorageResolveResponse.Result.CDN:
if track is not None:
return CdnFeedHelper.load_track(self.__session, track, file, response, preload, halt_lister)
return CdnFeedHelper.load_track(self.__session, track, file,
response, preload, halt_lister)
return CdnFeedHelper.load_episode(self.__session, episode, file,
response, preload, halt_lister)
elif response.result == StorageResolve.StorageResolveResponse.Result.STORAGE:
@@ -705,11 +732,13 @@ class PlayableContentFeeder:
else:
raise RuntimeError("Unknown result: {}".format(response.result))
def load_track(self, track_id_or_track: typing.Union[TrackId, Metadata.Track],
def load_track(self, track_id_or_track: typing.Union[TrackId,
Metadata.Track],
audio_quality_picker: AudioQualityPicker, preload: bool,
halt_listener: HaltListener):
if type(track_id_or_track) is TrackId:
original = self.__session.api().get_metadata_4_track(track_id_or_track)
original = self.__session.api().get_metadata_4_track(
track_id_or_track)
track = self.pick_alternative_if_necessary(original)
if track is None:
raise RuntimeError("Cannot get alternative track")
@@ -717,10 +746,12 @@ class PlayableContentFeeder:
track = track_id_or_track
file = audio_quality_picker.get_file(track.file)
if file is None:
self.logger.fatal("Couldn't find any suitable audio file, available")
self.logger.fatal(
"Couldn't find any suitable audio file, available")
return self.load_stream(file, track, None, preload, halt_listener)
def pick_alternative_if_necessary(self, track: Metadata.Track) -> typing.Union[Metadata.Track, None]:
def pick_alternative_if_necessary(
self, track: Metadata.Track) -> typing.Union[Metadata.Track, None]:
if len(track.file) > 0:
return track
for alt in track.alternative:
@@ -752,8 +783,11 @@ class PlayableContentFeeder:
preload: bool) -> StorageResolve.StorageResolveResponse:
resp = self.__session.api().send(
"GET",
(self.storage_resolve_interactive_prefetch if preload else self.storage_resolve_interactive)
.format(util.bytes_to_hex(file_id)), None, None,
(self.storage_resolve_interactive_prefetch
if preload else self.storage_resolve_interactive).format(
util.bytes_to_hex(file_id)),
None,
None,
)
if resp.status_code != 200:
raise RuntimeError(resp.status_code)
@@ -771,8 +805,10 @@ class PlayableContentFeeder:
normalization_data: NormalizationData
metrics: PlayableContentFeeder.Metrics
def __init__(self, track_or_episode: typing.Union[Metadata.Track, Metadata.Episode],
input_stream: GeneralAudioStream, normalization_data: typing.Union[NormalizationData, None],
def __init__(self, track_or_episode: typing.Union[Metadata.Track,
Metadata.Episode],
input_stream: GeneralAudioStream,
normalization_data: typing.Union[NormalizationData, None],
metrics: PlayableContentFeeder.Metrics):
if type(track_or_episode) is Metadata.Track:
self.track = track_or_episode
@@ -791,9 +827,10 @@ class PlayableContentFeeder:
preloaded_audio_key: bool
audio_key_time: int
def __init__(self, file_id: typing.Union[bytes, None], preloaded_audio_key: bool,
audio_key_time: int):
self.file_id = None if file_id is None else util.bytes_to_hex(file_id)
def __init__(self, file_id: typing.Union[bytes, None],
preloaded_audio_key: bool, audio_key_time: int):
self.file_id = None if file_id is None else util.bytes_to_hex(
file_id)
self.preloaded_audio_key = preloaded_audio_key
self.audio_key_time = audio_key_time
if preloaded_audio_key and audio_key_time != -1:

View File

@@ -27,9 +27,11 @@ class AudioQuality(enum.Enum):
return AudioQuality.VERY_HIGH
raise RuntimeError("Unknown format: {}".format(format))
def get_matches(self, files: typing.List[AudioFile]) -> typing.List[AudioFile]:
def get_matches(self,
files: typing.List[AudioFile]) -> typing.List[AudioFile]:
file_list = []
for file in files:
if hasattr(file, "format") and AudioQuality.get_quality(file.format) == self:
if hasattr(file, "format") and AudioQuality.get_quality(
file.format) == self:
file_list.append(file)
return file_list

View File

@@ -24,14 +24,16 @@ class AesAudioDecrypt(AudioDecrypt):
iv = self.iv_int + int(ChannelManager.chunk_size * chunk_index / 16)
start = time.time_ns()
for i in range(0, len(buffer), 4096):
cipher = AES.new(key=self.key, mode=AES.MODE_CTR,
cipher = AES.new(key=self.key,
mode=AES.MODE_CTR,
counter=Counter.new(128, initial_value=iv))
count = min(4096, len(buffer) - i)
decrypted_buffer = cipher.decrypt(buffer[i:i + count])
new_buffer.write(decrypted_buffer)
if count != len(decrypted_buffer):
raise RuntimeError("Couldn't process all data, actual: {}, expected: {}"
.format(len(decrypted_buffer), count))
raise RuntimeError(
"Couldn't process all data, actual: {}, expected: {}".
format(len(decrypted_buffer), count))
iv += self.iv_diff
self.decrypt_total_time += time.time_ns() - start
self.decrypt_count += 1
@@ -39,4 +41,5 @@ class AesAudioDecrypt(AudioDecrypt):
return new_buffer.read()
def decrypt_time_ms(self):
return 0 if self.decrypt_count == 0 else int((self.decrypt_total_time / self.decrypt_count) / 1000000)
return 0 if self.decrypt_count == 0 else int(
(self.decrypt_total_time / self.decrypt_count) / 1000000)

View File

@@ -50,21 +50,25 @@ class ChannelManager(Closeable, PacketsReceiver):
chunk_id = struct.unpack(">H", payload.read(2))[0]
channel = self.channels.get(chunk_id)
if channel is None:
self.logger.warning("Couldn't find channel, id: {}, received: {}"
.format(chunk_id, len(packet.payload)))
self.logger.warning(
"Couldn't find channel, id: {}, received: {}".format(
chunk_id, len(packet.payload)))
return
channel.add_to_queue(payload)
elif packet.is_cmd(Packet.Type.channel_error):
chunk_id = struct.unpack(">H", payload.read(2))[0]
channel = self.channels.get(chunk_id)
if channel is None:
self.logger.warning("Dropping channel error, id: {}, code: {}"
.format(chunk_id, struct.unpack(">H", payload.read(2))[0]))
self.logger.warning(
"Dropping channel error, id: {}, code: {}".format(
chunk_id,
struct.unpack(">H", payload.read(2))[0]))
return
channel.stream_error(struct.unpack(">H", payload.read(2))[0])
else:
self.logger.warning("Couldn't handle packet, cmd: {}, payload: {}"
.format(packet.cmd, util.bytes_to_hex(packet.payload)))
self.logger.warning(
"Couldn't handle packet, cmd: {}, payload: {}".format(
packet.cmd, util.bytes_to_hex(packet.payload)))
def close(self) -> None:
self.executor_service.shutdown()
@@ -86,14 +90,16 @@ class ChannelManager(Closeable, PacketsReceiver):
with self.channel_manager.seq_holder_lock:
self.chunk_id = self.channel_manager.seq_holder
self.channel_manager.seq_holder += 1
self.channel_manager.executor_service.submit(lambda: ChannelManager.Channel.Handler(self))
self.channel_manager.executor_service.submit(
lambda: ChannelManager.Channel.Handler(self))
def _handle(self, payload: bytes) -> bool:
if len(payload) == 0:
if not self.__header:
self.__file.write_chunk(payload, self.__chunk_index, False)
return True
self.channel_manager.logger.debug("Received empty chunk, skipping.")
self.channel_manager.logger.debug(
"Received empty chunk, skipping.")
return False
if self.__header:
length: int
@@ -123,7 +129,10 @@ class ChannelManager(Closeable, PacketsReceiver):
self.__channel = channel
def run(self) -> None:
self.__channel.channel_manager.logger.debug("ChannelManager.Handler is starting")
self.__channel.channel_manager.logger.debug(
"ChannelManager.Handler is starting")
with self.__channel.q.all_tasks_done:
self.__channel.channel_manager.channels.pop(self.__channel.chunk_id)
self.__channel.channel_manager.logger.debug("ChannelManager.Handler is shutting down")
self.__channel.channel_manager.channels.pop(
self.__channel.chunk_id)
self.__channel.channel_manager.logger.debug(
"ChannelManager.Handler is shutting down")