Commit graph

23 commits

Author SHA1 Message Date
a4fb791649 Cmd / Cmds: Add config file support
An application based on Cmds will now try to read StringTree
formatted config data from a file $HOME/.<name>rc, and make the data
available via the newly added conf_value() method.

Signed-off-by: Jan Lindemann <jan@janware.com>
2022-12-08 16:44:49 +01:00
bfd0544ff8 Fix errors reported by mypy
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-10-13 12:45:51 +02:00
1e16c0ffb1 Cmds: Add argcomplete support
argcomplete takes the arguments added to argparse, and builds bash
completion with it. Add it to all Cmds based executables.

Signed-off-by: Jan Lindemann <jan@janware.com>
2025-07-11 10:43:47 +02:00
428692ea3a Streamline Python file headers somewhat
- Add coding statement
  - Import all modules in one line where possible
  - Order: __future__, typing, plain imports, from imports,
    janware modules

Signed-off-by: Jan Lindemann <jan@janware.com>
2025-07-10 05:14:06 +02:00
d35a5588cd Cmds.__run(): Don't exit(0)
WSGI doesn't like sys.exit() being called, so avoid it. Two cases
need to be taken into consideration:

  1. No exception thrown by self.args.func()

     The variable exit_status is zero, we can check that and _not_
     call sys.exit()

  2. Exception thrown by self.args.func()

     In that case, the exception should be raised, to be caught by
     WSGI / ASGI whatever to do what they want with it. The code
     calling sys.exit() is never reached. Hence, we need to add
     --backtrace to the invocation options.

Signed-off-by: Jan Lindemann <jan@janware.com>
2025-07-05 12:27:11 +02:00
958c077da3 Cmds: Support --write-profile <filename>
Add an option which makes the program write profiling statistics in pstats
format to a specified file.

Visualize e.g. with:

  gprof2dot -f pstats <filename> -o <dotfile>
  dot -Tpng | display <dotfile>

Signed-off-by: Jan Lindemann <jan@janware.com>
2025-06-05 16:58:26 +02:00
5ee88c1a81 cmd.Cmds: Support --backtrace
Wrap all execution in try-block, don't raise in except block unless
--backtrace is passed to the command line.

Signed-off-by: Jan Lindemann <jan@janware.com>
2025-05-30 15:53:43 +02:00
859bb8000f Cmds / Cmd: Add comments and debug logging
Add some comments and a little debug logging to clarify operation.

Signed-off-by: Jan Lindemann <jan@janware.com>
2025-02-02 14:01:21 +01:00
2ec6e72cff Cmds: Add parsed args as attribute
The parsed args container is passed to the run() function and friends
for convenient use.  Sometimes, though, the base classes or umbrella
commands need to make use of it, too, even more so as they may define
command line arguments via add_arguments(). However, run() or _run()
or whatever is never called on them, neither any other callback, so
make args available to them as a member variable.

Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-29 12:51:01 +01:00
055c008a33 Cmds: Make run() accept argv argument
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-28 20:27:25 +01:00
224a16ec71 Cmds: Add TODO Rename to App
There's an ungreppable TODO remark in Cmds about renaming the class
to App, make it greppable.

Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-18 20:53:45 +01:00
4e53e37c7c Cmds.py: Emit usage message by default
Running an application derived from Cmds throws an error if invoked
without arguments, fix that.

Signed-off-by: Jan Lindemann <jan@janware.com>
2024-07-11 20:38:07 +02:00
77d43aebad Add type annotations from monkeytype + jw-devops/test
Add type annotations as generated by monkeytype and jw-devops/test, plus some
hand editing to satisfy both monkeytype and mypy.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-10 17:55:36 +02:00
a9dd9c7d0d jwutils.Cmds: Remove some useless code
This commit removes some useless code from the Cmds class.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-10 14:13:59 +02:00
33d3b7a7b3 jwutils.Cmds: Cmd.add_subcommands() fails for multiple commands
jwutils.Cmd.add_subcommands() had two issues. First, it failed for
more than one command, and with that fixed, it only added the first
command of a list of commands passed as an argument. This commit
fixes both issues and leaves some logging and cleanup in place, which
was used during debugging.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-07 09:23:13 +02:00
edf861c985 jwutils.Cmds: Fix empty to cmds member variable
Cmds.cmds was sometimes empty, so fix that and make sure it points to
the Cmds instance it was crated by. Actually the name of the Cmds
class is a bad pick, should have been App or something, so to add
.app, too, as a path for future compatibility.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-05 14:17:54 +02:00
c5b964a5bb jwutils.Cmd: Add method add_subcommands()
Add method jwutils.Cmd.add_subcommands(). cmd.add_subcommands(C) will
parse and invoke C as a subcommand of cmd, if C is of type jwutils.cmd.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-04 16:32:39 +02:00
ad67dd3832 jwutils.Cmds: Beautify log invocations
Add from jwutils import log to avoid unnecessary jwutils.log module
path.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-04 13:43:08 +02:00
e48fa1b00a Cmds.py: Add early evaluation of --log-xxx
jwutils.log.set_flags() and .set_level() is not set until after all
commands are loaded. This can make debugging complicated. OTOH,
command-line parsing is also not done until all commands are loaded,
because they define the command line options. This commit adds some
poor man's command line parsing for fetching --log-level and
--log-flags only, and applying them before the commands are all
loaded.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-04 11:39:01 +02:00
a85f0929c6 Consistently replace def run() by async def run()
Signed-off-by: Jan Lindemann <jan@janware.com>
2019-12-20 08:41:58 +01:00
6123a68195 Cmds.py: Run all commands in asyncio event loop
This commit makee Cmds run all sub-commands in an asyncio event loop.
The event loop is currently passed to the commands, which seems
unnecessary and looks like it's using a feature which is bound to be
deprecated in the future.

Signed-off-by: Jan Lindemann <jan@janware.com>
2019-10-28 13:12:18 +01:00
6dd594d47b Fix multiple Python 3 compatibility issues
Changes in Python 3 that made the code choke:

  o basestring is merged into str
  o print() needs parentesis
  o Class inheritance syntax changed
  o Abstract baseclass (ABCMeta) syntax changed
  o map.iteritems() is replaced by map.items()
  o Inconsistent use of tabs and spaces are no longer tolerated

Signed-off-by: Jan Lindemann <jan@janware.com>
2019-03-10 16:38:59 +01:00
fd125f07fe Add class Cmds
run_sub_commands() was the only way to access subcommands up to now,
Cmds.run() adds another interface, an object as a place to add
customizations affecting all commands, e.g. global command line
options.

Signed-off-by: Jan Lindemann <jan@janware.com>
2018-12-11 14:10:01 +01:00