Last active
November 3, 2021 14:18
-
-
Save Nuttymoon/1a3929eb543cff8387124893f4d57cc5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import argparse | |
import sys | |
import configparser | |
DATABRICKSCFG = '~/.databrickscfg' | |
def parse_args(): | |
"""Parse command line arguments | |
Returns: | |
Namespace: argparse Namespace containing parsed arguments | |
""" | |
parser = argparse.ArgumentParser( | |
description='Read configuration properties from databrickscfg') | |
parser.add_argument( | |
'action', type=str, choices=['list', 'show'], | |
help='The action to perform. Either "list" all the available profiles ' | |
'or "show" a property for one profile.') | |
parser.add_argument('-p', '--profile', type=str, required=('show' in sys.argv), | |
help='The Databricks workspace profile in the config') | |
parser.add_argument('property', type=str, choices=['host', 'token'], nargs='?', | |
default='token', help='The profile property to read') | |
return parser.parse_args() | |
def read_config(databrickscfg): | |
conf = configparser.ConfigParser() | |
conf.read(os.path.expanduser(databrickscfg)) | |
return conf | |
if __name__ == '__main__': | |
args = parse_args() | |
conf = read_config(DATABRICKSCFG) | |
if args.action == 'list': | |
print('\n'.join(conf.sections())) | |
elif args.action == 'show': | |
print(conf[args.profile][args.property]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment