Change Directory

This commit is contained in:
kokarare1212
2021-02-25 08:07:17 +09:00
parent 01dae8ada4
commit 8daf68831e
133 changed files with 221 additions and 38 deletions

View File

@@ -0,0 +1,3 @@
class AutoCloseable:
def close(self) -> None:
pass

View File

@@ -0,0 +1,68 @@
from librespot.standard.OutputStream import OutputStream
class ByteArrayOutputStream(OutputStream):
buf: bytearray
count: int = 0
def __init__(self, size: int = 32):
if size < 0:
raise RuntimeError("Negative initial size: {}".format(self))
self.buf = bytearray(size)
def ensure_capacity(self, min_capacity: int) -> None:
old_capacity = len(self.buf)
min_growth = min_capacity - old_capacity
if min_growth > 0:
new_buf = bytearray(min_capacity)
new_buf[0:len(self.buf)] = self.buf
self.buf = new_buf
def internal_write(self, byte: int) -> None:
self.ensure_capacity(self.count + 1)
self.buf[self.count] = byte
self.count += 1
def write(self,
byte: int = None,
buffer: bytearray = None,
offset: int = None,
length: int = None) -> None:
if byte is not None and buffer is None and offset is None and length is None:
self.internal_write(byte)
return
elif byte is None and buffer is not None and offset is None and length is None:
offset = 0
length = len(buffer)
elif not (byte is None and buffer is not None and offset is not None
and length is not None):
raise TypeError()
if len(buffer) < (offset + length):
raise IndexError()
self.ensure_capacity(self.count + length)
self.buf[self.count:self.count + length] = buffer[offset:offset +
length]
self.count += length
def write_bytes(self, b: bytearray):
self.write(buffer=b, offset=0, length=len(b))
def write_to(self, out: OutputStream) -> None:
out.write(buffer=self.buf, offset=0, length=self.count)
def reset(self) -> None:
self.count = 0
def to_byte_array(self) -> bytearray:
return self.buf
def to_bytes(self) -> bytes:
return bytes(self.buf)
def size(self) -> int:
return self.count
def close(self) -> None:
pass

View File

@@ -0,0 +1,42 @@
import struct
class BytesInputStream:
buffer: bytes
endian: str
def __init__(self, buffer: bytes, endian: str = ">"):
self.buffer = buffer
self.endian = endian
def read(self, length: int = None) -> bytes:
if length is None:
length = len(self.buffer)
buffer = self.buffer[:length]
self.buffer = self.buffer[length:]
return buffer
def read_byte(self) -> bytes:
buffer = struct.unpack("s", self.buffer[:1])[0]
self.buffer = self.buffer[1:]
return buffer
def read_int(self) -> int:
buffer = struct.unpack("{}i".format(self.endian), self.buffer[:4])[0]
self.buffer = self.buffer[4:]
return buffer
def read_short(self) -> int:
buffer = struct.unpack("{}h".format(self.endian), self.buffer[:2])[0]
self.buffer = self.buffer[2:]
return buffer
def read_long(self) -> int:
buffer = struct.unpack("{}q".format(self.endian), self.buffer[:8])[0]
self.buffer = self.buffer[8:]
return buffer
def read_float(self) -> float:
buffer = struct.unpack("{}f".format(self.endian), self.buffer[:4])[0]
self.buffer = self.buffer[4:]
return buffer

View File

@@ -0,0 +1,24 @@
import struct
class BytesOutputStream:
buffer: bytes
def __init__(self):
self.buffer = b""
def write(self, data: bytes):
self.buffer += data
return len(data)
def write_byte(self, data: int):
self.buffer += bytes([data])
return 1
def write_int(self, data: int):
self.buffer += struct.pack(">i", data)
return 4
def write_short(self, data: int):
self.buffer += struct.pack(">h", data)
return 2

View File

@@ -0,0 +1,6 @@
from librespot.standard.AutoCloseable import AutoCloseable
class Closeable(AutoCloseable):
def close(self) -> None:
pass

View File

@@ -0,0 +1,48 @@
class DataInput:
def internal_read_fully(self, b: bytearray) -> None:
pass
def read_fully(self,
b: bytearray = None,
offset: int = None,
length: int = None) -> None:
pass
def skip_bytes(self, n: int) -> int:
pass
def read_boolean(self) -> bool:
pass
def read_byte(self) -> bytes:
pass
def read_unsigned_byte(self) -> int:
pass
def read_short(self) -> int:
pass
def read_unsigned_short(self) -> int:
pass
def read_char(self) -> str:
pass
def read_int(self) -> int:
pass
def read_long(self) -> int:
pass
def read_float(self) -> float:
pass
def read_double(self) -> float:
pass
def read_line(self) -> str:
pass
def read_utf(self) -> str:
pass

View File

@@ -0,0 +1,118 @@
from librespot.standard.DataInput import DataInput
from librespot.standard.FilterInputStream import FilterInputStream
from librespot.standard.InputStream import InputStream
class DataInputStream(FilterInputStream, DataInput):
def __init__(self, input_stream: InputStream):
super().__init__(input_stream)
def read(self,
b: bytearray = None,
offset: int = None,
length: int = None) -> int:
if b is not None and offset is None and length is None:
return self.input_stream.read(b, 0, len(b))
elif b is not None and offset is not None and length is not None:
return self.input_stream.read(b, offset, length)
else:
raise TypeError()
def read_fully(self,
b: bytearray = None,
offset: int = None,
length: int = None) -> None:
if b is not None and offset is None and length is None:
offset = 0
length = len(b)
elif not (b is not None and offset is not None and length is not None):
raise TypeError()
if length < 0:
raise IndexError()
n = 0
while n < length:
count = self.input_stream.read(b, offset + n, length - n)
if count < 0:
raise EOFError()
n += count
def skip_bytes(self, n: int) -> int:
total = 0
cur = 0
while True:
cur = self.input_stream.skip(n - total)
if not (total < n and cur > 0):
break
total += cur
return total
def read_boolean(self) -> bool:
ch = self.input_stream.read()
if ch < 0:
raise EOFError()
return ch != 0
def read_byte(self) -> bytes:
ch = self.input_stream.read()
if ch < 0:
raise EOFError()
return bytes([ch])
def read_unsigned_byte(self) -> int:
ch = self.input_stream.read()
if ch < 0:
raise EOFError()
return ch
def read_short(self) -> int:
ch1 = self.input_stream.read()
ch2 = self.input_stream.read()
if (ch1 | ch2) < 0:
raise EOFError()
return (ch1 << 8) + (ch2 << 0)
def read_unsigned_short(self) -> int:
ch1 = self.input_stream.read()
ch2 = self.input_stream.read()
if (ch1 | ch2) < 0:
raise EOFError()
return (ch1 << 8) + (ch2 << 0)
def read_char(self) -> str:
ch1 = self.input_stream.read()
ch2 = self.input_stream.read()
if (ch1 | ch2) < 0:
raise EOFError()
return chr((ch1 << 8) + (ch2 << 0))
def read_int(self) -> int:
ch1 = self.input_stream.read()
ch2 = self.input_stream.read()
ch3 = self.input_stream.read()
ch4 = self.input_stream.read()
if (ch1 | ch2 | ch3 | ch4) < 0:
raise EOFError()
return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)
read_buffer = bytearray(8)
def read_long(self) -> int:
self.read_fully(self.read_buffer, 0, 8)
return (self.read_buffer[0] << 56) + \
((self.read_buffer[1] & 255) << 48) + \
((self.read_buffer[2] & 255) << 40) + \
((self.read_buffer[3] & 255) << 32) + \
((self.read_buffer[4] & 255) << 24) + \
((self.read_buffer[5] & 255) << 16) + \
((self.read_buffer[6] & 255) << 8) + \
((self.read_buffer[7] & 255) << 0)
def read_float(self) -> float:
pass
def read_double(self) -> float:
pass
def read_line(self) -> str:
pass

View File

@@ -0,0 +1,43 @@
class DataOutput:
def internal_write(self, byte: int) -> None:
pass
def write(self,
byte: int = None,
buffer: bytearray = None,
offset: int = None,
length: int = None) -> None:
pass
def write_boolean(self, v: bytes) -> None:
pass
def write_byte(self, v: int) -> None:
pass
def write_short(self, v: int) -> None:
pass
def write_char(self, v: int) -> None:
pass
def write_int(self, v: int) -> None:
pass
def write_long(self, v: int) -> None:
pass
def write_float(self, v: float) -> None:
pass
def write_double(self, v: float) -> None:
pass
def write_bytes(self, s: str) -> None:
pass
def write_chars(self, s: str) -> None:
pass
def write_utf(self, s: str) -> None:
pass

View File

@@ -0,0 +1,41 @@
from librespot.standard.InputStream import InputStream
class FilterInputStream(InputStream):
input_stream: InputStream
def __init__(self, input_stream: InputStream):
self.input_stream = input_stream
def internal_read(self):
return self.input_stream.read()
def read(self,
b: bytearray = None,
offset: int = None,
length: int = None) -> int:
if b is not None and offset is None and length is None:
offset = 0
length = len(b)
elif not (b is not None and offset is not None and length is not None):
raise TypeError()
return self.input_stream.read(b, offset, length)
def skip(self, n: int) -> int:
return self.input_stream.skip(n)
def available(self) -> int:
return self.input_stream.available()
def close(self) -> None:
self.input_stream.close()
def mark(self, read_limit: int) -> None:
self.input_stream.mark(read_limit)
def reset(self) -> None:
self.input_stream.reset()
def mark_supported(self) -> bool:
return self.input_stream.mark_supported()

View File

@@ -0,0 +1,3 @@
class Flushable:
def flush(self) -> None:
pass

View File

@@ -0,0 +1,240 @@
from __future__ import annotations
from librespot.standard.Closeable import Closeable
import sys
import typing
if typing.TYPE_CHECKING:
from librespot.standard.OutputStream import OutputStream
class InputStream(Closeable):
max_skip_buffer_size: typing.Final[int] = 2048
default_buffer_size: typing.Final[int] = 8192
@staticmethod
def null_input_stream():
class Anonymous(InputStream):
closed: bool
def ensure_open(self) -> None:
if self.closed:
raise IOError("Stream closed")
def available(self) -> int:
self.ensure_open()
return 0
def read(self,
b: bytearray = None,
offset: int = None,
length: int = None) -> int:
if b is not None and offset is not None and length is not None:
if len(b) < (offset + length):
raise IndexError()
if length == 0:
return 0
self.ensure_open()
return -1
elif b is None and offset is None and length is None:
self.ensure_open()
return -1
else:
raise TypeError()
def read_all_bytes(self):
self.ensure_open()
return bytearray(0)
def read_n_bytes(self,
b: bytearray = None,
offset: int = None,
length: int = None) -> bytearray:
if length < 0:
raise TypeError("length < 0")
self.ensure_open()
return bytearray(0)
def skip(self, n) -> int:
self.ensure_open()
return 0
def skip_n_bytes(self, n: int) -> None:
self.ensure_open()
if n > 0:
raise EOFError()
def transfer_to(self, out) -> int:
if out is None:
raise TypeError()
self.ensure_open()
return 0
def close(self):
self.closed = True
return Anonymous()
def internal_read(self):
raise NotImplementedError()
def read(self,
b: bytearray = None,
offset: int = None,
length: int = None) -> int:
if b is None and offset is None and length is None:
return self.internal_read()
elif b is not None and offset is None and length is None:
offset = 0
length = len(b)
elif not (b is not None and offset is not None and length is not None):
raise TypeError()
if len(b) < (offset + length):
raise IndexError()
if length == 0:
return 0
c = self.read()
if c == -1:
return -1
b[offset] = c
i = 1
for i in range(i, length):
c = self.read()
if c == -1:
break
b[offset + i] = c
return i
max_buffer_size: typing.Final[int] = sys.maxsize - 8
def read_all_bytes(self) -> bytearray:
return self.read_n_bytes(length=sys.maxsize)
def read_n_bytes(self,
b: bytearray = None,
offset: int = None,
length: int = None) -> typing.Union[bytearray, int]:
if b is None and offset is None and len is not None:
if length < 0:
raise TypeError("length < 0")
bufs = None
result = None
total = 0
remaining = length
n: int
while True:
buf = bytearray(min(remaining, self.default_buffer_size))
nread = 0
while True:
n = self.read(buf, nread, min(len(buf) - nread, remaining))
if not n > 0:
break
nread += n
remaining -= n
if nread > 0:
if self.max_buffer_size - total < nread:
raise MemoryError("Required array size too large")
total += nread
if result is None:
result = buf
else:
if bufs is None:
bufs = [result]
bufs.append(buf)
if n >= 0 and remaining > 0:
break
if bufs is None:
if result is None:
return bytearray(0)
return result if len(result) == total else result[:total]
result = bytearray(total)
offset = 0
remaining = total
for b in bufs:
count = min(len(b), remaining)
for i in range(offset, offset + count):
result.insert(i, b[i])
offset += count
remaining -= count
return result
elif b is not None and offset is not None and length is not None:
if len(b) < (offset + length):
raise IndexError()
n = 0
while n < length:
count = self.read(b, offset + n, length - n)
if count < 0:
break
n += count
return n
else:
raise TypeError()
def skip(self, n: int) -> int:
remaining = n
nr: int
if n <= 0:
return 0
size = min(self.max_skip_buffer_size, remaining)
skip_buffer = bytearray(size)
while remaining > 0:
nr = self.read(skip_buffer, 0, min(size, remaining))
if nr < 0:
break
remaining -= nr
return n - remaining
def skip_n_bytes(self, n: int) -> None:
if n > 0:
ns = self.skip(n)
if ns >= 0 and ns < n:
n -= ns
while n > 0 and self.read() != -1:
n -= 1
if n != 0:
raise EOFError()
elif ns != n:
raise IOError("Unable to skip exactly")
def available(self) -> int:
return 0
def close(self) -> None:
pass
def mark(self, read_limit: int) -> None:
pass
def reset(self) -> None:
raise IOError("mark/reset not supported")
def mark_supported(self) -> bool:
return False
def transfer_to(self, out: OutputStream) -> int:
if out is None:
raise TypeError()
transferred = 0
buffer = bytearray(self.default_buffer_size)
read: int
while True:
read = self.read(buffer, 0, self.default_buffer_size)
if not read:
break
out.write(buffer=buffer, offset=0, length=read)
transferred += read
return transferred

View File

@@ -0,0 +1,61 @@
from librespot.standard.Closeable import Closeable
from librespot.standard.Flushable import Flushable
class OutputStream(Closeable, Flushable):
def null_output_stream(self):
class Annonymous(OutputStream):
closed: bool
def ensure_open(self) -> None:
if self.closed:
raise IOError("Stream closed")
def internal_write(self, byte: int):
self.ensure_open()
def write(self,
byte: int = None,
buffer: bytearray = None,
offset: int = None,
length: int = None):
if byte is not None and buffer is None and offset is None and length is None:
self.internal_write(byte)
elif not (byte is None and buffer is not None
and offset is not None and length is not None):
raise TypeError()
if len(bytearray) < (offset + length):
raise IndexError()
self.ensure_open()
def close(self) -> None:
self.closed = True
def internal_write(self, byte: int):
raise NotImplementedError()
def write(self,
byte: int = None,
buffer: bytearray = None,
offset: int = None,
length: int = None):
if byte is not None and buffer is None and offset is None and length is None:
self.internal_write(byte)
elif byte is None and buffer is not None and offset is None and length is None:
offset = 0
length = len(buffer)
elif not (byte is None and buffer is not None and offset is not None
and length is not None):
raise TypeError()
if len(bytearray) < (offset + length):
raise IndexError()
for i in range(length):
self.write(buffer[offset + i])
def flush(self) -> None:
pass
def close(self) -> None:
pass

View File

@@ -0,0 +1,9 @@
from __future__ import annotations
import enum
class Proxy:
class Type(enum.Enum):
DIRECT = enum.auto()
HTTP = enum.auto()
SOCKS = enum.auto()

View File

@@ -0,0 +1,3 @@
class Runnable:
def run(self) -> None:
raise NotImplementedError()

View File

@@ -0,0 +1,14 @@
from librespot.standard.AutoCloseable import AutoCloseable
from librespot.standard.ByteArrayOutputStream import ByteArrayOutputStream
from librespot.standard.BytesInputStream import BytesInputStream
from librespot.standard.BytesOutputStream import BytesOutputStream
from librespot.standard.Closeable import Closeable
from librespot.standard.DataInput import DataInput
from librespot.standard.DataInputStream import DataInputStream
from librespot.standard.DataOutput import DataOutput
from librespot.standard.FilterInputStream import FilterInputStream
from librespot.standard.Flushable import Flushable
from librespot.standard.InputStream import InputStream
from librespot.standard.OutputStream import OutputStream
from librespot.standard.Proxy import Proxy
from librespot.standard.Runnable import Runnable