From 01577288aa53436c08ebcb784cf4c17fd9f11fc3 Mon Sep 17 00:00:00 2001 From: Kyle Bowman Date: Sat, 25 Jan 2025 16:56:16 -0500 Subject: [PATCH] refactor: clean cli dispatch and tests --- src/nom/main.py | 54 ++++++++++++++++++++++++++++------------------- tests/test_cli.py | 30 ++++++++++++++------------ 2 files changed, 49 insertions(+), 35 deletions(-) diff --git a/src/nom/main.py b/src/nom/main.py index a56a89d..fc76b2d 100644 --- a/src/nom/main.py +++ b/src/nom/main.py @@ -25,40 +25,50 @@ def cli(): entry_parser = subparsers.add_parser('entry', help='Dispatches commands that operate on a table of entries.') entry_subparsers = entry_parser.add_subparsers(dest='entry_command', help='Dispatches entry commands.') entry_show_parser = entry_subparsers.add_parser('show', parents=[label_parser], help='Show entries.') + entry_show_parser.set_defaults(func=handle_entry_show) entry_update_parser = entry_subparsers.add_parser('update', help='Update entry data.') + entry_update_parser.set_defaults(func=handle_entry_update) # Feed subcommand feed_parser = subparsers.add_parser('feed', help='Dispatches commands that operate on a table of feeds.') feed_subparsers = feed_parser.add_subparsers(dest='feed_command', help='Dispatches feed commands.') feed_update_parser = feed_subparsers.add_parser('update', help='Update feed') + feed_update_parser.set_defaults(func=handle_feed_update) feed_show_parser = feed_subparsers.add_parser('show', help='Show feeds') + feed_show_parser.set_defaults(func=handle_feed_show) return parser +def dispatch(args): + # Relies on setting args.func with set_defaults in cli() + args.func(args) -def main(args=['nom'].append(sys.argv)): - parser = cli() - args = parser.parse_args(args=args) +def handle_entry_show(args): + elist = EntryList.from_csv(ENTRY_LIST) + if args.label: + elist = elist.select(lambda e: e.label in args.label) + elist.to_stdout(show_header=True) + +def handle_entry_update(args): + elist=EntryList() + feedlist=FeedList.from_csv(FEED_LIST) + for flitem in feedlist: + elist += flitem.to_feed().to_entrylist() + print(f"Updating from [{str(flitem)}]") + elist.to_csv(ENTRY_LIST) - # Dispatch Logic +def handle_feed_show(args): feedlist=FeedList.from_csv(FEED_LIST) - if args.command == "entry" and args.entry_command == "update": - elist=EntryList() - for flitem in feedlist: - elist += flitem.to_feed().to_entrylist() - print(f"Updating from [{str(flitem)}]") - elist.to_csv(ENTRY_LIST) - elif args.command == "entry" and args.entry_command == "show": - elist = EntryList.from_csv(ENTRY_LIST) - if args.label: - elist = elist.select(lambda e: e.label in args.label) - elist.to_stdout(show_header=True) - elif args.command == "feed" and args.feed_command == "update": - feedlist.update_labels() - feedlist.fetch_feeds(FEED_CACHE) - elif args.command == "feed" and args.feed_command == "show": - feedlist.to_stdout(show_header=True) - else: - raise NomError("That option is not yet supported.") + feedlist.to_stdout(show_header=True) + +def handle_feed_update(args): + feedlist=FeedList.from_csv(FEED_LIST) + feedlist.update_labels() + feedlist.fetch_feeds(FEED_CACHE) + +def main(): + parser = cli() + args = parser.parse_args() + dispatch(args) if __name__ == "__main__": diff --git a/tests/test_cli.py b/tests/test_cli.py index b7fb51d..35649c2 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,22 +1,26 @@ -from nom.main import main +from nom.main import cli, dispatch -def test_nom_entry_show(): - main(args='entry show'.split(' ')) - assert True - -def test_nom_entry_show_one(): - main(args='entry show 1'.split(' ')) +def argtest(argstr): + """ Asserts that the CLI entry points don't raise errors. """ + argv = argstr.split(' ') + args = cli().parse_args(argv) + dispatch(args) assert True +# Feed Tests def test_nom_feed_update(): - main(args='feed update'.split(' ')) - assert True + argtest('feed update') def test_nom_feed_show(): - main(args='feed show'.split(' ')) - assert True + argtest('feed show') + +# Entry Tests +def test_nom_entry_show(): + argtest('entry show') + +def test_nom_entry_show_one(): + argtest('entry show 1') def test_nom_entry_update(): - main(args='entry update'.split(' ')) - assert True \ No newline at end of file + argtest('entry update') -- 2.39.5