Skip to content

Commit

Permalink
Add type hint for mypy
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr committed May 7, 2021
1 parent 1d50380 commit 51ad874
Show file tree
Hide file tree
Showing 17 changed files with 839 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.

Added
-----
* type hint for mypy

Changed
-------
Expand Down
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ include *.yml
include LICENSE
include .coveragerc
include tox.ini
include py7zr/py.typed
recursive-include py7zr *.py
recursive-include py7zr *.pyi
recursive-include tests *.py
recursive-include tests/data *.txt
recursive-include tests/data *.7z
Expand Down
6 changes: 6 additions & 0 deletions py7zr/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from py7zr.exceptions import Bad7zFile as Bad7zFile, DecompressionError as DecompressionError, PasswordRequired as PasswordRequired, UnsupportedCompressionMethodError as UnsupportedCompressionMethodError
from py7zr.properties import CHECK_CRC32 as CHECK_CRC32, CHECK_CRC64 as CHECK_CRC64, CHECK_NONE as CHECK_NONE, CHECK_SHA256 as CHECK_SHA256, FILTER_ARM as FILTER_ARM, FILTER_ARMTHUMB as FILTER_ARMTHUMB, FILTER_BROTLI as FILTER_BROTLI, FILTER_BZIP2 as FILTER_BZIP2, FILTER_COPY as FILTER_COPY, FILTER_CRYPTO_AES256_SHA256 as FILTER_CRYPTO_AES256_SHA256, FILTER_DEFLATE as FILTER_DEFLATE, FILTER_DELTA as FILTER_DELTA, FILTER_IA64 as FILTER_IA64, FILTER_LZMA as FILTER_LZMA, FILTER_LZMA2 as FILTER_LZMA2, FILTER_POWERPC as FILTER_POWERPC, FILTER_PPMD as FILTER_PPMD, FILTER_SPARC as FILTER_SPARC, FILTER_X86 as FILTER_X86, FILTER_ZSTD as FILTER_ZSTD, PRESET_DEFAULT as PRESET_DEFAULT, PRESET_EXTREME as PRESET_EXTREME
from py7zr.py7zr import ArchiveInfo as ArchiveInfo, FileInfo as FileInfo, SevenZipFile as SevenZipFile, is_7zfile as is_7zfile, pack_7zarchive as pack_7zarchive, unpack_7zarchive as unpack_7zarchive

# Names in __all__ with no definition:
# __version__
3 changes: 3 additions & 0 deletions py7zr/__main__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import cli as cli

def main(): ...
142 changes: 142 additions & 0 deletions py7zr/archiveinfo.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
from io import BytesIO
from py7zr.compressor import SevenZipCompressor as SevenZipCompressor, SevenZipDecompressor as SevenZipDecompressor
from py7zr.exceptions import Bad7zFile as Bad7zFile
from py7zr.helpers import ArchiveTimestamp as ArchiveTimestamp, calculate_crc32 as calculate_crc32
from py7zr.properties import DEFAULT_FILTERS as DEFAULT_FILTERS, MAGIC_7Z as MAGIC_7Z, PROPERTY as PROPERTY
from typing import Any, BinaryIO, List, Tuple

MAX_LENGTH: int
P7ZIP_MAJOR_VERSION: bytes
P7ZIP_MINOR_VERSION: bytes

def read_crcs(file: BinaryIO, count: int) -> List[int]: ...
def write_crcs(file: BinaryIO, crcs: Any) -> Any: ...
def read_byte(file: BinaryIO) -> int: ...
def write_bytes(file: BinaryIO, data: bytes) -> Any: ...
def write_byte(file: BinaryIO, data: Any) -> Any: ...
def read_real_uint64(file: BinaryIO) -> Tuple[int, bytes]: ...
def read_uint32(file: BinaryIO) -> Tuple[int, bytes]: ...
def write_uint32(file: BinaryIO, value: Any) -> Any: ...
def read_uint64(file: BinaryIO) -> int: ...
def write_real_uint64(file: BinaryIO, value: int) -> Any: ...
def write_uint64(file: BinaryIO, value: int) -> Any: ...
def read_boolean(file: BinaryIO, count: int, checkall: bool=...) -> List[bool]: ...
def write_boolean(file: BinaryIO, booleans: List[bool], all_defined: bool=...) -> Any: ...
def read_utf16(file: BinaryIO) -> str: ...
def write_utf16(file: BinaryIO, val: str) -> Any: ...
def bits_to_bytes(bit_length: int) -> int: ...

class PackInfo:
packpos: int = ...
numstreams: int = ...
packsizes: Any = ...
digestdefined: Any = ...
crcs: Any = ...
enable_digests: bool = ...
def __init__(self) -> None: ...
@classmethod
def retrieve(cls, file: BinaryIO) -> Any: ...
def write(self, file: BinaryIO) -> Any: ...

class Bond:
incoder: Any = ...
outcoder: Any = ...
def __init__(self, incoder: Any, outcoder: Any) -> None: ...

class Folder:
unpacksizes: Any = ...
coders: Any = ...
bindpairs: Any = ...
packed_indices: Any = ...
solid: bool = ...
digestdefined: bool = ...
crc: Any = ...
decompressor: Any = ...
compressor: Any = ...
files: Any = ...
password: Any = ...
def __init__(self) -> None: ...
@classmethod
def retrieve(cls, file: BinaryIO) -> Any: ...
def prepare_coderinfo(self, filters: Any) -> None: ...
def write(self, file: BinaryIO) -> Any: ...
def is_simple(self, coder: Any): ...
def get_decompressor(self, packsize: int, reset: bool=...) -> SevenZipDecompressor: ...
def get_compressor(self) -> SevenZipCompressor: ...
def get_unpack_size(self) -> int: ...

class UnpackInfo:
@classmethod
def retrieve(cls, file: BinaryIO) -> Any: ...
numfolders: Any = ...
folders: Any = ...
datastreamidx: Any = ...
def __init__(self) -> None: ...
def write(self, file: BinaryIO) -> Any: ...

class SubstreamsInfo:
digests: Any = ...
digestsdefined: Any = ...
unpacksizes: Any = ...
num_unpackstreams_folders: Any = ...
def __init__(self) -> None: ...
@classmethod
def retrieve(cls, file: BinaryIO, numfolders: int, folders: List[Folder]) -> Any: ...
def write(self, file: BinaryIO) -> Any: ...

class StreamsInfo:
packinfo: Any = ...
unpackinfo: Any = ...
substreamsinfo: Any = ...
def __init__(self) -> None: ...
@classmethod
def retrieve(cls, file: BinaryIO) -> Any: ...
def read(self, file: BinaryIO) -> None: ...
def write(self, file: BinaryIO) -> Any: ...

class HeaderStreamsInfo(StreamsInfo):
packinfo: Any = ...
unpackinfo: Any = ...
def __init__(self) -> None: ...
def write(self, file: BinaryIO) -> Any: ...

class FilesInfo:
files: Any = ...
emptyfiles: Any = ...
antifiles: Any = ...
def __init__(self) -> None: ...
@classmethod
def retrieve(cls, file: BinaryIO) -> Any: ...
def write(self, file: BinaryIO) -> Any: ...

class WriteWithCrc:
digest: int = ...
def __init__(self, fp: BinaryIO) -> None: ...
def write(self, data: Any): ...
def tell(self): ...

class Header:
__slot__: Any = ...
solid: bool = ...
main_streams: Any = ...
files_info: Any = ...
size: int = ...
password: Any = ...
def __init__(self) -> None: ...
@classmethod
def retrieve(cls, fp: BinaryIO, buffer: BytesIO, start_pos: int, password: Any=...) -> Any: ...
def write(self, file: BinaryIO, afterheader: int, encoded: Any=..., encrypted: Any=...) -> Any: ...
@staticmethod
def build_header(folders: Any): ...

class SignatureHeader:
version: Any = ...
startheadercrc: int = ...
nextheaderofs: int = ...
nextheadersize: int = ...
nextheadercrc: int = ...
def __init__(self) -> None: ...
@classmethod
def retrieve(cls, file: BinaryIO) -> Any: ...
def calccrc(self, length: int, header_crc: int) -> Any: ...
def write(self, file: BinaryIO) -> Any: ...
48 changes: 48 additions & 0 deletions py7zr/bcjfilter.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from typing import Any, Union

class BCJFilter:
is_encoder: Any = ...
prev_mask: int = ...
prev_pos: int = ...
current_position: int = ...
stream_size: Any = ...
buffer: Any = ...
def __init__(self, func: Any, readahead: int, is_encoder: bool, stream_size: int=...) -> None: ...
def sparc_code(self): ...
def ppc_code(self): ...
def armt_code(self) -> int: ...
def arm_code(self) -> int: ...
def x86_code(self) -> int: ...
def decode(self, data: Union[bytes, bytearray, memoryview], max_length: int=...) -> bytes: ...
def encode(self, data: Union[bytes, bytearray, memoryview]) -> bytes: ...
def flush(self): ...

class BCJDecoder(BCJFilter):
def __init__(self, size: int) -> None: ...

class BCJEncoder(BCJFilter):
def __init__(self) -> None: ...

class SparcDecoder(BCJFilter):
def __init__(self, size: int) -> None: ...

class SparcEncoder(BCJFilter):
def __init__(self) -> None: ...

class PpcDecoder(BCJFilter):
def __init__(self, size: int) -> None: ...

class PpcEncoder(BCJFilter):
def __init__(self) -> None: ...

class ArmtDecoder(BCJFilter):
def __init__(self, size: int) -> None: ...

class ArmtEncoder(BCJFilter):
def __init__(self) -> None: ...

class ArmDecoder(BCJFilter):
def __init__(self, size: int) -> None: ...

class ArmEncoder(BCJFilter):
def __init__(self) -> None: ...
18 changes: 18 additions & 0 deletions py7zr/callbacks.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import abc
from abc import ABC, abstractmethod
from typing import Any

class Callback(ABC, metaclass=abc.ABCMeta):
@abstractmethod
def report_start_preparation(self) -> Any: ...
@abstractmethod
def report_start(self, processing_file_path: Any, processing_bytes: Any) -> Any: ...
@abstractmethod
def report_end(self, processing_file_path: Any, wrote_bytes: Any) -> Any: ...
@abstractmethod
def report_warning(self, message: Any) -> Any: ...
@abstractmethod
def report_postprocess(self) -> Any: ...

class ExtractCallback(Callback, metaclass=abc.ABCMeta): ...
class ArchiveCallback(Callback, metaclass=abc.ABCMeta): ...
35 changes: 35 additions & 0 deletions py7zr/cli.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import argparse
from py7zr.callbacks import ExtractCallback as ExtractCallback
from py7zr.compressor import SupportedMethods as SupportedMethods
from py7zr.helpers import Local as Local
from py7zr.properties import COMMAND_HELP_STRING as COMMAND_HELP_STRING
from typing import Any, Optional

class CliExtractCallback(ExtractCallback):
ofd: Any = ...
archive_total: Any = ...
total_bytes: int = ...
pwidth: int = ...
def __init__(self, total_bytes: Any, ofd: Any = ...) -> None: ...
def report_start_preparation(self) -> None: ...
def report_start(self, processing_file_path: Any, processing_bytes: Any) -> None: ...
def report_end(self, processing_file_path: Any, wrote_bytes: Any) -> None: ...
def report_postprocess(self) -> None: ...
def report_warning(self, message: Any) -> None: ...

class Cli:
dunits: Any = ...
parser: Any = ...
unit_pattern: Any = ...
def __init__(self) -> None: ...
def run(self, arg: Optional[Any]=...) -> int: ...
def show_version(self) -> None: ...
def show_help(self, args: Any): ...
def run_info(self, args: Any) -> None: ...
def run_list(self, args: Any): ...
@staticmethod
def print_archiveinfo(archive: Any, file: Any) -> None: ...
def run_test(self, args: Any): ...
def run_extract(self, args: argparse.Namespace) -> int: ...
def run_create(self, args: Any): ...
def run_append(self, args: Any): ...
20 changes: 10 additions & 10 deletions py7zr/compressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,19 +431,19 @@ def flush(self):
return self.encoder.flush()


class BrotliCompressor:
class BrotliCompressor(ISevenZipCompressor):
def __init__(self, level):
self._compressor = brotli.Compressor(quality=level)

def compress(self, data):
def compress(self, data: Union[bytes, bytearray, memoryview]) -> bytes:
return self._compressor.process(data)

def flush(self):
def flush(self) -> bytes:
return self._compressor.flush()


class BrotliDecompressor:
def __init__(self, properties, block_size):
def __init__(self, properties: bytes, block_size: int):
if len(properties) != 3 or (properties[0], properties[1]) > (brotli_major, brotli_minor):
raise UnsupportedCompressionMethodError
self._decompressor = brotli.Decompressor()
Expand All @@ -461,23 +461,23 @@ def _decompress1(self, data):


class ZstdCompressor:
def __init__(self, level):
def __init__(self, level: int):
self.compressor = pyzstd.ZstdCompressor(level)

def compress(self, data):
def compress(self, data: Union[bytes, bytearray, memoryview]) -> bytes:
return self.compressor.compress(data)

def flush(self):
def flush(self) -> bytes:
return self.compressor.flush()


class ZstdDecompressor:
def __init__(self, properties, blocksize):
def __init__(self, properties: bytes, blocksize: int):
if len(properties) not in [3, 5] or (properties[0], properties[1], 0) > pyzstd.zstd_version_info:
raise UnsupportedCompressionMethodError
self.decompressor = pyzstd.ZstdDecompressor()

def decompress(self, data):
def decompress(self, data: Union[bytes, bytearray, memoryview]) -> bytes:
return self.decompressor.decompress(data)


Expand All @@ -502,7 +502,7 @@ def __init__(self, filters, unpacksize):
self._decompressor = lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=filters)
self.unpacksize = unpacksize

def decompress(self, data, max_length):
def decompress(self, data: Union[bytes, bytearray, memoryview], max_length: int) -> bytes:
return self._decompressor.decompress(data, max_length)


Expand Down
Loading

0 comments on commit 51ad874

Please sign in to comment.