From 1080a965e4dabf73256df72622692094b41a64f9 Mon Sep 17 00:00:00 2001 From: Vignesh Rao Date: Tue, 31 Dec 2024 20:42:38 -0600 Subject: [PATCH] Release `v0.1.0` Onboard pre-commit linter --- .pre-commit-config.yaml | 44 +++++++++++++++++++++++++++++++++++++ phydisk/__init__.py | 48 +++++++++++++++++++++++++++++++++-------- phydisk/macOS.py | 4 +--- phydisk/models.py | 12 +++++------ phydisk/windows.py | 4 +++- 5 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..a7eac4a --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,44 @@ +fail_fast: true +exclude: ^(notebooks/|scripts/|.github/|docs/) +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: check-added-large-files + - id: check-ast + - id: check-builtin-literals + - id: check-case-conflict + - id: check-docstring-first + - id: check-merge-conflict + - id: check-shebang-scripts-are-executable + - id: check-toml + - id: check-vcs-permalinks + - id: check-yaml + - id: debug-statements + - id: detect-private-key + - id: end-of-file-fixer + - id: fix-byte-order-marker + - id: mixed-line-ending + - id: sort-simple-yaml + - id: trailing-whitespace + + - repo: https://github.com/psf/black + rev: 22.3.0 + hooks: + - id: black + exclude: docs_gen/ + + - repo: https://github.com/PyCQA/flake8 + rev: 7.1.0 + hooks: + - id: flake8 + additional_dependencies: + - flake8-docstrings + - flake8-sfs + args: [ --max-line-length=120, --extend-ignore=SFS3 D107 SFS301 D100 D104 D401 SFS101 SFS201 D412 E731 ] + + - repo: https://github.com/PyCQA/isort + rev: 5.13.2 + hooks: + - id: isort + args: [ --profile, black ] diff --git a/phydisk/__init__.py b/phydisk/__init__.py index d022fbb..25518fc 100644 --- a/phydisk/__init__.py +++ b/phydisk/__init__.py @@ -2,28 +2,42 @@ import logging import os import sys +import time from typing import Dict, List -from . import linux, macOS, windows, models +from . import linux, macOS, models, windows -version = "0.0.0-a1" +version = "0.1.0" LOGGER = logging.getLogger(__name__) -def get_disk_lib(user_input: str | os.PathLike): +def get_disk_lib(user_input: str | os.PathLike) -> str: + """Get the disk library for the appropriate OS. + + Args: + user_input: Disk library input by user. + """ disk_lib = ( - user_input or - os.environ.get("disk_lib") or - os.environ.get("DISK_LIB") or - models.default_disk_lib()[models.OPERATING_SYSTEM] + user_input + or os.environ.get("disk_lib") + or os.environ.get("DISK_LIB") + or models.default_disk_lib()[models.OPERATING_SYSTEM] ) assert os.path.isfile(disk_lib), f"Disk library {disk_lib!r} doesn't exist" return disk_lib def get_all_disks(disk_lib: str | os.PathLike = None) -> List[Dict[str, str]]: - """OS-agnostic function to get all disks connected to the host system.""" + """OS-agnostic function to get all disks connected to the host system. + + Args: + disk_lib: Custom disk library path. + + Returns: + List[Dict[str, str]]: + Returns a list of disk information. + """ os_map = { models.OperatingSystem.darwin: macOS.drive_info, models.OperatingSystem.linux: linux.drive_info, @@ -34,6 +48,7 @@ def get_all_disks(disk_lib: str | os.PathLike = None) -> List[Dict[str, str]]: return os_map[models.OperatingSystem(models.OPERATING_SYSTEM)](disk_lib) except Exception as error: LOGGER.error(error) + return [] def commandline() -> None: @@ -44,6 +59,7 @@ def commandline() -> None: - ``--help | -H``: Prints the help section. - ``print``: Prints the disk information in terminal. - ``save``: Saves the disk information into a JSON file. + - ``--filename``: Filename to store the disk information. """ assert sys.argv[0].lower().endswith("phydisk"), "Invalid commandline trigger!!" @@ -51,12 +67,26 @@ def commandline() -> None: get_help = "--help" in sys.argv or "-H" in sys.argv print_info = "print" in sys.argv save_info = "save" in sys.argv + filename = None + custom_filename = "--filename" in sys.argv + if custom_filename: + filename_idx = sys.argv.index("--filename") + 1 + try: + filename = sys.argv[filename_idx] + assert filename.endswith(".json") + except IndexError: + print("ERROR:\n\t--filename argument requires a value") + sys.exit(1) + except AssertionError: + print("ERROR:\n\tfilename must be JSON") + sys.exit(1) options = { "--version | -V": "Prints the version.", "--help | -H": "Prints the help section.", "print": "Prints the disk information in the terminal.", "save": "Saves the disk information into a JSON file.", + "--filename": "Filename to store the disk information.", } # weird way to increase spacing to keep all values monotonic _longest_key = len(max(options.keys())) @@ -75,7 +105,7 @@ def commandline() -> None: print(disk) sys.exit(0) elif save_info: - filename = "disk_info.json" + filename = filename or f"PhyDisk_{int(time.time())}.json" with open(filename, "w") as file: json.dump(get_all_disks(), file, indent=2) print(f"Physical disks' information has been stored in {filename!r}") diff --git a/phydisk/macOS.py b/phydisk/macOS.py index 92f02e5..f706a84 100644 --- a/phydisk/macOS.py +++ b/phydisk/macOS.py @@ -87,9 +87,7 @@ def drive_info(disk_lib: str | os.PathLike) -> List[Dict[str, str]]: List[Dict[str, str]]: Returns disks information for macOS devices. """ - result = subprocess.run( - [disk_lib, "info", "-all"], capture_output=True, text=True - ) + result = subprocess.run([disk_lib, "info", "-all"], capture_output=True, text=True) disks = parse_diskutil_output(result.stdout) device_ids = defaultdict(list) physical_disks = [] diff --git a/phydisk/models.py b/phydisk/models.py index d681588..c5de14e 100644 --- a/phydisk/models.py +++ b/phydisk/models.py @@ -5,9 +5,9 @@ except ImportError: from enum import Enum - class StrEnum(str, Enum): - """Custom StrEnum object for python3.10""" + """Custom StrEnum object for python3.10.""" + OPERATING_SYSTEM = platform.system().lower() @@ -25,9 +25,9 @@ class OperatingSystem(StrEnum): if OPERATING_SYSTEM not in ( - OperatingSystem.linux, - OperatingSystem.darwin, - OperatingSystem.windows, + OperatingSystem.linux, + OperatingSystem.darwin, + OperatingSystem.windows, ): raise RuntimeError( f"{OPERATING_SYSTEM!r} is unsupported.\n\t" @@ -40,5 +40,5 @@ def default_disk_lib(): return dict( linux="/usr/bin/lsblk", darwin="/usr/sbin/diskutil", - windows="C:\\Program Files\\PowerShell\\7\\pwsh.exe" + windows="C:\\Program Files\\PowerShell\\7\\pwsh.exe", ) diff --git a/phydisk/windows.py b/phydisk/windows.py index 78727e6..e854c99 100644 --- a/phydisk/windows.py +++ b/phydisk/windows.py @@ -62,7 +62,9 @@ def clean_ansi_escape_sequences(text: str) -> str: return ansi_escape.sub("", text) -def get_physical_disks_and_partitions(disk_lib: str | os.PathLike) -> List[Tuple[str, str, str]]: +def get_physical_disks_and_partitions( + disk_lib: str | os.PathLike, +) -> List[Tuple[str, str, str]]: """Powershell Core command to get physical disks and their partitions with drive letters (mount points). Returns: