Skip to content

Helpers

Helper functions for file and date processing.

Classes:

Name Description
SizeUnit

Enumeration for size units.

Functions:

Name Description
iter_files

Recursively iterate over files in the root directory.

process_aliases

Process aliases to extract individual aliases.

sort_dict_by_value

Sort a dictionary by its values.

yield_attrs

Collect slotted attributes from an object.

process_pattern_hierarchy

Process a pattern hierarchy to create a mapping of patterns to their respective values.

iter_pattern_split

Emulate re.Pattern.split() but yields sections of text instead of returning a list.

get_count_and_foundin_data

Update the result dictionary with counts and file occurrences.

extract_builtin_properties

Get built-in properties.

remove_builtin_properties

Get properties that are not built-in.

format_bytes

Convert a byte value into a human-readable string using SI or IEC units.

yield_asset_paths

Yield the file paths of unlinked assets.

yield_bak_rec_paths

Yield the file paths of bak and recycle directories.

process_moves

Process the moving of files to a specified directory.

SizeUnit

Bases: StrEnum

Enumeration for size units.

iter_files(root_dir: Path, target_dirs: set[str]) -> Generator[Path]

Recursively iterate over files in the root directory.

process_aliases(aliases: str) -> Generator[str]

Process aliases to extract individual aliases.

sort_dict_by_value(data: dict, value: str = '', *, reverse: bool = False) -> dict

Sort a dictionary by its values.

yield_attrs(obj: object) -> Generator[tuple[str, Any]]

Collect slotted attributes from an object.

process_pattern_hierarchy(content: str, pattern_mod: ModuleType) -> Generator[tuple[str, str]]

Process a pattern hierarchy to create a mapping of patterns to their respective values.

Parameters:

Name Type Description Default
content str

The content to process.

required
pattern_mod ModuleType

A module containing regex patterns and their corresponding criteria.

required

Yields:

Type Description
Generator[tuple[str, str]]

Generator[tuple[str, str], None, None]: A generator yielding key-value pairs of patterns and their values.

iter_pattern_split(pattern: re.Pattern, text: str, maxsplit: int = 0) -> Generator[tuple[int, str]]

Emulate re.Pattern.split() but yields sections of text instead of returning a list.

Iterate over sections of text separated by bullet markers.

Parameters:

Name Type Description Default
pattern Pattern

The regex pattern to match bullet markers.

required
text str

The text to split into sections.

required
maxsplit int

Maximum number of splits. If 0, all sections are returned.

0

Yields:

Type Description
Generator[tuple[int, str]]

Generator[tuple[int, str], None, None]: Sections of text with their respective indices.

get_count_and_foundin_data(result: dict, collection: list[str], filename: str) -> dict

Update the result dictionary with counts and file occurrences.

Parameters:

Name Type Description Default
result dict

The dictionary to update with counts and file occurrences.

required
collection list[str]

The collection of items to count.

required
filename str

The name of the file containing the path information.

required

Returns:

Name Type Description
dict dict

The updated result dictionary with counts and file occurrences.

extract_builtin_properties(properties: set[str]) -> set[str]

Get built-in properties.

remove_builtin_properties(properties: set[str]) -> set[str]

Get properties that are not built-in.

format_bytes(size_bytes: int, system: str = SizeUnit.SI, precision: int = 2) -> str

Convert a byte value into a human-readable string using SI or IEC units.

Parameters:

Name Type Description Default
size_bytes int

Number of bytes.

required
system str

'si' for powers of 1000, 'iec' for powers of 1024.

SI
precision int

Number of decimal places.

2

Returns:

Name Type Description
str str

Human-readable string, e.g. '1.23 MB' or '1.20 MiB'.

yield_asset_paths(unlinked_assets: set[LogseqFile]) -> Generator[Path]

Yield the file paths of unlinked assets.

yield_bak_rec_paths(source_dir: Path) -> Generator[Path]

Yield the file paths of bak and recycle directories.

process_moves(target_dir: Path, paths: Generator[Path], *, move: bool) -> list[str]

Process the moving of files to a specified directory.

Parameters:

Name Type Description Default
target_dir Path

The directory to move files to.

required
paths Generator[Path, None, None]

A generator yielding file paths to move.

required
move bool

If True, move the files. If False, simulate the move.

required

Returns:

Type Description
list[str]

list[str]: A list of names of the moved files/folders.