| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| 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 """ |
| |
| parser = argparse.ArgumentParser(prog='pgacloud.py') |
|
|
| |
| parsers = parser.add_subparsers(help='provider help', dest='provider') |
|
|
| |
| 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 """ |
|
|
| |
| |
| if 'command' in args and args.command is not None: |
| args.command = args.command.replace('-', '_') |
|
|
| |
| |
| 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 args.provider is None: |
| parser.print_help() |
| else: |
| command = providers[args.provider].commands()['help'] |
| command() |
|
|
|
|
| def main(): |
| """ Entry point """ |
| |
| providers = load_providers() |
|
|
| |
| parser, args = get_args(providers) |
|
|
| |
| execute_command(providers, parser, args) |
|
|
|
|
| if __name__ == '__main__': |
| main() |
|
|