From: Kyle Bowman Date: Sat, 25 Jan 2025 23:26:54 +0000 (-0500) Subject: feat: add ability to update column X-Git-Url: https://git.rocketbowman.com/?a=commitdiff_plain;h=59ffb6f148c594a0d62934d4e2c225b8cb4ae5a3;p=nom.git feat: add ability to update column --- diff --git a/src/nom/base.py b/src/nom/base.py index 3ed198e..dc5192c 100644 --- a/src/nom/base.py +++ b/src/nom/base.py @@ -22,6 +22,9 @@ class NomListItem(BaseModel): def get_fieldnames(cls): return cls.__fields__.keys() + def update(self, key, val): + self.__setattr__(key, val) + # TODO: What if there's a pipe in one of the fields? def to_str(self, delimiter: str ='|'): return delimiter.join([str(v) for v in self.__dict__.values()]) diff --git a/src/nom/main.py b/src/nom/main.py index fc76b2d..3aafdbe 100644 --- a/src/nom/main.py +++ b/src/nom/main.py @@ -26,8 +26,10 @@ def cli(): 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 = entry_subparsers.add_parser('update', parents=[label_parser], help='update entry data.') entry_update_parser.set_defaults(func=handle_entry_update) + entry_update_parser.add_argument('--update', dest="updates", nargs="*", help="Update values as key=value pairs") + # Feed subcommand feed_parser = subparsers.add_parser('feed', help='Dispatches commands that operate on a table of feeds.') @@ -49,11 +51,23 @@ def handle_entry_show(args): 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)}]") + if not ENTRY_LIST.exists(): + elist=EntryList() + else: + elist = EntryList.from_csv(ENTRY_LIST) + + if args.updates: + sublist = elist.select(lambda e: e.label in args.label) + for u in args.updates: + key, val = u.split('=') + for e in elist: + if e in sublist: + e.update(key,val) + else: + 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) def handle_feed_show(args): diff --git a/tests/test_cli.py b/tests/test_cli.py index 35649c2..6ee4f1a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,5 +1,5 @@ from nom.main import cli, dispatch - +from nom.base import NomListItem def argtest(argstr): """ Asserts that the CLI entry points don't raise errors. """ @@ -24,3 +24,9 @@ def test_nom_entry_show_one(): def test_nom_entry_update(): argtest('entry update') + +def test_nom_entry_update_val(): + NomListItem.update = print("Don't really update") #This actually works, lol + argv='entry update 1 --update url=https://path/to/new/url'.split(' ') + args = cli().parse_args(argv) + assert True