def list_users(ctx, token): """list users """ db = get_mailadm_db(ctx) with db.read_connection() as conn: for user_info in conn.get_user_list(token=token): click.secho("{} [token={}]".format(user_info.addr, user_info.token_name))
def list_tokens(ctx): """list available tokens """ db = get_mailadm_db(ctx) with db.read_connection() as conn: for name in conn.get_token_list(): token_info = conn.get_tokeninfo_by_name(name) dump_token_info(token_info)
def config(ctx): """show and manipulate config settings. """ db = get_mailadm_db(ctx) with db.read_connection() as conn: click.secho("** mailadm version: {}".format(mailadm.__version__)) click.secho("** mailadm database path: {}".format(db.path)) for name, val in conn.get_config_items(): click.secho("{:22s} {}".format(name, val))
def gen_qr(ctx, tokenname): """generate qr code image for a token. """ from .gen_qr import gen_qr db = get_mailadm_db(ctx) with db.read_connection() as conn: token_info = conn.get_tokeninfo_by_name(tokenname) config = conn.config if token_info is None: ctx.fail("token {!r} does not exist".format(tokenname)) image = gen_qr(config, token_info) fn = "dcaccount-{domain}-{name}.png".format(domain=config.mail_domain, name=token_info.name) image.save(fn) click.secho("{} written for token '{}'".format(fn, token_info.name))
def get_mailadm_db(ctx, show=False, fail_missing_config=True): try: db_path = mailadm.db.get_db_path() except RuntimeError as e: ctx.fail(e.args) try: db = mailadm.db.DB(db_path) except DBError as e: ctx.fail(str(e)) if show: click.secho("using db: {}".format(db_path), file=sys.stderr) if fail_missing_config: with db.read_connection() as conn: if not conn.is_initialized(): ctx.fail( "database not initialized, use 'init' subcommand to do so") return db