Merge pull request #58 from kokarare1212/deepsource-transform-d7e0d881

Format code with yapf
This commit is contained in:
こうから
2021-09-12 14:01:06 +09:00
committed by GitHub
14 changed files with 309 additions and 187 deletions

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")