source: ogCli-Git/ogcli

Last change on this file was d90ab82, checked in by Jose M. Guisado <jguisado@…>, 2 years ago

format: use autopep8

Use autopep8 for coding format, and only for whitespace changes. This
change drops use of tabs in favor of spaces.

Doesn't use autopep8 --aggresive option.

Format command:

$ autopep8 --inline --recursive .

When using git-blame, use --ignore-rev in order to ignore this
reformatting commit.

  • Property mode set to 100755
File size: 1.8 KB
Line 
1#!/usr/bin/env python3
2
3# Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
4#
5# This program is free software: you can redistribute it and/or modify it under
6# the terms of the GNU Affero General Public License as published by the
7# Free Software Foundation; either version 3 of the License, or
8# (at your option) any later version.
9
10from inspect import ismethod, getmembers
11
12from cli.cli import OgCLI
13import argparse
14import json
15import sys
16
17OG_CLI_CFG_PATH = "/opt/opengnsys/etc/ogcli.json"
18
19
20class CLI():
21    def __init__(self):
22        try:
23            with open(OG_CLI_CFG_PATH, 'r') as json_file:
24                self.cfg = json.load(json_file)
25        except json.JSONDecodeError:
26            sys.exit(f'ERROR: Failed parse malformed JSON file '
27                     f'{OG_CLI_CFG_PATH}')
28        except:
29            sys.exit(f'ERROR: cannot open {OG_CLI_CFG_PATH}')
30
31        required_cfg_params = {'api_token', 'ip', 'port'}
32        difference_cfg_params = required_cfg_params - self.cfg.keys()
33        if len(difference_cfg_params) > 0:
34            sys.exit(f'Missing {difference_cfg_params} key in '
35                     f'json config file')
36
37        self.ogcli = OgCLI(self.cfg)
38
39        parser = argparse.ArgumentParser(prog='ogcli')
40        parser.add_argument('command', help='Subcommand to run', nargs='?',
41                            choices=[attr for attr, _ in getmembers(self.ogcli, lambda x: ismethod(x))
42                                     if not attr.startswith('_')])
43        args = parser.parse_args(sys.argv[1:2])
44
45        if not hasattr(self.ogcli, args.command):
46            parser.print_help()
47            sys.exit('Unknown command')
48
49        # Call the command with the same name.
50        getattr(self.ogcli, args.command)(sys.argv[2:])
51
52
53if __name__ == "__main__":
54    CLI()
Note: See TracBrowser for help on using the repository browser.