175 lines
5.4 KiB
Python
175 lines
5.4 KiB
Python
import argparse
|
|
from AuthCDS import AuthCDS
|
|
|
|
# Version of the library
|
|
__version__ = "1.0.0"
|
|
|
|
# Standalone function for authentication from Jupyter
|
|
|
|
def cds_help():
|
|
"""Provide instructions on how to use methods programmatically from Jupyter."""
|
|
help_message = '''
|
|
CDS Authentication Helper - Jupyter Usage Guide
|
|
Version: %s
|
|
|
|
Available Functions:
|
|
---------------------
|
|
- cds_authenticate(verbose=True): Perform authentication and return CDS client.
|
|
- cds_remove_conf(verbose=True): Remove saved configurations from default paths.
|
|
- cds_save_conf(config=None, verbose=True): Save configuration to the default path.
|
|
- cds_remove_env(verbose=True): Remove environment-based credentials for CDS.
|
|
- cds_show_conf(verbose=True): Display current configuration from environment and files.
|
|
|
|
Usage Examples:
|
|
---------------
|
|
1. Authenticate and get CDS client:
|
|
client = cds_authenticate()
|
|
|
|
2. Remove configurations from default paths:
|
|
cds_remove_conf()
|
|
|
|
3. Save the current or new configuration:
|
|
cds_save_conf()
|
|
|
|
4. Remove environment variables for CDS API:
|
|
cds_remove_env()
|
|
|
|
5. Show current configuration from environment and files:
|
|
cds_show_conf()
|
|
|
|
''' % __version__
|
|
print(help_message)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description="""Authenticate with CDS and configure .cdsapirc
|
|
Version: {__version__}""",
|
|
add_help=False
|
|
)
|
|
parser.add_argument(
|
|
"-v", "--verbose",
|
|
action="store_true",
|
|
help="Enable verbose mode for detailed logging"
|
|
)
|
|
parser.add_argument(
|
|
"-a", "--authenticate",
|
|
action="store_true",
|
|
help="Run authentication and set environment variables"
|
|
)
|
|
parser.add_argument(
|
|
"-s", "--save-config",
|
|
metavar="PATH",
|
|
type=str,
|
|
help="Save configuration to a specific file path (default: ~/.cdsapirc)"
|
|
)
|
|
parser.add_argument(
|
|
"-r", "--remove-config",
|
|
action="store_true",
|
|
help="Remove saved configurations from default paths"
|
|
)
|
|
parser.add_argument(
|
|
"-q", "--query-credentials",
|
|
action="store_true",
|
|
help="Prompt user to re-enter credentials and optionally save them"
|
|
)
|
|
parser.add_argument(
|
|
"-p", "--print-config",
|
|
action="store_true",
|
|
help="Print current configuration from environment and files"
|
|
)
|
|
parser.add_argument(
|
|
"-h", "--help",
|
|
action="store_true",
|
|
help="Display help information with usage examples"
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
if not any(vars(args).values()):
|
|
print("CDS Authentication CLI")
|
|
print("Version: %s" % __version__)
|
|
print("----------------------")
|
|
print("Usage:")
|
|
print(" python auth_cds.py [OPTIONS]")
|
|
print("Options:")
|
|
print(" -v, --verbose Enable verbose mode for detailed logging")
|
|
print(" -a, --authenticate Run authentication and set environment variables")
|
|
print(" -s, --save-config PATH Save configuration to a specific file path")
|
|
print(" -r, --remove-config Remove saved configurations from default paths")
|
|
print(" -q, --query-credentials Prompt user to re-enter credentials")
|
|
print(" -p, --print-config Print current configuration from environment and files")
|
|
print(" -h, --help Display this help information with usage examples")
|
|
print("Run with -h or --help to see examples and additional information.")
|
|
return
|
|
|
|
if args.help:
|
|
cds_help()
|
|
return
|
|
|
|
auth = AuthCDS(verbose=args.verbose)
|
|
|
|
if args.authenticate:
|
|
client = auth.authenticate()
|
|
if client:
|
|
auth.log("Authenticated successfully")
|
|
else:
|
|
auth.log("Authentication failed")
|
|
|
|
if args.save_config:
|
|
config = auth.config_from_env() or auth.config_from_file()
|
|
if config:
|
|
auth.config_to_file(config, config_path=args.save_config)
|
|
auth.log(f"Configuration saved to {args.save_config}")
|
|
else:
|
|
auth.log("No configuration found to save")
|
|
|
|
if args.remove_config:
|
|
removed_paths = auth.remove_conf_credentials()
|
|
auth.log(f"Removed configurations from paths: {removed_paths}")
|
|
|
|
if args.query_credentials:
|
|
auth.query_credentials()
|
|
auth.log("Credentials queried and optionally saved")
|
|
|
|
if args.print_config:
|
|
env_config, file_config = auth.show_credentials()
|
|
auth.log(f"Environment Config: {env_config}", force=True)
|
|
auth.log(f"File Config: {file_config}", force=True)
|
|
|
|
def cds_authenticate(verbose=True):
|
|
"""Perform authentication and return the CDS client if successful."""
|
|
auth = AuthCDS(verbose=verbose)
|
|
conf = auth.authenticate()
|
|
return conf
|
|
|
|
def cds_get_client(verbose=True):
|
|
auth = AuthCDS(verbose=verbose)
|
|
return auth.get_authenticated_client()
|
|
|
|
def cds_remove_conf(verbose=True):
|
|
auth = AuthCDS(verbose=verbose)
|
|
auth.remove_conf_credentials()
|
|
|
|
|
|
def cds_save_conf(config=None, verbose=True):
|
|
auth = AuthCDS(verbose=verbose)
|
|
if config is None:
|
|
config = auth.authenticate()
|
|
if config is not None:
|
|
auth.config_to_file(config)
|
|
|
|
|
|
def cds_remove_env(verbose=True):
|
|
auth = AuthCDS(verbose=verbose)
|
|
auth.remove_env_credentials()
|
|
|
|
|
|
def cds_show_conf(verbose=True):
|
|
auth = AuthCDS(verbose=verbose)
|
|
auth.show_credentials()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |