Skip to content

Commit

Permalink
Clean up logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ssorj committed Jan 13, 2024
1 parent d32cb45 commit eaf07e9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ jobs:
- run: curl https://skupper.io/install.sh | sh
- run: echo "$HOME/.local/bin" >> $GITHUB_PATH
- run: ./plano test
env:
PLANO_COLOR: 1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__/
/README.html
/htmlcov
/.coverage
22 changes: 11 additions & 11 deletions .plano.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# under the License.
#

import skewer.tests

from skewer import *

@command(passthrough=True)
Expand All @@ -29,17 +31,14 @@ def test(verbose=False, quiet=False, passthrough_args=[]):
if quiet:
passthrough_args.append("--quiet")

args = " ".join(passthrough_args)

import skewer.tests
PlanoTestCommand(skewer.tests).main(passthrough_args)
with working_env(PYTHONPATH="python"):
run(["plano-test", "-m", "skewer.tests"] + passthrough_args)

@command
def coverage():
def coverage(verbose=False, quiet=False):
"""
Run the tests and measure code coverage
"""

check_program("coverage")

with working_env(PYTHONPATH="python"):
Expand All @@ -48,23 +47,24 @@ def coverage():
run("coverage report")
run("coverage html")

print(f"file:{get_current_dir()}/htmlcov/index.html")
if not quiet:
print(f"file:{get_current_dir()}/htmlcov/index.html")

@command
def render():
def render(verbose=False, quiet=False):
"""
Render README.html from README.md
"""
check_program("pandoc")

run(f"pandoc -o README.html README.md")

print(f"file:{get_real_path('README.html')}")
if not quiet:
print(f"file:{get_real_path('README.html')}")

@command
def clean():
remove(join("python", "__pycache__"))
remove(join("test-example", "python", "__pycache__"))
remove(find(".", "__pycache__"))
remove("README.html")
remove("htmlcov")
remove(".coverage")
Expand Down
11 changes: 5 additions & 6 deletions external/plano-main/src/plano/_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,26 @@ def __init__(self):
self.parser.add_argument("--explode", action="store_true")
self.parser.add_argument("--verbose", action="store_true")
self.parser.add_argument("--quiet", action="store_true")
self.parser.add_argument("--init-only", action="store_true")

def parse_args(self, args):
return self.parser.parse_args(args)

def init(self, args):
self.verbose = args.verbose
self.interrupt = args.interrupt
self.explode = args.explode
self.verbose = args.verbose
self.quiet = args.quiet

def run(self):
if self.verbose:
print("Hello")

if self.interrupt:
raise KeyboardInterrupt()

if self.explode:
raise PlanoError("Exploded")

if self.verbose:
print("Hello")

SomeCommand().main([])
SomeCommand().main(["--interrupt"])

Expand Down Expand Up @@ -1186,7 +1186,6 @@ def run_command(*args):
run_command()
run_command("--help")
run_command("--quiet")
run_command("--init-only")

with expect_system_exit():
run_command("no-such-command")
Expand Down
75 changes: 31 additions & 44 deletions external/plano-main/src/plano/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,14 @@
import traceback as _traceback

class BaseCommand:
default_logging_level = "warning"
verbose_logging_level = "notice"
quiet_logging_level = "error"

def __init__(self):
self.verbose = False
self.quiet = False
self.init_only = False

def parse_args(self, args): # pragma: nocover
raise NotImplementedError()

def init(self, args): # pragma: nocover
pass
def configure_logging(self, args):
return "warning", None

def init(self, args):
raise NotImplementedError()

def run(self): # pragma: nocover
raise NotImplementedError()
Expand All @@ -53,21 +47,11 @@ def main(self, args=None):

assert isinstance(args, _argparse.Namespace), args

level = self.default_logging_level

if self.verbose:
level = self.verbose_logging_level

if self.quiet:
level = self.quiet_logging_level
level, output = self.configure_logging(args)

with logging_enabled(level=level):
with logging_enabled(level=level, output=output):
try:
self.init(args)

if self.init_only:
return

self.run()
except KeyboardInterrupt:
pass
Expand All @@ -90,13 +74,7 @@ def __init__(self, **kwargs):
_plano_command = None

class PlanoCommand(BaseCommand):
default_logging_level = "notice"
verbose_logging_level = "debug"
quiet_logging_level = "error"

def __init__(self, module=None, description="Run commands defined as Python functions", epilog=None):
super().__init__()

self.module = module
self.bound_commands = dict()
self.running_commands = list()
Expand Down Expand Up @@ -155,6 +133,16 @@ def parse_args(self, args):

return args

def configure_logging(self, args):
if args.command is not None:
if args.verbose:
return "debug", None

if args.quiet:
return "warning", None

return "notice", None

def init(self, args):
self.help = args.help

Expand All @@ -163,10 +151,8 @@ def init(self, args):
self.command_kwargs = dict()

if args.command is not None:
# These args are taken from the subcommand
self.verbose = args.verbose
self.quiet = args.quiet
self.init_only = args.init_only

for command in self.preceding_commands:
command()
Expand Down Expand Up @@ -199,8 +185,9 @@ def run(self):
with Timer() as timer:
self.selected_command(*self.command_args, **self.command_kwargs)

cprint("OK", color="green", file=_sys.stderr, end="")
cprint(" ({})".format(format_duration(timer.elapsed_time)), color="magenta", file=_sys.stderr)
if not self.quiet:
cprint("OK", color="green", file=_sys.stderr, end="")
cprint(" ({})".format(format_duration(timer.elapsed_time)), color="magenta", file=_sys.stderr)

def _load_module(self, name):
try:
Expand Down Expand Up @@ -269,11 +256,9 @@ def _process_commands(self):
help="Print detailed logging to the console")
subparser.add_argument("--quiet", action="store_true",
help="Print no logging to the console")
subparser.add_argument("--init-only", action="store_true",
help=_argparse.SUPPRESS)

for param in command.parameters.values():
if param.name in ("verbose", "quiet", "init_only"):
if param.name in ("verbose", "quiet"):
continue

if param.positional:
Expand Down Expand Up @@ -431,20 +416,22 @@ def __call__(self, *args, **kwargs):

app.running_commands.append(self)

dashes = "--- " * (len(app.running_commands) - 1)
display_args = list(self._get_display_args(args, kwargs))
if not app.quiet:
dashes = "--- " * (len(app.running_commands) - 1)
display_args = list(self._get_display_args(args, kwargs))

with console_color("magenta", file=_sys.stderr):
eprint("{}--> {}".format(dashes, self.name), end="")
with console_color("magenta", file=_sys.stderr):
eprint("{}--> {}".format(dashes, self.name), end="")

if display_args:
eprint(" ({})".format(", ".join(display_args)), end="")
if display_args:
eprint(" ({})".format(", ".join(display_args)), end="")

eprint()
eprint()

self.function(*args, **kwargs)

cprint("{}<-- {}".format(dashes, self.name), color="magenta", file=_sys.stderr)
if not app.quiet:
cprint("{}<-- {}".format(dashes, self.name), color="magenta", file=_sys.stderr)

app.running_commands.pop()

Expand Down
6 changes: 2 additions & 4 deletions external/plano-main/src/plano/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

class PlanoTestCommand(BaseCommand):
def __init__(self, test_modules=[]):
super().__init__()

self.test_modules = test_modules

if _inspect.ismodule(self.test_modules):
Expand Down Expand Up @@ -59,8 +57,6 @@ def __init__(self, test_modules=[]):
help="Print detailed logging to the console")
self.parser.add_argument("--quiet", action="store_true",
help="Print no logging to the console")
self.parser.add_argument("--init-only", action="store_true",
help=_argparse.SUPPRESS)

def parse_args(self, args):
return self.parser.parse_args(args)
Expand All @@ -74,6 +70,8 @@ def init(self, args):
self.timeout = args.timeout
self.fail_fast = args.fail_fast
self.iterations = args.iterations
self.verbose = args.verbose
self.quiet = args.quiet

try:
for name in args.module:
Expand Down

0 comments on commit eaf07e9

Please sign in to comment.