diff --git a/src/mpapi/__init__.py b/src/mpapi/__init__.py index dd2ef53..f90e10c 100644 --- a/src/mpapi/__init__.py +++ b/src/mpapi/__init__.py @@ -1,6 +1,6 @@ """An Unofficial Open Source Client for the MuseumPlus API""" -__version__ = "0.1.8" # cleanup, Vocabulary, validate cli +__version__ = "0.1.9" # replace pyexiv, cleanup __init__ import argparse from getAttachments import GetAttachments from mpapi.mink import Mink @@ -9,8 +9,23 @@ from mpapi.constants import get_credentials from mpapi.validate import Validate from pathlib import Path +import sys -user, pw, baseURL = get_credentials() + +def _login() -> MpApi: + user, pw, baseURL = get_credentials() + c = MpApi(baseURL=baseURL, pw=pw, user=user) + + +def _setup_args(parser): + parser.add_argument( + "-v", "--version", help="Display version information", action="store_true" + ) + args = parser.parse_args() + if args.version: + print(f"Version: {__version__}") + sys.exit(0) + return args def getDefinition(): @@ -21,14 +36,9 @@ def getDefinition(): help="module type (Object, Multimedia etc.); optional", default=None, ) - parser.add_argument("-v", "--version", help="Display version information") - args = parser.parse_args() + args = _setup_args(parser) + c = _login() - if args.version: - print(f"Version: {__version__}") - sys.exit(0) - - c = MpApi(baseURL=baseURL, pw=pw, user=user) print(f"Logging in as '{user}'") m = c.getDefinition2(mtype=args.mtype) if args.mtype is None: @@ -50,15 +60,9 @@ def getItem(): "-u", "--upload", help="Save in upload form", action="store_true" ) parser.add_argument("-i", "--ID", help="ID") - parser.add_argument("-v", "--version", help="Display version information") - args = parser.parse_args() - - if args.version: - print(f"Version: {__version__}") - sys.exit(0) + args = _setup_args(parser) - c = MpApi(baseURL=baseURL, pw=pw, user=user) - print(f"Logging in as '{user}'") + c = _login() m = c.getItem2(mtype=args.mtype, ID=args.ID) if args.upload: fn = f"getItem-{args.mtype}{args.ID}u.xml" @@ -79,7 +83,8 @@ def getAttachments(): parser.add_argument( "-j", "--job", required=True, help="pick a job from getAttachments.jobs file" ) - args = parser.parse_args() + args = _setup_args(parser) + GetAttachments(baseURL=baseURL, job=args.job, user=user, pw=pw) @@ -87,11 +92,8 @@ def mink(): parser = argparse.ArgumentParser(description="Commandline frontend for MpApi.py") parser.add_argument("-j", "--job", help="job to run") # , required=True parser.add_argument("-c", "--conf", help="config file", default="jobs.dsl") - parser.add_argument("-v", "--version", help="Display version information") - args = parser.parse_args() - if args.version: - print(f"Version: {__version__}") - sys.exit(0) + args = _setup_args(parser) + m = Mink(job=args.job, conf=args.conf, baseURL=baseURL, pw=pw, user=user) @@ -100,24 +102,18 @@ def updateItem(): parser.add_argument("-f", "--file", help="File location to upload") parser.add_argument("-i", "--id", help="moduleItem/@id") parser.add_argument("-t", "--mtype", help="module type") - parser.add_argument("-v", "--version", help="Display version information") - args = parser.parse_args() + args = _setup_args(parser) # todowe could extract module type and id from the record required_args = ["file", "id", "mtype"] - if args.version: - print(f"Version: {__version__}") - sys.exit(0) - for req in required_args: if not req in args: raise SyntaxError("Required args not provided") - args = parser.parse_args() m = Module(file=args.file) - c = MpApi(baseURL=baseURL, pw=pw, user=user) + c = _login() m = c.uploadItem2(mtype=args.mtype, ID=args.ID) @@ -126,12 +122,8 @@ def validate(): parser.add_argument( "-m", "--mode", help="mode (module, search or vocabulary)", default="module" ) - parser.add_argument("-v", "--version", help="Display version information") parser.add_argument("file", nargs="?") - args = parser.parse_args() - if args.version: - print(f"Version: {__version__}") - sys.exit(0) + args = _setup_args(parser) if args.file is None: raise SyntaxError("ERROR: Required arg 'file' not provided!")