Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

put config object as parameter to functions #91

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions h5pyd/_apps/hsacl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
else:
from .config import Config

cfg = Config()

#
# get given ACL, return None if not found
#
Expand All @@ -46,7 +44,7 @@ def getACL(f, username="default"):
#
# Usage
#
def printUsage():
def printUsage(cfg):
print("")
print("Usage: {} [options] domain [+crudep] [-crudep] [userid1 userid2 ...]".format(cfg["cmd"]))
print("")
Expand Down Expand Up @@ -75,6 +73,8 @@ def printUsage():


def main():
cfg = Config()

cfg["cmd"] = sys.argv[0].split('/')[-1]
if cfg["cmd"].endswith(".py"):
cfg["cmd"] = "python " + cfg["cmd"]
Expand All @@ -91,7 +91,7 @@ def main():
remove_list = set()

if len(sys.argv) == 1 or sys.argv[1] == "-h":
printUsage()
printUsage(cfg)

# setup logging
logging.basicConfig(filename=logfname, format='%(levelname)s %(asctime)s %(message)s', level=loglevel)
Expand Down Expand Up @@ -120,13 +120,13 @@ def main():
elif val == "ERROR":
loglevel = logging.ERROR
else:
printUsage()
printUsage(cfg)
argn += 2
elif domain is None and arg == '--logfile':
logfname = val
argn += 2
elif domain is None and arg in ("-h", "--help"):
printUsage()
printUsage(cfg)
elif domain is None and arg in ("-e", "--endpoint"):
cfg["hs_endpoint"] = val
argn += 2
Expand All @@ -141,7 +141,7 @@ def main():
argn += 2
elif domain is None and arg[0] in ('-', '+'):
print("No domain given")
printUsage()
printUsage(cfg)
elif domain is None:
logging.debug("get domain")
domain = arg
Expand All @@ -154,23 +154,23 @@ def main():
logging.debug("got plus")
if len(usernames) > 0:
logging.debug("usernames:", usernames)
printUsage()
printUsage(cfg)
add_list = set(arg[1:])
logging.info("add_list:", add_list)
argn += 1

elif arg[0] == '-':
logging.debug("got minus")
if len(usernames) > 0:
printUsage()
printUsage(cfg)
remove_list = set(arg[1:])
logging.info("remove_list:", remove_list)
argn += 1
else:
logging.info("got username:", arg)
if arg.find('/') >= 0:
print("Invalid username:", arg)
printUsage()
printUsage(cfg)
usernames.append(arg)
argn += 1

Expand All @@ -181,7 +181,7 @@ def main():

if len(usernames) == 0 and (add_list or remove_list):
print("At least one username must be given to add/remove permissions")
printUsage()
printUsage(cfg)

if domain is None:
print("no domain specified")
Expand Down
20 changes: 9 additions & 11 deletions h5pyd/_apps/hscopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@
from .utillib import load_file


cfg = Config()


#----------------------------------------------------------------------------------
def usage():
def usage(cfg):
print("Usage:\n")
print((" {} [ OPTIONS ] source destination".format(cfg["cmd"])))
print("")
Expand Down Expand Up @@ -71,6 +68,7 @@ def print_config_example():

#----------------------------------------------------------------------------------
def main():
cfg = Config()

loglevel = logging.ERROR
verbose = False
Expand All @@ -91,7 +89,7 @@ def main():
if arg[0] == '-' and len(src_files) > 0:
# options must be placed before filenames
print("options must precead source files")
usage()
usage(cfg)
sys.exit(-1)
if len(sys.argv) > argn + 1:
val = sys.argv[argn+1]
Expand All @@ -112,14 +110,14 @@ def main():
loglevel = logging.ERROR
else:
print("unknown loglevel")
usage()
usage(cfg)
sys.exit(-1)
argn += 2
elif arg == '--logfile':
logfname = val
argn += 2
elif arg in ("-h", "--help"):
usage()
usage(cfg)
sys.exit(0)
elif arg in ("-e", "--endpoint"):
cfg["hs_endpoint"] = val
Expand Down Expand Up @@ -147,7 +145,7 @@ def main():
deflate = compressLevel
argn += 1
elif arg[0] == '-':
usage()
usage(cfg)
sys.exit(-1)
else:
src_files.append(arg)
Expand All @@ -164,7 +162,7 @@ def main():

if len(src_files) < 2:
# need at least a src and destination
usage()
usage(cfg)
sys.exit(-1)
src_domain = src_files[0]
des_domain = src_files[1]
Expand All @@ -173,12 +171,12 @@ def main():
logging.info("target domain: {}".format(des_domain))
if src_domain[0] != '/' or src_domain[-1] == '/':
print("source domain must be an absolute path, non-folder domain")
usage()
usage(cfg)
sys.exit(-1)

if des_domain[0] != '/' or des_domain[-1] == '/':
print("source domain must be an absolute path, non-folder domain")
usage()
usage(cfg)
sys.exit(-1)


Expand Down
22 changes: 11 additions & 11 deletions h5pyd/_apps/hsdel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
# Delete domain
#

cfg = Config()

def getFolder(domain, mode='r'):
def getFolder(cfg, domain, mode='r'):
username = cfg["hs_username"]
password = cfg["hs_password"]
endpoint = cfg["hs_endpoint"]
Expand All @@ -22,7 +20,7 @@ def getFolder(domain, mode='r'):
return dir


def deleteDomain(domain):
def deleteDomain(cfg, domain):

# get handle to parent folder
if domain.endswith('/'):
Expand All @@ -39,7 +37,7 @@ def deleteDomain(domain):
if not parent_domain.endswith('/'):
parent_domain += '/'
try:
hparent = getFolder(parent_domain, mode='a')
hparent = getFolder(cfg, parent_domain, mode='a')
except IOError as oe:
if oe.errno == 404: # Not Found
sys.exit("Parent domain: {} not found".format(parent_domain))
Expand Down Expand Up @@ -79,7 +77,7 @@ def deleteDomain(domain):
#
# Usage
#
def printUsage():
def printUsage(cfg):
print("usage: {} [-v] [-e endpoint] [-u username] [-p password] [--loglevel debug|info|warning|error] [--logfile <logfile>] [--bucket <bucket_name>] domains".format(cfg["cmd"]))
print("example: {} -e http://hsdshdflab.hdfgroup.org /hdfgroup/data/test/deleteme.h5".format(cfg["cmd"]))
sys.exit()
Expand All @@ -88,6 +86,8 @@ def printUsage():
# Main
#
def main():
cfg = Config()

domains = []
argn = 1
loglevel = logging.ERROR
Expand All @@ -104,7 +104,7 @@ def main():
val = sys.argv[argn+1]

if arg in ("-h", "--help"):
printUsage()
printUsage(cfg)
elif arg in ("-e", "--endpoint"):
cfg["hs_endpoint"] = val
argn += 2
Expand All @@ -131,20 +131,20 @@ def main():
elif val == "ERROR":
loglevel = logging.ERROR
else:
printUsage()
printUsage(cfg)
argn += 2
elif arg == '--logfile':
logfname = val
argn += 2
elif arg[0] == '-':
printUsage()
printUsage(cfg)
else:
domains.append(arg)
argn += 1

if len(domains) == 0:
# need a domain
printUsage()
printUsage(cfg)


logging.basicConfig(filename=logfname, format='%(levelname)s %(asctime)s %(message)s', level=loglevel)
Expand All @@ -154,7 +154,7 @@ def main():
if not domain.startswith('/'):
sys.exit("domain: {} must start with a slash".format(domain))

deleteDomain(domain)
deleteDomain(cfg, domain)


if __name__ == "__main__":
Expand Down
16 changes: 8 additions & 8 deletions h5pyd/_apps/hsdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from .config import Config
from .chunkiter import ChunkIterator

cfg = Config()

def diff_attrs(src, tgt, ctx):
""" compare attributes of src and tgt """
Expand Down Expand Up @@ -360,7 +359,7 @@ def object_diff_helper(name, obj):


#----------------------------------------------------------------------------------
def usage():
def usage(cfg):
print("Usage:\n")
print((" {} [ OPTIONS ] file domain".format(cfg["cmd"])))
print("")
Expand Down Expand Up @@ -397,6 +396,7 @@ def print_config_example():

#----------------------------------------------------------------------------------
def main():
cfg = Config()

loglevel = logging.ERROR
verbose = False
Expand All @@ -419,7 +419,7 @@ def main():
if arg[0] == '-' and len(src_files) > 0:
# options must be placed before filenames
print("options must precead source files")
usage()
usage(cfg)
sys.exit(-1)
if len(sys.argv) > argn + 1:
val = sys.argv[argn+1]
Expand All @@ -446,7 +446,7 @@ def main():
loglevel = logging.ERROR
else:
print("unknown loglevel")
usage()
usage(cfg)
sys.exit(-1)
argn += 2
elif arg == '--logfile':
Expand All @@ -456,7 +456,7 @@ def main():
cfg["hs_bucket"] = val
argn += 2
elif arg in ("-h", "--help"):
usage()
usage(cfg)
sys.exit(0)
elif arg in ("-e", "--endpoint"):
cfg["hs_endpoint"] = val
Expand All @@ -471,7 +471,7 @@ def main():
print_config_example()
sys.exit(0)
elif arg[0] == '-':
usage()
usage(cfg)
sys.exit(-1)
else:
src_files.append(arg)
Expand All @@ -488,7 +488,7 @@ def main():

if len(src_files) < 2:
# need at least a src and destination
usage()
usage(cfg)
sys.exit(-1)
file_path = src_files[0]
domain_path = src_files[1]
Expand All @@ -497,7 +497,7 @@ def main():
logging.info("domain: {}".format(domain_path))
if domain_path[0] != '/' or domain_path[-1] == '/':
print("domain must be an absolute path, non-folder domain")
usage()
usage(cfg)
sys.exit(-1)


Expand Down
Loading