Skip to content

Utils

ghflowgen.utils

Contain the utility functions.

ghflowgen.utils.export

Contain utility functions to export data to JSON format.

ghflowgen.utils.export.generate_unique_tmp_path

generate_unique_tmp_path(path: Path) -> Path

Return a unique temporary path given a path.

This function updates the name to add a UUID.

Parameters:

Name Type Description Default
path Path

The input path.

required

Returns:

Type Description
Path

The unique name.

Example usage:

>>> import tempfile
>>> from pathlib import Path
>>> from ghflowgen.utils.export import generate_unique_tmp_path
>>> with tempfile.TemporaryDirectory() as tmpdir:
...     path = generate_unique_tmp_path(Path(tmpdir).joinpath("data.json"))
...     path
...
PosixPath('/.../data-....json')

ghflowgen.utils.export.load_json

load_json(path: Path) -> Any

Load the data from a given JSON file.

Parameters:

Name Type Description Default
path Path

The path to the JSON file.

required

Returns:

Type Description
Any

The data from the JSON file.

Example usage:

>>> import tempfile
>>> from pathlib import Path
>>> from ghflowgen.utils.export import save_json, load_json
>>> with tempfile.TemporaryDirectory() as tmpdir:
...     path = Path(tmpdir).joinpath("data.json")
...     save_json({"key1": [1, 2, 3], "key2": "abc"}, path)
...     data = load_json(path)
...     data
...
{'key1': [1, 2, 3], 'key2': 'abc'}

ghflowgen.utils.export.save_json

save_json(
    to_save: Any, path: Path, *, exist_ok: bool = False
) -> None

Save the given data in a JSON file.

Parameters:

Name Type Description Default
to_save Any

The data to write in a JSON file.

required
path Path

The path where to write the JSON file.

required
exist_ok bool

If exist_ok is False (the default), FileExistsError is raised if the target file already exists. If exist_ok is True, FileExistsError will not be raised unless the given path already exists in the file system and is not a file.

False

Raises:

Type Description
FileExistsError

if the file already exists.

Example usage:

>>> import tempfile
>>> from pathlib import Path
>>> from ghflowgen.utils.export import save_json
>>> with tempfile.TemporaryDirectory() as tmpdir:
...     path = Path(tmpdir).joinpath("data.json")
...     save_json({"key1": [1, 2, 3], "key2": "abc"}, path)
...     data = load_json(path)
...     data
...
{'key1': [1, 2, 3], 'key2': 'abc'}

ghflowgen.utils.pypi

Contain PyPI utility functions.

ghflowgen.utils.pypi.get_pypi_versions cached

get_pypi_versions(
    package: str, reverse: bool = False
) -> tuple[str, ...]

Get the package versions available on PyPI.

The package versions are read from PyPI.

Parameters:

Name Type Description Default
package str

The package name.

required
reverse bool

If False, sort in ascending order; if True, sort in descending order.

False

Returns:

Type Description
tuple[str, ...]

A list containing the version strings.

Example usage:

>>> from ghflowgen.utils.pypi import get_pypi_versions
>>> versions = get_pypi_versions("requests")  # doctest: +SKIP