Skip to content

Commit

Permalink
Add functions for printing to stdout or debug outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
dmach committed Dec 12, 2022
1 parent d81c688 commit 78c3cf1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions osc/_private/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
#
# The cherry-picked imports will be the supported API.

from .common import print_msg
from .common import format_msg_project_package_options
from .package import ApiPackage
from .request import forward_request
35 changes: 35 additions & 0 deletions osc/_private/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sys


def print_msg(msg, print_to="debug"):
from .. import conf

if print_to is None:
return
elif print_to == "debug":
if conf.config["debug"]:
print(f"DEBUG: {msg}", file=sys.stderr)
elif print_to == "stdout":
print(msg)
else:
raise ValueError(f"Invalid value of the 'output' option: {output}")


def format_msg_project_package_options(msg, project=None, package=None, **options):
"""
Format msg, project, package and options into a meaningful message
that can be printed out directly or as a debug message.
"""
if project:
msg += f" project: '{project}'"

if package:
msg += f" package: '{package}'"

msg_options = [key.replace("_", "-") for key, value in options.items() if value]
if msg_options:
msg_options.sort()
msg_options_str = ", ".join(msg_options)
msg += f" options: {msg_options_str}"

return msg

0 comments on commit 78c3cf1

Please sign in to comment.