Skip to content

Commit

Permalink
Update Plano
Browse files Browse the repository at this point in the history
  • Loading branch information
ssorj committed Jan 12, 2024
1 parent e78b898 commit d32cb45
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 39 deletions.
4 changes: 4 additions & 0 deletions .plano.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def test(verbose=False, quiet=False, passthrough_args=[]):

@command
def coverage():
"""
Run the tests and measure code coverage
"""

check_program("coverage")

with working_env(PYTHONPATH="python"):
Expand Down
8 changes: 6 additions & 2 deletions external/plano-main/src/plano/_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ def archive_operations():
def command_operations():
class SomeCommand(BaseCommand):
def __init__(self):
super().__init__()

self.parser = BaseArgumentParser()
self.parser.add_argument("--interrupt", action="store_true")
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)
Expand All @@ -90,10 +95,9 @@ def run(self):

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

with expect_system_exit():
SomeCommand().main(["--verbose", "--debug", "--explode"])
SomeCommand().main(["--verbose", "--explode"])

@test
def console_operations():
Expand Down
73 changes: 39 additions & 34 deletions external/plano-main/src/plano/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,24 @@
import traceback as _traceback

class BaseCommand:
initial_logging_level = "warning"
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 run(self): # pragma: nocover
raise NotImplementedError()

def main(self, args=None):
if args is None:
args = ARGS[1:]
Expand All @@ -39,22 +53,14 @@ def main(self, args=None):

assert isinstance(args, _argparse.Namespace), args

self.verbose = args.verbose or args.debug
self.quiet = args.quiet
self.debug = args.debug
self.init_only = args.init_only

level = self.initial_logging_level
level = self.default_logging_level

if self.verbose:
level = self.verbose_logging_level

if self.quiet:
level = self.quiet_logging_level

if self.debug:
level = "debug"

with logging_enabled(level=level):
try:
self.init(args)
Expand All @@ -66,46 +72,31 @@ def main(self, args=None):
except KeyboardInterrupt:
pass
except PlanoError as e:
if self.debug:
if PLANO_DEBUG:
_traceback.print_exc()
exit(1)
else:
exit(str(e))

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

def init(self, args): # pragma: nocover
pass

def run(self): # pragma: nocover
raise NotImplementedError()

class BaseArgumentParser(_argparse.ArgumentParser):
def __init__(self, **kwargs):
super().__init__(**kwargs)

self.allow_abbrev = False
self.formatter_class = _argparse.RawDescriptionHelpFormatter

self.add_argument("--verbose", action="store_true",
help="Print detailed logging to the console")
self.add_argument("--quiet", action="store_true",
help="Print no logging to the console")
self.add_argument("--debug", action="store_true",
help="Print debugging output to the console")
self.add_argument("--init-only", action="store_true",
help=_argparse.SUPPRESS)

_capitalize_help(self)

_plano_command = None

class PlanoCommand(BaseCommand):
initial_logging_level = "notice"
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 @@ -172,6 +163,11 @@ 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 @@ -269,8 +265,17 @@ def _process_commands(self):

subparser = subparsers.add_parser(command.name, help=help, add_help=add_help, description=description,
formatter_class=_argparse.RawDescriptionHelpFormatter)
subparser.add_argument("--verbose", action="store_true",
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"):
continue

if param.positional:
if param.multiple:
subparser.add_argument(param.name, metavar=param.metavar, type=param.type, help=param.help,
Expand Down Expand Up @@ -323,9 +328,9 @@ def __init__(self, function):
self.parent = parent

if self.parent is None:
# Strip trailing underscores and convert remaining
# underscores to hyphens
default = self.function.__name__.rstrip("_").replace("_", "-")
# Strip leading and trailing underscores and convert
# remaining underscores to hyphens
default = self.function.__name__.strip("_").replace("_", "-")

self.name = nvl(self.name, default)
self.parameters = self._process_parameters(parameters)
Expand Down Expand Up @@ -500,7 +505,7 @@ def __init__(self, name, display_name=None, type=None, metavar=None, help=None,
self.multiple = False

def __repr__(self):
return "argument '{}' (default {})".format(self.name, repr(self.default))
return "parameter '{}' (default {})".format(self.name, repr(self.default))

# Patch the default help text
def _capitalize_help(parser):
Expand Down
4 changes: 2 additions & 2 deletions external/plano-main/src/plano/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ def _print_message(level, message, args):

line.append(cformat(program_text, color="gray"))

level_text = "{}:".format(_logging_levels[level]).ljust(8)
level_text = "{}:".format(_logging_levels[level])
level_color = ("white", "cyan", "yellow", "red", None)[level]
level_bright = (False, False, False, True, False)[level]

Expand Down Expand Up @@ -1247,7 +1247,7 @@ def wait(proc, timeout=None, check=False, quiet=False):
try:
proc.wait(timeout=timeout)
except _subprocess.TimeoutExpired:
# XXX warning or error
error("{} timed out after {} seconds", proc, timeout)
raise PlanoTimeout()

if proc.exit_code == 0:
Expand Down
8 changes: 7 additions & 1 deletion external/plano-main/src/plano/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

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

self.test_modules = test_modules

Expand All @@ -55,6 +55,12 @@ def __init__(self, test_modules=[]):
help="Exit on the first failure encountered in a test run")
self.parser.add_argument("--iterations", metavar="COUNT", type=int, default=1,
help="Run the tests COUNT times (default 1)")
self.parser.add_argument("--verbose", action="store_true",
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 Down

0 comments on commit d32cb45

Please sign in to comment.