From f03c12c69ba15873e99d66519d61e705092cd85e Mon Sep 17 00:00:00 2001 From: Severin Denisenko <81779013+SeverinDenisenko@users.noreply.github.com> Date: Mon, 25 Nov 2024 08:41:42 +0300 Subject: [PATCH] Add simple options to run client from script --- net/client/README | 14 ++++++++++++++ net/client/client.py | 27 +++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/net/client/README b/net/client/README index 9b8f4aa2f..2604fe3c1 100644 --- a/net/client/README +++ b/net/client/README @@ -12,3 +12,17 @@ Example use: ''' python3 client.py -u ../../demo/apod1.jpg -k XXXXXXXX --scale-lower 0.2 --scale-units degwidth --wcs apod1.wcs ''' + +Example use as a library: + +``` +opts = ClientRunnerOptions( + upload="../../demo/apod2.jpg", + apikey="XXXXXXXX", + scale_lower=0.2, + scale_units="degwidth", + newfits="apod2wcs.jpg", +) + +run_client(opts) +``` diff --git a/net/client/client.py b/net/client/client.py index 5d365cd66..53b5e3968 100755 --- a/net/client/client.py +++ b/net/client/client.py @@ -256,6 +256,21 @@ def jobs_by_tag(self, tag, exact): ) return result +class ClientRunnerOptions(object): + def __init__(self, **entries): + self.server = Client.default_url + self.public = 'y' + self.allow_mod = 'd' + self.allow_commercial = 'd' + + self.__dict__.update(entries) + + def __getattr__(self, name): + try: + return object.__getattr__(self, name) + except: + return None + def run_client(opt): args = {} args['apiurl'] = opt.server @@ -398,9 +413,9 @@ def run_client(opt): jobs = c.myjobs() print(jobs) -if __name__ == '__main__': - print("Running with args %s"%sys.argv) +def get_args(): import optparse + parser = optparse.OptionParser() parser.add_option('--server', dest='server', default=Client.default_url, help='Set server base URL (eg, %default)') @@ -467,6 +482,7 @@ def run_client(opt): const='n', default='d', help='Select license to disallow commercial use of submission') + opt,args = parser.parse_args() if opt.apikey is None: @@ -478,4 +494,11 @@ def run_client(opt): print('You must either specify --apikey or set AN_API_KEY') sys.exit(-1) + return opt + +if __name__ == '__main__': + print("Running with args %s"%sys.argv) + + opt = get_args() + run_client(opt)