Skip to content

Commit

Permalink
Wrap click.secho to predefined functions - better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
befeleme committed Dec 20, 2024
1 parent a48d6c0 commit 22931b5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
5 changes: 3 additions & 2 deletions pyp2spec/conf2spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from pyp2spec.rpmversion import RpmVersion
from pyp2spec.utils import Pyp2specError, normalize_as_wheel_name
from pyp2spec.utils import warn, yay


TEMPLATE_FILENAME = "template.spec"
Expand Down Expand Up @@ -204,7 +205,7 @@ def save_spec_file(config, output):
output = config.get_string("python_name") + ".spec"
with open(output, "w", encoding="utf-8") as spec_file:
spec_file.write(result)
click.secho(f"Spec file was saved successfully to '{output}'")
yay(f"Spec file was saved successfully to '{output}'")
return output


Expand All @@ -225,7 +226,7 @@ def main(config, spec_output):
try:
create_spec_file(config, spec_output)
except (Pyp2specError, NotImplementedError) as exc:
click.secho(f"Fatal exception occurred: {exc}", fg="red")
warn(f"Fatal exception occurred: {exc}")
sys.exit(1)

if __name__ == "__main__":
Expand Down
23 changes: 12 additions & 11 deletions pyp2spec/pyp2conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pyp2spec.utils import Pyp2specError, normalize_name, extras, summary
from pyp2spec.utils import prepend_name_with_python, archive_name
from pyp2spec.utils import is_archful, find_project_url
from pyp2spec.utils import warn, caution, inform, yay
from pyp2spec.pypi_loaders import load_from_pypi, load_core_metadata_from_pypi, CoreMetadataNotFoundError
from pyp2spec.pypi_loaders import extract_common_fields

Expand Down Expand Up @@ -78,30 +79,30 @@ def create_config_contents(
pkg_info["python_name"] = prepend_name_with_python(pkg_info["pypi_name"], python_alt_version)

if python_alt_version is not None:
click.secho(f"Assuming build for Python: {python_alt_version}", fg="yellow")
inform(f"Assuming build for Python: {python_alt_version}")
pkg_info["python_alt_version"] = python_alt_version

if version is None:
click.secho(f"Assuming the latest version found on PyPI: '{pkg_info["pypi_version"]}'", fg="yellow")
inform(f"Assuming the latest version found on PyPI: '{pkg_info["pypi_version"]}'")

if pkg_info["archful"]:
click.secho("Package contains compiled extensions - you may need to specify additional build requirements", fg="magenta")
caution("Package contains compiled extensions - you may need to specify additional build requirements")

if pkg_info["license"] is None:
click.secho("WARNING: No valid license found. Inspect the project manually to find the license", fg="red")
warn("WARNING: No valid license found. Inspect the project manually to find the license")
# TOML can't handle None value; we don't need this key explicitly
del pkg_info["license"]
else:
if compliant:
is_compliant, results = check_compliance(pkg_info["license"], session=session)
if not is_compliant:
click.secho(f"The license '{pkg_info['license']}' is not compliant with Fedora, discarding", fg="red")
warn(f"The license '{pkg_info['license']}' is not compliant with Fedora")
if results["bad"]:
err_string = "Found identifiers: '{0}' aren't allowed in Fedora."
click.secho(err_string.format(", ".join(results["bad"])), fg="red")
info_str = "Found identifiers: '{0}' aren't allowed in Fedora."
warn(info_str.format(", ".join(results["bad"])))
if results["good"]:
err_string = "Found identifiers: '{0}' are good for Fedora."
click.secho(err_string.format(", ".join(results["good"])), fg="yellow")
info_str = "Found identifiers: '{0}' are good for Fedora."
inform(info_str.format(", ".join(results["good"])))

if automode:
pkg_info["automode"] = True
Expand All @@ -123,7 +124,7 @@ def save_config(contents, output=None):
output = f"{package}.conf"
with open(output, "wb") as f:
tomli_w.dump(contents, f, multiline_strings=True)
click.secho(f"Configuration file was saved successfully to '{output}'")
yay(f"Configuration file was saved successfully to '{output}'")
return output


Expand Down Expand Up @@ -173,7 +174,7 @@ def main(**options):
try:
create_config(options)
except (Pyp2specError, NotImplementedError) as exc:
click.secho(f"Fatal exception occurred: {exc}", fg="red")
warn(f"Fatal exception occurred: {exc}")
sys.exit(1)

if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion pyp2spec/pyp2spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pyp2spec.pyp2conf import create_config, pypconf_args
from pyp2spec.conf2spec import create_spec_file
from pyp2spec.utils import Pyp2specError
from pyp2spec.utils import warn


@click.command()
Expand All @@ -18,7 +19,7 @@ def main(**options):
config_file = create_config(options)
create_spec_file(config_file, options["spec_output"])
except (Pyp2specError, NotImplementedError) as exc:
click.secho(f"Fatal exception occurred: {exc}", fg="red")
warn(f"Fatal exception occurred: {exc}")
sys.exit(1)


Expand Down
9 changes: 9 additions & 0 deletions pyp2spec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
"""

import re
from functools import partial

import click

from packaging.requirements import Requirement


warn = partial(click.secho, fg="red")
caution = partial(click.secho, fg="magenta")
inform = partial(click.secho, fg="yellow")
yay = partial(click.secho, fg="green")


class Pyp2specError(Exception):
"""Metaexception to derive the custom errors from"""

Expand Down

0 comments on commit 22931b5

Please sign in to comment.