File size: 2,810 Bytes
ae01f49 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #!/usr/bin/env python3
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2024, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################
import argparse
import os
import sys
def load_providers():
""" Loads all the providers """
providers = {}
path = os.path.dirname(os.path.realpath(__file__))
modules = os.listdir(path + '/providers')
sys.path.append(path)
for filename in modules:
filename = path + '/providers/' + filename
if os.path.isfile(filename):
basename = os.path.basename(filename)
_, extension = os.path.splitext(basename)
if extension == ".py" and not basename.startswith("_"):
module = __import__("providers." + basename[:-3],
fromlist=["providers"])
provider = module.load()
providers[basename[:-3]] = provider
return providers
def get_args(providers):
""" Creates the parsers and returns the args """
# Create the top-level parser
parser = argparse.ArgumentParser(prog='pgacloud.py')
# Create the provider sub-parser
parsers = parser.add_subparsers(help='provider help', dest='provider')
# Load the provider parsers
for provider in providers:
providers[provider].init_args(parsers)
args = parser.parse_args()
return parser, args
def execute_command(providers, parser, args):
""" Executes the command in the provider """
# Switch - for _ in command names. We use - in the CLI syntax for ease of
# use, but we need an _ in Python function names
if 'command' in args and args.command is not None:
args.command = args.command.replace('-', '_')
# Figure out what provider the command was for (if any) and call the
# relevant function. If we don't get a match, print the help
if args.provider in providers and \
'command' in args and \
args.command is not None:
command = providers[args.provider].commands()[args.command]
command(args)
else:
# If no provider has been given, display the top level help,
# otherwise, call the help() method in the provider
if args.provider is None:
parser.print_help()
else:
command = providers[args.provider].commands()['help']
command()
def main():
""" Entry point """
# Load the providers
providers = load_providers()
# Get the args
parser, args = get_args(providers)
# Execute the command
execute_command(providers, parser, args)
if __name__ == '__main__':
main()
|