Skip to content

Commit

Permalink
Release v0.1.0
Browse files Browse the repository at this point in the history
Onboard pre-commit linter
  • Loading branch information
dormant-user committed Jan 1, 2025
1 parent 35977fd commit 1080a96
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 19 deletions.
44 changes: 44 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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 ]
48 changes: 39 additions & 9 deletions phydisk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -44,19 +59,34 @@ 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!!"

print_ver = "--version" in sys.argv or "-V" in sys.argv
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()))
Expand All @@ -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}")
Expand Down
4 changes: 1 addition & 3 deletions phydisk/macOS.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
12 changes: 6 additions & 6 deletions phydisk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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"
Expand All @@ -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",
)
4 changes: 3 additions & 1 deletion phydisk/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1080a96

Please sign in to comment.