mirror of
https://github.com/scito/extract_otp_secret_keys.git
synced 2025-12-13 10:06:36 +01:00
add type hints (Python 3.11)
This commit is contained in:
parent
f933cd0d32
commit
201e6510f8
10 changed files with 272 additions and 135 deletions
43
utils.py
43
utils.py
|
|
@ -21,23 +21,26 @@ import os
|
|||
import re
|
||||
import shutil
|
||||
import sys
|
||||
import pathlib
|
||||
from typing import BinaryIO, Any
|
||||
|
||||
|
||||
# Ref. https://stackoverflow.com/a/16571630
|
||||
class Capturing(list):
|
||||
class Capturing(list[Any]):
|
||||
'''Capture stdout and stderr
|
||||
Usage:
|
||||
with Capturing() as output:
|
||||
print("Output")
|
||||
'''
|
||||
def __enter__(self):
|
||||
# TODO remove type ignore when fixed, see https://github.com/python/mypy/issues/11871, https://stackoverflow.com/questions/72174409/type-hinting-the-return-value-of-a-class-method-that-returns-self
|
||||
def __enter__(self): # type: ignore
|
||||
self._stdout = sys.stdout
|
||||
sys.stdout = self._stringio_std = io.StringIO()
|
||||
self._stderr = sys.stderr
|
||||
sys.stderr = self._stringio_err = io.StringIO()
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
def __exit__(self, *args: Any) -> None:
|
||||
self.extend(self._stringio_std.getvalue().splitlines())
|
||||
del self._stringio_std # free up some memory
|
||||
sys.stdout = self._stdout
|
||||
|
|
@ -47,71 +50,71 @@ with Capturing() as output:
|
|||
sys.stderr = self._stderr
|
||||
|
||||
|
||||
def file_exits(file):
|
||||
def file_exits(file: str | pathlib.Path) -> bool:
|
||||
return os.path.isfile(file)
|
||||
|
||||
|
||||
def remove_file(file):
|
||||
def remove_file(file: str | pathlib.Path) -> None:
|
||||
if file_exits(file): os.remove(file)
|
||||
|
||||
|
||||
def remove_files(glob_pattern):
|
||||
def remove_files(glob_pattern: str) -> None:
|
||||
for f in glob.glob(glob_pattern):
|
||||
os.remove(f)
|
||||
|
||||
|
||||
def remove_dir_with_files(dir):
|
||||
def remove_dir_with_files(dir: str | pathlib.Path) -> None:
|
||||
if os.path.exists(dir): shutil.rmtree(dir)
|
||||
|
||||
|
||||
def read_csv(filename):
|
||||
def read_csv(filename: str) -> list[list[str]]:
|
||||
"""Returns a list of lines."""
|
||||
with open(filename, "r", encoding="utf-8", newline='') as infile:
|
||||
lines = []
|
||||
lines: list[list[str]] = []
|
||||
reader = csv.reader(infile)
|
||||
for line in reader:
|
||||
lines.append(line)
|
||||
return lines
|
||||
|
||||
|
||||
def read_csv_str(str):
|
||||
def read_csv_str(data_str: str) -> list[list[str]]:
|
||||
"""Returns a list of lines."""
|
||||
lines = []
|
||||
reader = csv.reader(str.splitlines())
|
||||
lines: list[list[str]] = []
|
||||
reader = csv.reader(data_str.splitlines())
|
||||
for line in reader:
|
||||
lines.append(line)
|
||||
return lines
|
||||
|
||||
|
||||
def read_json(filename):
|
||||
def read_json(filename: str) -> Any:
|
||||
"""Returns a list or a dictionary."""
|
||||
with open(filename, "r", encoding="utf-8") as infile:
|
||||
return json.load(infile)
|
||||
|
||||
|
||||
def read_json_str(str):
|
||||
def read_json_str(data_str: str) -> Any:
|
||||
"""Returns a list or a dictionary."""
|
||||
return json.loads(str)
|
||||
return json.loads(data_str)
|
||||
|
||||
|
||||
def read_file_to_list(filename):
|
||||
def read_file_to_list(filename: str) -> list[str]:
|
||||
"""Returns a list of lines."""
|
||||
with open(filename, "r", encoding="utf-8") as infile:
|
||||
return infile.readlines()
|
||||
|
||||
|
||||
def read_file_to_str(filename):
|
||||
def read_file_to_str(filename: str) -> str:
|
||||
"""Returns a str."""
|
||||
return "".join(read_file_to_list(filename))
|
||||
|
||||
|
||||
def read_binary_file_as_stream(filename):
|
||||
def read_binary_file_as_stream(filename: str) -> BinaryIO:
|
||||
"""Returns binary file content."""
|
||||
with open(filename, "rb",) as infile:
|
||||
return io.BytesIO(infile.read())
|
||||
|
||||
|
||||
def replace_escaped_octal_utf8_bytes_with_str(str):
|
||||
def replace_escaped_octal_utf8_bytes_with_str(str: str) -> str:
|
||||
encoded_name_strings = re.findall(r'name: .*$', str, flags=re.MULTILINE)
|
||||
for encoded_name_string in encoded_name_strings:
|
||||
escaped_bytes = re.findall(r'((?:\\[0-9]+)+)', encoded_name_string)
|
||||
|
|
@ -122,5 +125,5 @@ def replace_escaped_octal_utf8_bytes_with_str(str):
|
|||
return str
|
||||
|
||||
|
||||
def quick_and_dirty_workaround_encoding_problem(str):
|
||||
def quick_and_dirty_workaround_encoding_problem(str: str) -> str:
|
||||
return re.sub(r'name: "encoding: .*$', '', str, flags=re.MULTILINE)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue