Skip to content

Instantly share code, notes, and snippets.

@kjacque
Last active August 19, 2021 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjacque/ad44888415d1b9ae41ffa74ce1f0148a to your computer and use it in GitHub Desktop.
Save kjacque/ad44888415d1b9ae41ffa74ce1f0148a to your computer and use it in GitHub Desktop.
Simple DAOS API example script
#!/usr/bin/python3
# encoding: utf-8
from __future__ import print_function
import sys
from argparse import ArgumentParser
from argparse import RawDescriptionHelpFormatter
import pydaos
import os
import uuid
PYKV = "pykv"
def cont_open(args):
cont = pydaos.DCont(pool=args.pool, cont=args.container)
return cont
def get_store(cont):
try:
kv = cont.get(PYKV)
except:
kv = cont.dict(PYKV)
return kv
def setkv(args):
print("Writing:")
print('Key: {0}'.format(args.key))
print('value: {0}'.format(args.value))
cont = cont_open(args)
try:
store = get_store(cont)
store[args.key] = args.value
except Exception as e:
print("Error writing key:", e)
print("Done.")
def getkv(args):
print("Reading key '{0}':".format(args.key))
cont = cont_open(args)
try:
store = get_store(cont)
print("Value: {0}".format(store[args.key]))
except Exception as e:
print("Error reading key:", e)
print("Done.")
def main(argv=None): # IGNORE:C0111
'''Command line options.'''
if argv is None:
argv = sys.argv
else:
sys.argv.extend(argv)
parser = ArgumentParser(prog="daos", formatter_class=RawDescriptionHelpFormatter)
subparsers = parser.add_subparsers(help="Sub-command Help")
parse_setkv = subparsers.add_parser("setkv", help="Given a pool and container set the key to the provided value")
parse_setkv.add_argument("--pool", required=True, help="UUID of the Pool that contains the container.")
parse_setkv.add_argument("--container", required=True, help="UUID of the Container to set the key in.")
parse_setkv.add_argument("--key", required=True, help="Key to set the value of in the container (used for both dkey and akey)")
parse_setkv.add_argument("--value", required=True, help="Value to set the key to.")
parse_setkv.set_defaults(func=setkv)
parse_getkv = subparsers.add_parser("getkv", help="Given a pool and container get the value from the provided key")
parse_getkv.add_argument("--pool", required=True, help="UUID of the Pool that contains the container.")
parse_getkv.add_argument("--container", required=True, help="UUID of the Container to set the key in.")
parse_getkv.add_argument("--key", required=True, help="Key to get the value of in the container")
parse_getkv.set_defaults(func=getkv)
if len(sys.argv) < 2:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
args.func(args)
return 0
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment