def run(): """ The main cli function. """ # create an argparse instance parser = argparse.ArgumentParser(prog='newslynx/nlynx') parser.add_argument('--no-color', dest='no_color', action="store_true", default=False, help='Disable colored logging.') # add the subparser "container" subparser = parser.add_subparsers(help='Subcommands', dest='cmd') subcommands = setup(subparser) # parse the arguments + options opts, kwargs = parser.parse_known_args() kwargs = parse_runtime_args(kwargs) # run the necessary subcommand if opts.cmd not in subcommands: subcommands echo_error(RuntimeError("No such subcommand."), no_color=opts.no_color) try: subcommands[opts.cmd](opts, **kwargs) except KeyboardInterrupt as e: echo('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)
def run_flush_extract_cache(opts, **kwargs): """ Flush the extraction cache. """ URLCache.flush() ExtractCache.flush() ThumbnailCache.flush() echo('Extraction caches flushed.', no_color=opts.no_color)
def run_flush_comparison_cache(opts, **kwargs): """ Flush the comparison cache. """ ComparisonsCache.flush() echo('Compaison cache flushed.', no_color=opts.no_color)
def run(opts, **kwargs): """ Report the version. """ echo(pkg_resources.get_distribution("newslynx").version, color=Fore.BLUE, no_color=opts.no_color)
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): # create the database try: with app.app_context(): echo('Creating database "{}"'.format(settings.SQLALCHEMY_DATABASE_URI), no_color=opts.no_color) db.configure_mappers() db.create_all() # create the super user u = User.query.filter_by(email=settings.SUPER_USER_EMAIL).first() if not u: echo('Creating super user "{}"'.format(settings.SUPER_USER_EMAIL), no_color=opts.no_color) u = User(name=settings.SUPER_USER, email=settings.SUPER_USER_EMAIL, password=settings.SUPER_USER_PASSWORD, admin=True, super_user=True) # optionally add super user apikey if getattr(settings, 'SUPER_USER_APIKEY', None): u.apikey = settings.SUPER_USER_APIKEY else: echo('Updating super user "{}"'.format(settings.SUPER_USER_EMAIL), no_color=opts.no_color) u.name=settings.SUPER_USER, u.email=settings.SUPER_USER_EMAIL, u.password=settings.SUPER_USER_PASSWORD, u.admin=True super_user=True db.session.add(u) echo('(Re)Loading SQL Extensions', no_color=opts.no_color) # load sql extensions + functions for sql in load_sql(): db.session.execute(sql) # load built-in sous-chefs for sc in load_sous_chefs(): sc = sous_chef_schema.validate(sc) sc_obj = db.session.query(SousChef).filter_by(slug=sc['slug']).first() if not sc_obj: echo('Importing Sous Chef "{}"'.format(sc['slug']), no_color=opts.no_color) sc_obj = SousChef(**sc) else: echo('Updating Sous Chef "{}"'.format(sc['slug']), no_color=opts.no_color) sc = sous_chef_schema.update(sc_obj.to_dict(), sc) # udpate for name, value in sc.items(): setattr(sc_obj, name, value) db.session.add(sc_obj) # commit db.session.commit() db.session.close() except Exception as e: db.session.rollback() db.session.close() raise e