From afc70926d7e7d4229bb373aa12cf5518b2888411 Mon Sep 17 00:00:00 2001 From: Yann Sionneau Date: Wed, 17 Jun 2015 16:40:27 +0200 Subject: [PATCH 1/2] artiq_coreconfig: use subparsers for arg parsing --- artiq/frontend/artiq_coreconfig.py | 64 +++++++++++++++++------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/artiq/frontend/artiq_coreconfig.py b/artiq/frontend/artiq_coreconfig.py index 7540b7f32..d5289bdb4 100755 --- a/artiq/frontend/artiq_coreconfig.py +++ b/artiq/frontend/artiq_coreconfig.py @@ -13,20 +13,30 @@ def to_bytes(string): def get_argparser(): parser = argparse.ArgumentParser(description="ARTIQ core device config " "remote access") - parser.add_argument("-r", "--read", type=to_bytes, - help="read key from core device config") - parser.add_argument("-w", "--write", nargs=2, action="append", default=[], - metavar=("KEY", "STRING"), type=to_bytes, - help="write key-value records to core device config") - parser.add_argument("-f", "--write-file", nargs=2, action="append", - type=to_bytes, default=[], metavar=("KEY", "FILENAME"), - help="write the content of a file into core device " - "config") - parser.add_argument("-e", "--erase", action="store_true", - help="erase core device config") - parser.add_argument("-d", "--delete", action="append", default=[], - type=to_bytes, - help="delete key from core device config") + subparsers = parser.add_subparsers(dest="action") + subparsers.required = True + p_read = subparsers.add_parser("read", + help="read key from core device config") + p_read.add_argument("-k", "--key", type=to_bytes, required=True, + help="key to be read from core device config") + p_write = subparsers.add_parser("write", + help="write key-value records to core " + "device config") + p_write.add_argument("-s", "--string", nargs=2, action="append", + default=[], metavar=("KEY", "STRING"), type=to_bytes, + help="key-value records to be written to core device " + "config") + p_write.add_argument("-f", "--file", nargs=2, action="append", + type=to_bytes, default=[], + metavar=("KEY", "FILENAME"), + help="key and file whose content to be written to " + "core device config") + subparsers.add_parser("erase", help="erase core device config") + p_delete = subparsers.add_parser("delete", + help="delete key from core device config") + p_delete.add_argument("-k", "--key", action="append", default=[], + type=to_bytes, required=True, + help="key to be deleted from core device config") parser.add_argument("--ddb", default="ddb.pyon", help="device database file") return parser @@ -37,25 +47,23 @@ def main(): ddb = FlatFileDB(args.ddb) comm = create_device(ddb.request("comm"), None) - if args.read: - value = comm.flash_storage_read(args.read) + if args.action == "read": + value = comm.flash_storage_read(args.key) if not value: - print("Key {} does not exist".format(args.read)) + print("Key {} does not exist".format(args.key)) else: print(value) - elif args.erase: + elif args.action == "erase": comm.flash_storage_erase() - elif args.delete: - for key in args.delete: + elif args.action == "delete": + for key in args.key: comm.flash_storage_remove(key) - else: - if args.write: - for key, value in args.write: - comm.flash_storage_write(key, value) - if args.write_file: - for key, filename in args.write_file: - with open(filename, "rb") as fi: - comm.flash_storage_write(key, fi.read()) + elif args.action == "write": + for key, value in args.string: + comm.flash_storage_write(key, value) + for key, filename in args.file: + with open(filename, "rb") as fi: + comm.flash_storage_write(key, fi.read()) if __name__ == "__main__": main() From 250f9e0bc072ee2a90d94ad8cda072edd3b0ae57 Mon Sep 17 00:00:00 2001 From: Yann Sionneau Date: Wed, 17 Jun 2015 16:46:39 +0200 Subject: [PATCH 2/2] manual: update artiq_coreconfig parameter's syntax --- doc/manual/installing.rst | 2 +- doc/manual/utilities.rst | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/doc/manual/installing.rst b/doc/manual/installing.rst index 145eb585b..cfc8643b9 100644 --- a/doc/manual/installing.rst +++ b/doc/manual/installing.rst @@ -265,7 +265,7 @@ To flash the ``idle`` kernel: * Write it into the core device configuration flash storage: :: - $ artiq_coreconfig -f idle_kernel idle.elf + $ artiq_coreconfig write -f idle_kernel idle.elf .. note:: You can find more information about how to use the ``artiq_coreconfig`` tool on the :ref:`Utilities ` page. diff --git a/doc/manual/utilities.rst b/doc/manual/utilities.rst index 027d22470..a27d7d8d7 100644 --- a/doc/manual/utilities.rst +++ b/doc/manual/utilities.rst @@ -109,38 +109,38 @@ When not specified, the artiq_coreconfig tool will assume that there is a file n To read the record whose key is ``mac``:: - $ artiq_coreconfig -r mac + $ artiq_coreconfig read -k mac To write the value ``test_value`` in the key ``my_key``:: - $ artiq_coreconfig -w my_key test_value - $ artiq_coreconfig -r my_key + $ artiq_coreconfig write -s my_key test_value + $ artiq_coreconfig read -k my_key b'test_value' You can also write entire files in a record using the ``-f`` parameter. This is useful for instance to write the ``idle`` kernel in the flash storage:: - $ artiq_coreconfig -f idle_kernel idle.elf - $ artiq_coreconfig -r idle_kernel | head -c9 + $ artiq_coreconfig write -f idle_kernel idle.elf + $ artiq_coreconfig read -k idle_kernel | head -c9 b'\x7fELF You can write several records at once:: - $ artiq_coreconfig -w key1 value1 -f key2 filename -w key3 value3 + $ artiq_coreconfig write -s key1 value1 -f key2 filename -s key3 value3 To remove the previously written key ``my_key``:: - $ artiq_coreconfig -d my_key + $ artiq_coreconfig delete -k my_key To erase the entire flash storage area:: - $ artiq_coreconfig -e + $ artiq_coreconfig erase You don't need to remove a record in order to change its value, just overwrite it:: - $ artiq_coreconfig -w my_key some_value - $ artiq_coreconfig -w my_key some_other_value - $ artiq_coreconfig -r my_key + $ artiq_coreconfig write -s my_key some_value + $ artiq_coreconfig write -s my_key some_other_value + $ artiq_coreconfig read -k my_key b'some_other_value' .. argparse::