Skip to content

Commit

Permalink
Merge pull request #10 from pharmaverse/shorthand
Browse files Browse the repository at this point in the history
Use shorthand syntax for unions
  • Loading branch information
nanxstats authored Jan 12, 2025
2 parents a71c40b + 7d5a4b7 commit cc45366
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/pkglite/classify.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from typing import FrozenSet, Optional
from typing import FrozenSet


def is_text_file(path: str, n: Optional[int] = None) -> bool:
def is_text_file(path: str, n: int | None = None) -> bool:
"""
Classify any file as text or binary.
Expand Down
10 changes: 5 additions & 5 deletions src/pkglite/pack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from pathlib import Path
from typing import List, Callable, Tuple, Optional
from typing import Callable

from pathspec import PathSpec

Expand Down Expand Up @@ -142,7 +142,7 @@ def process_single_file(
directory: str,
package_name: str,
ignore_matcher: Callable[[str], bool],
) -> Optional[str]:
) -> str | None:
"""
Process a single file and return its formatted content.
Expand All @@ -153,7 +153,7 @@ def process_single_file(
ignore_matcher (function): Function to check if file should be ignored.
Returns:
Optional[str]: Formatted file content if not ignored, None otherwise.
str | None: Formatted file content if not ignored, None otherwise.
"""
if ignore_matcher(file_path):
return None
Expand Down Expand Up @@ -182,15 +182,15 @@ def create_header() -> str:


def pack(
input_dirs: str | List[str] | Path,
input_dirs: str | Path | list[str | Path],
output_file: str | Path = Path("pkglite.txt"),
quiet: bool = False,
) -> None:
"""
Pack files from one or multiple directories into a text file.
Args:
input_dirs (str or list or Path): Path or list of paths to the
input_dirs (str or Path or list): Path or list of paths to the
directories to pack.
output_file (str or Path): Path to the output file.
Default is 'pkglite.txt'.
Expand Down
18 changes: 14 additions & 4 deletions src/pkglite/unpack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import binascii
from typing import List, Dict, Set, Optional
from typing import List, Dict, Set
from pathlib import Path
from dataclasses import dataclass

Expand All @@ -21,7 +21,7 @@ class FileData:
content: str


def extract_metadata_field(line: str, tag: str) -> Optional[str]:
def extract_metadata_field(line: str, tag: str) -> str | None:
"""
Extract a metadata field value from a line with a given tag.
Expand All @@ -30,7 +30,7 @@ def extract_metadata_field(line: str, tag: str) -> Optional[str]:
tag (str): The tag to look for.
Returns:
Optional[str]: The extracted value if found, None otherwise.
str | None: The extracted value if found, None otherwise.
"""
return line.split(f"{tag}: ")[1] if line.startswith(f"{tag}: ") else None

Expand Down Expand Up @@ -81,7 +81,17 @@ def parse_packed_file(input_file: str) -> List[FileData]:

def process_file_entry(
current: Dict[str, str], lines: List[str]
) -> Optional[FileData]:
) -> FileData | None:
"""
Process a file entry and create a FileData object.
Args:
current: Dictionary containing current file metadata.
lines: List of content lines.
Returns:
FileData | None: FileData object if valid entry, None otherwise.
"""
if not (current and "package" in current and "path" in current):
return None
content = create_file_entry(
Expand Down
8 changes: 3 additions & 5 deletions src/pkglite/use.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import shutil
import importlib.resources as pkg_resources
from pathlib import Path
from typing import List, Union, Tuple
from typing import List, Tuple

import pkglite.templates
from .cli import print_success, print_warning, format_path


def process_directory(
template: Path, directory: Union[str, Path], force: bool, quiet: bool
template: Path, directory: str | Path, force: bool, quiet: bool
) -> Tuple[str, bool]:
"""
Process a single directory and create/overwrite `.pkgliteignore` file.
Expand Down Expand Up @@ -50,9 +50,7 @@ def process_directory(


def use_pkglite(
input_dirs: Union[str, Path, List[Union[str, Path]]],
force: bool = False,
quiet: bool = False,
input_dirs: str | Path | list[str | Path], force: bool = False, quiet: bool = False
) -> List[str]:
"""
Copy the `.pkgliteignore` template into one or more directories.
Expand Down

0 comments on commit cc45366

Please sign in to comment.