From cfe76ca4970e491f19b5fcad9d882d9c3af33698 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Mon, 27 Jun 2022 04:24:41 +0200 Subject: [PATCH] Type hinting --- modules/agnostic_paths.py | 38 +++++++++++++++----------------------- modules/argument_parser.py | 2 +- modules/test_adler.py | 8 +++----- modules/test_line_count.py | 4 ++-- modules/test_sha.py | 6 +++--- modules/test_size.py | 4 ++-- 6 files changed, 26 insertions(+), 36 deletions(-) diff --git a/modules/agnostic_paths.py b/modules/agnostic_paths.py index dbdde00..f00e945 100644 --- a/modules/agnostic_paths.py +++ b/modules/agnostic_paths.py @@ -5,6 +5,9 @@ import os import re import sys import random +import argparse + +from typing import List class AgnosticPaths: @@ -12,10 +15,9 @@ class AgnosticPaths: An agnostic path object, containing all methods to access the files and folders in a standard discord install """ - def __init__(self, args): + def __init__(self, args: argparse.Namespace): """ :param args: The arguments passed to the script - :type args: argparse.Namespace """ self.args = args self.ptb = args.ptb @@ -32,13 +34,12 @@ class AgnosticPaths: random.shuffle(self.all_files) self.size = len(self.all_files) - def get_os(self): + def get_os(self) -> str: """ Get the os string in a short, spydey-detector way. For now only windows and linux are supported, note that due to the nature of linux builds, strings will likely become linux-arch-deb, linux-arch-snap etc... :return: ("windows", "linux", "mac") - :rtype: str """ platform = sys.platform if platform.startswith('win'): @@ -54,12 +55,11 @@ class AgnosticPaths: raise OSError("This script can only check for Spideys on Windows, Linux and Mac.") return self.os - def _get_path(self): + def _get_path(self) -> str: """ Return the base path of the install, note that the self.main_path is not returned, and this method is meant to be used at init only :return: Base path of the install (minus the version on Windows) - :rtype: str """ if self.force_path is not None: return self.force_path @@ -88,12 +88,11 @@ class AgnosticPaths: return "" raise OSError("Cannot detect discord PATH") - def get_version(self): + def get_version(self) -> str: """ Looks for the version file in the discord folder and return the latest version of the discord build :return: The version build - :rtype: str """ versions = [] if self.os == 'windows': @@ -119,7 +118,7 @@ class AgnosticPaths: if self.os == 'windows': self.main_path = os.path.join(self.main_path, self.version) - def list(self, *args): + def list(self, *args: str) -> List[float]: """ Lists all files in the path /discord_path/args[0]/args[1].../args[n] :param args: The path as a list of parameters list("folder1", "folder2", ..., "foldern") @@ -129,7 +128,7 @@ class AgnosticPaths: """ return os.listdir(self(*args)) - def list_dir_files(self, *args): + def list_dir_files(self, *args: str) -> List[str]: """ Lists all files and directories in the given path. :param args: The path @@ -145,13 +144,11 @@ class AgnosticPaths: files.append(element) return dirs, files - def __call__(self, *args): + def __call__(self, *args: str) -> str: """ Tries to open the path /discord_path/args[0]/args[1].../args[n] :param args: The path of the file or directory as a list of parameters __call__("folder1", "folder2", ..., "foldern") - :type args: str :return: The path is it exists - :rtype: str :raises: FileNotFoundError if the path doesn't exists """ _path = os.path.join(self.main_path, *args) @@ -159,44 +156,39 @@ class AgnosticPaths: raise FileNotFoundError(f"Could not find {_path}") return _path - def get_path(self): + def get_path(self) -> str: """ The main path of the discord install :return: self.main_path - :rtype: str """ return self.main_path - def walk_all_files(self, *args): + def walk_all_files(self, *args : str) -> List[str]: """ Walks all files in the given path :param args: The path :return: A list of all files in the given path - :rtype: list[str] """ return [os.path.join(dirpath, filename) for dirpath, dirnames, filenames in os.walk(self(*args)) for filename in filenames] - def get_short_path(self, path): + def get_short_path(self, path: str) -> str: """ Get the short path of the given path :param path: The path :return: The short path - :rtype: str """ return os.path.relpath(path, self.main_path) - def __repr__(self): + def __repr__(self) -> str: """ :return: A representation of the AgnosticPath - :rtype: str """ return f"AgnosticPaths(ptb={self.ptb}, os={self.os}, main_path={self.main_path}, version={self.version})" - def __str__(self): + def __str__(self) -> str: """ :return: The main path of the discord install - :rtype: str """ return self.main_path diff --git a/modules/argument_parser.py b/modules/argument_parser.py index d9380af..fef97c5 100644 --- a/modules/argument_parser.py +++ b/modules/argument_parser.py @@ -1,7 +1,7 @@ import argparse -def parse(_args): +def parse(_args: list) -> argparse.Namespace: parser = argparse.ArgumentParser( prog="Spidey Detector", description='Checks for corrupt discord install' diff --git a/modules/test_adler.py b/modules/test_adler.py index 2ef4431..8612aa0 100644 --- a/modules/test_adler.py +++ b/modules/test_adler.py @@ -2,24 +2,22 @@ Fast adler32 checksum implementation. """ -from modules import test_template +from modules import test_template, internal_io from zlib import adler32 -def apply_adler(path): +def apply_adler(path: str) -> hex: """ Apply the adler32 checksum to a file. :param path: The path to the file. - :type path: str :return: The adler32 checksum. - :rtype: int """ with open(path, "rb") as f: return hex(adler32(f.read())) class Adler32(test_template.TestWalkTemplateSimpleFunction): - def __init__(self, thread_parameters): + def __init__(self, thread_parameters: internal_io.thread_parameters): """ Simple test runner, compare adler32 to known good values. """ diff --git a/modules/test_line_count.py b/modules/test_line_count.py index e79808e..335d3de 100644 --- a/modules/test_line_count.py +++ b/modules/test_line_count.py @@ -2,11 +2,11 @@ Test against the line count of the files in the discord folder. """ -from modules import test_template +from modules import test_template, internal_io class LineCount(test_template.TestWalkTemplateSimpleFunction): - def __init__(self, thread_parameters): + def __init__(self, thread_parameters: internal_io.thread_parameters): """ Simple test runner, compare line count to known good values. """ diff --git a/modules/test_sha.py b/modules/test_sha.py index a265a54..efcc3b3 100644 --- a/modules/test_sha.py +++ b/modules/test_sha.py @@ -3,10 +3,10 @@ Sha256 hash function. """ from hashlib import sha256 -from modules import test_template +from modules import test_template, internal_io -def sha256_hash(path): +def sha256_hash(path: str) -> hex: """ Returns the sha256 hash of the data. """ @@ -18,7 +18,7 @@ class Sha256(test_template.TestWalkTemplateSimpleFunction): """ Sha256 hash function. """ - def __init__(self, thread_parameters): + def __init__(self, thread_parameters: internal_io.thread_parameters): """ Sha256 hash function. """ diff --git a/modules/test_size.py b/modules/test_size.py index 4b70887..f9934f5 100644 --- a/modules/test_size.py +++ b/modules/test_size.py @@ -1,10 +1,10 @@ import os -from modules import test_template +from modules import test_template, internal_io class SizeCheck(test_template.TestWalkTemplateSimpleFunction): - def __init__(self, thread_parameters): + def __init__(self, thread_parameters: internal_io.thread_parameters): """ Simple test runner, compare file size to known good values. """