Commit graph

110 commits

Author SHA1 Message Date
cc7aeeac31 misc: Add load_function()
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-29 16:54:10 +01:00
e004f8ade7 db: Add TableIoHandler
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-28 14:27:27 +01:00
7399388f60 Fix errors reported by mypy
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-29 16:51:07 +01:00
bee3e28ff5 Config: Accept strings as value for glob_paths
Make Config's ctor accept strings as values for glob_paths, as
opposed to lists. This covers the canonical case of one configuration
file being passed.

Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-29 12:56:32 +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
d5e268a03b log: Add throw()
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-28 10:17:27 +01:00
2462ad8c60 log: Add log_level()
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-28 10:17:17 +01:00
ce347772dd misc: Add debug logging to load_class()
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-28 10:16:06 +01:00
ddb1fd0eb4 Add db.rows
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-28 08:11:54 +01:00
8c0b975e1b Config: Mutually exclude loading from tree or files
Initializing Config from a StringTree object doesn't stop it from
looking for config files to read. Stop that.

Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-28 08:05:16 +01:00
64ce94b142 String Tree: Fix order of overwrites
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-28 07:18:50 +01:00
126ecbb411 StringTree: Beautify debug logging
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-28 05:08:50 +01:00
84a4053157 Add class Config
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-20 11:39:56 +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
45b5a8fb5a log: Add prio_gets_logged()
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-18 16:42:33 +01:00
24bfa0f806 stree/serdes: Support -include lines
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-16 13:44:09 +01:00
a0234e7d54 Add module jwutils.cast
Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-16 12:39:20 +01:00
57ff96608b StringTree.value(): Add keyword-argument default
Add argument default to StringTree.value(), defaulting to None, doing
the obvious.

Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-16 10:53:09 +01:00
381514ab2c misc: Add load_class()
Add load_class() to complement load_object(), returning the class as
opposed to instantiated object.

Signed-off-by: Jan Lindemann <jan@janware.com>
2025-01-16 10:51:28 +01:00
3a7fb50979 Apply monkeytype traces
Run

       monkeytype run jw-acc.py --log-level debug db import --format mdb-json-dir dbs
       monkeytype list-modules | grep ^jw | while read m; do
               monkeytype apply $m
       done

and fix the fallout.

Signed-off-by: Jan Lindemann <jan@janware.com>
2024-12-15 15:35:29 +01:00
385edd7169 StopWatch: Fix compilation error
Signed-off-by: Jan Lindemann <jan@janware.com>
2024-12-03 13:34:26 +01:00
4b8d67b8ee Cmd.add_subcommand(): Add support for passing cmd argument as string
Signed-off-by: Jan Lindemann <jan@janware.com>
2024-12-03 13:34:25 +01:00
3cd7926693 stree.parse(): Beautify debug logging
Signed-off-by: Jan Lindemann <jan@janware.com>
2024-10-06 17:13:41 +02:00
08045442e2 StringTree: Fix exception in child_list()
child_list() throws an exception saying that <str> has no to_list()
method. No, it hasn't. No idea how this has even worked at all, ever.

Signed-off-by: Jan Lindemann <jan@janware.com>
2024-10-06 17:04:25 +02: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
748247f1ed Add jwutils.StopWatch
Add StopWatch class, intended to be useful for benchmarking.

Signed-off-by: Jan Lindemann <jan@janware.com>
2024-06-03 12:33:36 +02:00
27813efc04 Fix bogus type errors dug up by mypy
Signed-off-by: Jan Lindemann <jan@janware.com>
2024-06-02 12:05:44 +02:00
319d97e8bc log.py: Fix mypy warning (Incompatible types in assignment)
Fix mypy warning

   jwutils/log.py:209: error: Incompatible types in assignment
   (expression has type int, variable has type Literal[5])

Signed-off-by: Jan Lindemann <jan@janware.com>
2022-10-04 13:11:44 +02:00
8029b672bf slog(): Add only_printable keyword argument
Add only_printable keyword argument to slog(). It defaults to False.
If it's true, non-printable characters are not logged but replaced
with a printable character.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-05-16 15:33:13 +00:00
c63d1a729e misc: Add dump()
Add misc.dump(), which takes an iterable, assumes all elements have a
dump() method, and calls it on all of them.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-23 10:34:28 +02:00
4a047ca260 log.remove_from_prefix(): Add support for str argument
In addition to an integer, a string can now be passed, which allows

	prefix = ' | '
	append_to_prefix(prefix)
	slog(INFO, "something")
	remove_from_prefix(prefx)

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-23 10:32:05 +02:00
86e25a2dfb misc: Add load_(classes|class_names|object)()
Add some functions to aid in dynamically loading objects.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-22 14:02:35 +02:00
85672d6cf3 StringTree.value(): Add optional path argument
As opposed to the C++ variant, StringTree.value() doesn't take a path
argument, but only returns the value of the node it's called on.
Change this. Returns None if the path is not found.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-18 08:38:34 +02:00
ee3d49a23c Add multi_key_dict.py
Add multi_key_dict.py from https://github.com/formiaczek/multi_key_dict

It uses an MIT license, the Copyright notice should be added to this
project's documentation.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-13 15:14:52 +02:00
5924ec9769 log.get_caller_pos(): Add kwargs argument
Add a kwargs keyword argument. This should allow a kwargs containing
a "caller=" argument to be passed to the function and just do the
right thing. Badly tested.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-11 10:56:30 +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
952bf4a4e1 Fix Python errors reported by mypy
This commit fixes Python errors and warnings reported by static type checking
with mypy.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-10 14:17:15 +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
47f4de938f jwutils.misc: Add commit_tmpfile()
commit_tmpfile() is a better os.rename which doesn't do anything if
source and target files are equal, and logs if does something.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-08 08:50:54 +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
6d6c48e6b8 jwutils.misc: Add get_derived_classes()
get_derived_classes(mod, baseclass) returns a list of class
definitions from a module which are subclasses of baseclass.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-05 16:53:12 +02:00
fb6b205440 StringTree.__getitem__(): Return value child
StringTree.__getitem__() returned the node, now return its value
child instead. That's nicer and closer to the expected behaviour, as
it supports StringTree usage as a key-value store for configuration
or so. It breaks the existing API, but I think I have fixed the only
pace where it was used: jw-grammar/grammar.py.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-05 14:23:59 +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
dc532704d0 StringTree: Add support for adding StringTree children
Add support for adding children of type StringTree. If a StringTree
argument is passed to .add(), this should do the right thing. It
makes use of the newly added .__add_children() method and involves
quite a bit of hairy case distinctions.

Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-04 11:35:26 +02:00
dead9d9561 StringTree: add child_list()
Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-04 11:31:25 +02:00
160667b6a9 StringTree: Re-implement __str__()
Signed-off-by: Jan Lindemann <jan@janware.com>
2020-04-04 11:29:14 +02:00