def run(opts, **kw): from newslynx.sc import sc_exec from newslynx.lib import serialize from newslynx.cli.common import load_data from newslynx.client import API # connect to the api and fetch org kw['apikey'] = opts.apikey kw['api_url'] = opts.apiurl api = API( apikey=opts.apikey, org=opts.org, api_url=opts.apiurl, raise_errors=True) try: kw['org'] = api.orgs.get(opts.org) except: log.warning('Cannot connect to the API. Running in dev mode.') kw['org'] = {'id': opts.org} # parse body file / json string. recipe = load_data(opts.recipe) if recipe: kw.update(recipe) res = sc_exec.run(opts.sous_chef, **kw) if not res: return # stream output if isgenerator(res): for r in res: sys.stdout.write(serialize.obj_to_json(r) + "\n") # stream else: sys.stdout.write(serialize.obj_to_json(res))
def run(opts, **kw): from newslynx.sc import sc_exec from newslynx.lib import serialize from newslynx.cli.common import load_data from newslynx.client import API # connect to the api and fetch org kw['apikey'] = opts.apikey kw['api_url'] = opts.apiurl api = API(apikey=opts.apikey, org=opts.org, api_url=opts.apiurl, raise_errors=True) try: kw['org'] = api.orgs.get(opts.org) except: log.warning('Cannot connect to the API. Running in dev mode.') kw['org'] = {'id': opts.org} # parse body file / json string. recipe = load_data(opts.recipe) if recipe: kw.update(recipe) res = sc_exec.run(opts.sous_chef, **kw) if not res: return # stream output if isgenerator(res): for r in res: sys.stdout.write(serialize.obj_to_json(r) + "\n") # stream else: sys.stdout.write(serialize.obj_to_json(res))
def run(opts, **kwargs): from newslynx.lib import serialize from newslynx.cli.common import load_data # dynamically list collections from newslynx.client import API # connect to the api api = API(apikey=opts.apikey, org=opts.org, api_url=opts.apiurl, raise_errors=opts.raise_errors) # get the collection cobj = None if opts.collection: cobj = getattr(api, opts.collection.replace('-', '_'), None) if not cobj: # report options collections = [c.replace('_', "-") for c in dir(api) if _keep(c)] log.error("Collection '{}' does not exist.".format(opts.collection)) log.warning("Choose from the following collections:\n\t- {}".format( opts.collection, "\n\t- {}".join(collections))) sys.exit(1) # get the method mobj = None if opts.method: mobj = getattr(cobj, opts.method.replace('-', '_'), None) if not mobj: # report options if opts.method != 'ls': log.warning( "Method '{}' does not exist for collection '{}'".format( opts.method, opts.collection)) # compute the tree here to save on processing time. options = [m.replace('_', '-') for m in dir(cobj) if _keep(m)] # list of methods for this collection msg = "choose from the following methods:\n\t- {}"\ .format("\n\t- ".join(options)) log.warning("\n/{}\n".format(opts.collection) + msg) sys.exit(0) # parse body file / json string. d = load_data(opts.data) if d: kwargs.update(d) # execute method try: res = mobj(**kwargs) except KeyboardInterrupt as e: log.warning("\nInterrupted by user. Exiting...\n") sys.exit(2) # interrupt except Exception as e: log.error(format_exc()) sys.exit(1) # stream output if isgenerator(res): for r in res: sys.stdout.write(serialize.obj_to_json(r) + "\n") # stream else: sys.stdout.write(serialize.obj_to_json(res)) sys.stdout.write("\n") sys.exit(0)
def run(opts, **kwargs): # connect to the api api = API( apikey=opts.apikey, org=opts.org, api_url=opts.api_url, raise_errors=opts.raise_errors) # get the collection cobj = getattr(api, opts.collection, None) if not cobj: e = RuntimeError("Error: Collection '{}' does not exist." .format(opts.collection)) echo_error(e) echo("Choose from the following collections:\n\t- {}" .format(opts.collection, "\n\t- {}".join(COLLECTIONS)), color = fore.WHITE) sys.exit(1) # allow for `-` instead of `_`: if opts.method: opts.method = opts.method.replace('-', "_") mobj = getattr(cobj, opts.method, None) if not mobj: options = CMD_TREE[opts.collection] if opts.method != 'ls': e = RuntimeError("Method '{}' does not exist for collection '{}'" .format(opts.method, opts.collection)) echo_error(e, no_color=opts.no_color) else: echo("/{}".format(opts.collection), color=Fore.BLUE, no_color=opts.no_color) msg = "choose from the following methods:\n\t- {}"\ .format( "\n\t- ".join(options)) echo(msg, color=Fore.YELLOW, no_color=opts.no_color) sys.exit(1) # parse body file / json string. kwargs.update(load_data(opts.data, opts)) # execute method try: res = mobj(**kwargs) except KeyboardInterrupt as e: echo_error("Interrupted by user. Exiting.", color=Fore.YELLOW, no_color=opts.no_color) sys.exit(2) # interrupt except Exception as e: tb = format_exc() echo_error(e, tb, no_color=opts.no_color) sys.exit(1) # stream output if isgenerator(res): for r in res: sys.stdout.write(serialize.obj_to_json(r) +"\n") # stream else: sys.stdout.write(serialize.obj_to_json(res)) sys.exit(0)
def run(opts, **kwargs): from newslynx.lib import serialize from newslynx.cli.common import load_data # dynamically list collections from newslynx.client import API # connect to the api api = API( apikey=opts.apikey, org=opts.org, api_url=opts.apiurl, raise_errors=opts.raise_errors) # get the collection cobj = None if opts.collection: cobj = getattr(api, opts.collection.replace('-', '_'), None) if not cobj: # report options collections = [c.replace('_', "-") for c in dir(api) if _keep(c)] log.error("Collection '{}' does not exist." .format(opts.collection)) log.warning("Choose from the following collections:\n\t- {}" .format(opts.collection, "\n\t- {}".join(collections))) sys.exit(1) # get the method mobj = None if opts.method: mobj = getattr(cobj, opts.method.replace('-', '_'), None) if not mobj: # report options if opts.method != 'ls': log.warning("Method '{}' does not exist for collection '{}'" .format(opts.method, opts.collection)) # compute the tree here to save on processing time. options = [m.replace('_', '-') for m in dir(cobj) if _keep(m)] # list of methods for this collection msg = "choose from the following methods:\n\t- {}"\ .format("\n\t- ".join(options)) log.warning("\n/{}\n".format(opts.collection) + msg) sys.exit(0) # parse body file / json string. d = load_data(opts.data) if d: kwargs.update(d) # execute method try: res = mobj(**kwargs) except KeyboardInterrupt as e: log.warning("\nInterrupted by user. Exiting...\n") sys.exit(2) # interrupt except Exception as e: log.error(format_exc()) sys.exit(1) # stream output if isgenerator(res): for r in res: sys.stdout.write(serialize.obj_to_json(r) + "\n") # stream else: sys.stdout.write(serialize.obj_to_json(res)) sys.stdout.write("\n") sys.exit(0)