]> git.rocketbowman.com Git - nom.git/commitdiff
refactor: clean cli dispatch and tests
authorKyle Bowman <kyle+github@rocketbowman.com>
Sat, 25 Jan 2025 21:56:16 +0000 (16:56 -0500)
committerKyle Bowman <kyle+github@rocketbowman.com>
Sat, 25 Jan 2025 22:20:23 +0000 (17:20 -0500)
src/nom/main.py
tests/test_cli.py

index a56a89ddcaff8ac1e3f6d9e535b1469f158748ff..fc76b2d40ad22c15e3fb611bbf5d138f386f2450 100644 (file)
@@ -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__":
index b7fb51de22a0481e14ae909c1463d91d608c663a..35649c2164a3ebc50559006baab8232b1f4b6435 100644 (file)
@@ -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')