def run(args): """Handle ensure config commandline script.""" parser = argparse.ArgumentParser( description=( "Ensure a Open Peer Power config exists, creates one if necessary." ) ) parser.add_argument( "-c", "--config", metavar="path_to_config_dir", default=config_util.get_default_config_dir(), help="Directory that contains the Open Peer Power configuration", ) parser.add_argument("--script", choices=["ensure_config"]) args = parser.parse_args() config_dir = os.path.join(os.getcwd(), args.config) # Test if configuration directory exists if not os.path.isdir(config_dir): print("Creating directory", config_dir) os.makedirs(config_dir) opp = OpenPeerPower() opp.config.config_dir = config_dir config_path = opp.loop.run_until_complete(async_run(opp)) print("Configuration file:", config_path) return 0
async def async_run(config_dir): """Make sure config exists.""" opp = OpenPeerPower() opp.config.config_dir = config_dir path = await config_util.async_ensure_config_exists(opp) await opp.async_stop(force=True) return path
async def run_command(args): """Run the command.""" opp = OpenPeerPower() opp.config.config_dir = os.path.join(os.getcwd(), args.config) opp.auth = await auth_manager_from_config(opp, [{"type": "openpeerpower"}], []) provider = opp.auth.auth_providers[0] await provider.async_initialize() await args.func(opp, provider, args) # Triggers save on used storage helpers with delay (core auth) logging.getLogger("openpeerpower.core").setLevel(logging.WARNING) await opp.async_stop()
def run(args): """Handle Open Peer Power auth provider script.""" parser = argparse.ArgumentParser( description="Manage Open Peer Power users") parser.add_argument("--script", choices=["auth"]) parser.add_argument( "-c", "--config", default=get_default_config_dir(), help="Directory that contains the Open Peer Power configuration", ) subparsers = parser.add_subparsers(dest="func") subparsers.required = True parser_list = subparsers.add_parser("list") parser_list.set_defaults(func=list_users) parser_add = subparsers.add_parser("add") parser_add.add_argument("username", type=str) parser_add.add_argument("password", type=str) parser_add.set_defaults(func=add_user) parser_validate_login = subparsers.add_parser("validate") parser_validate_login.add_argument("username", type=str) parser_validate_login.add_argument("password", type=str) parser_validate_login.set_defaults(func=validate_login) parser_change_pw = subparsers.add_parser("change_password") parser_change_pw.add_argument("username", type=str) parser_change_pw.add_argument("new_password", type=str) parser_change_pw.set_defaults(func=change_password) args = parser.parse_args(args) loop = asyncio.get_event_loop() opp = OpenPeerPower(loop=loop) loop.run_until_complete(run_command(opp, args)) # Triggers save on used storage helpers with delay (core auth) logging.getLogger("openpeerpower.core").setLevel(logging.WARNING) loop.run_until_complete(opp.async_stop())