From dbf0f00650200b8b4817274953f2e0b684bd622d Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Tue, 30 Jun 2020 12:51:51 +0200 Subject: Rename files to remove 'og' prefix This prefix is redundant on almost every file, so the prefix is removed from filenames and imports. --- cli/__init__.py | 1 + cli/cli.py | 39 +++++++++++++++++++++++++++++++++++++++ cli/objects/__init__.py | 0 cli/objects/client.py | 6 ++++++ cli/objects/modes.py | 6 ++++++ cli/objects/scopes.py | 6 ++++++ ogcli | 41 +++++++++++++++++++++++++++++++++++++++++ ogcli.py | 41 ----------------------------------------- ogcli/__init__.py | 1 - ogcli/objects/__init__.py | 0 ogcli/objects/modes.py | 6 ------ ogcli/objects/og_client.py | 6 ------ ogcli/objects/og_scopes.py | 6 ------ ogcli/ogcli.py | 39 --------------------------------------- 14 files changed, 99 insertions(+), 99 deletions(-) create mode 100644 cli/__init__.py create mode 100644 cli/cli.py create mode 100644 cli/objects/__init__.py create mode 100644 cli/objects/client.py create mode 100644 cli/objects/modes.py create mode 100644 cli/objects/scopes.py create mode 100755 ogcli delete mode 100755 ogcli.py delete mode 100644 ogcli/__init__.py delete mode 100644 ogcli/objects/__init__.py delete mode 100644 ogcli/objects/modes.py delete mode 100644 ogcli/objects/og_client.py delete mode 100644 ogcli/objects/og_scopes.py delete mode 100644 ogcli/ogcli.py diff --git a/cli/__init__.py b/cli/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/cli/__init__.py @@ -0,0 +1 @@ + diff --git a/cli/cli.py b/cli/cli.py new file mode 100644 index 0000000..30c1bb6 --- /dev/null +++ b/cli/cli.py @@ -0,0 +1,39 @@ +from cli.objects.client import OgClient +from cli.objects.scopes import OgScope +from cli.objects.modes import OgModes +import argparse +import requests +import sys + +class OgREST(): + def __init__(self, ip, port, api_token): + self.URL = f'http://{ip}:{port}' + self.HEADERS = {'Authorization' : api_token} + + def get(self, path): + try: + r = requests.get(f'{self.URL}{path}', + headers=self.HEADERS) + if r.status_code != 200: + sys.exit(f"Cannot connect to ogServer: " + f"{r.status_code} HTTP status code") + except IOError as e: + sys.exit(f"Cannot connect to ogServer: {e}") + return r + +class OgCLI(): + def __init__(self, cfg): + self.rest = OgREST(cfg['ip'], cfg['port'], cfg['api_token']) + + def list(self, args): + choices = ['clients', 'scopes', 'modes'] + parser = argparse.ArgumentParser() + parser.add_argument('item', choices=choices) + args = parser.parse_args(args) + + if args.item == 'clients': + OgClient.list_clients(self.rest) + elif args.item == 'modes': + OgModes.list_available_modes(self.rest) + elif args.item == 'scopes': + OgScope.list_scopes(self.rest) diff --git a/cli/objects/__init__.py b/cli/objects/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cli/objects/client.py b/cli/objects/client.py new file mode 100644 index 0000000..ca91036 --- /dev/null +++ b/cli/objects/client.py @@ -0,0 +1,6 @@ +class OgClient(): + + @staticmethod + def list_clients(rest): + r = rest.get('/clients') + print(r.json()) diff --git a/cli/objects/modes.py b/cli/objects/modes.py new file mode 100644 index 0000000..bf175d6 --- /dev/null +++ b/cli/objects/modes.py @@ -0,0 +1,6 @@ +class OgModes(): + + @staticmethod + def list_available_modes(rest): + r = rest.get('/modes') + print(r.json()) diff --git a/cli/objects/scopes.py b/cli/objects/scopes.py new file mode 100644 index 0000000..0bf3ca8 --- /dev/null +++ b/cli/objects/scopes.py @@ -0,0 +1,6 @@ +class OgScope(): + + @staticmethod + def list_scopes(rest): + r = rest.get('/scopes') + print(r.json()) diff --git a/ogcli b/ogcli new file mode 100755 index 0000000..33d740a --- /dev/null +++ b/ogcli @@ -0,0 +1,41 @@ +#!/usr/bin/python + +from cli.cli import OgCLI +import argparse +import json +import sys + +OG_CLI_CFG_PATH="/opt/opengnsys/etc/ogcli.json" + +class CLI(): + def __init__(self): + try: + with open(OG_CLI_CFG_PATH, 'r') as json_file: + self.cfg = json.load(json_file) + except json.JSONDecodeError: + sys.exit(f'ERROR: Failed parse malformed JSON file ' + f'{OG_CLI_CFG_PATH}') + except: + sys.exit(f'ERROR: cannot open {OG_CLI_CFG_PATH}') + + required_cfg_params = {'api_token', 'ip', 'port'} + difference_cfg_params = required_cfg_params - self.cfg.keys() + if len(difference_cfg_params) > 0: + sys.exit(f'Missing {difference_cfg_params} key in ' + f'json config file') + + self.ogcli = OgCLI(self.cfg) + + parser = argparse.ArgumentParser(prog='ogcli') + parser.add_argument('command', help='Subcommand to run') + args = parser.parse_args(sys.argv[1:2]) + + if not hasattr(self.ogcli, args.command): + parser.print_help() + sys.exit('Unknown command') + + # Call the command with the same name. + getattr(self.ogcli, args.command)(sys.argv[2:]) + +if __name__ == "__main__": + CLI() diff --git a/ogcli.py b/ogcli.py deleted file mode 100755 index 4672a3a..0000000 --- a/ogcli.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/python - -from ogcli.ogcli import OgCLI -import argparse -import json -import sys - -OG_CLI_CFG_PATH="/opt/opengnsys/etc/ogcli.json" - -class CLI(): - def __init__(self): - try: - with open(OG_CLI_CFG_PATH, 'r') as json_file: - self.cfg = json.load(json_file) - except json.JSONDecodeError: - sys.exit(f'ERROR: Failed parse malformed JSON file ' - f'{OG_CLI_CFG_PATH}') - except: - sys.exit(f'ERROR: cannot open {OG_CLI_CFG_PATH}') - - required_cfg_params = {'api_token', 'ip', 'port'} - difference_cfg_params = required_cfg_params - self.cfg.keys() - if len(difference_cfg_params) > 0: - sys.exit(f'Missing {difference_cfg_params} key in ' - f'json config file') - - self.ogcli = OgCLI(self.cfg) - - parser = argparse.ArgumentParser(prog='ogcli') - parser.add_argument('command', help='Subcommand to run') - args = parser.parse_args(sys.argv[1:2]) - - if not hasattr(self.ogcli, args.command): - parser.print_help() - sys.exit('Unknown command') - - # Call the command with the same name. - getattr(self.ogcli, args.command)(sys.argv[2:]) - -if __name__ == "__main__": - CLI() diff --git a/ogcli/__init__.py b/ogcli/__init__.py deleted file mode 100644 index 8b13789..0000000 --- a/ogcli/__init__.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/ogcli/objects/__init__.py b/ogcli/objects/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/ogcli/objects/modes.py b/ogcli/objects/modes.py deleted file mode 100644 index bf175d6..0000000 --- a/ogcli/objects/modes.py +++ /dev/null @@ -1,6 +0,0 @@ -class OgModes(): - - @staticmethod - def list_available_modes(rest): - r = rest.get('/modes') - print(r.json()) diff --git a/ogcli/objects/og_client.py b/ogcli/objects/og_client.py deleted file mode 100644 index ca91036..0000000 --- a/ogcli/objects/og_client.py +++ /dev/null @@ -1,6 +0,0 @@ -class OgClient(): - - @staticmethod - def list_clients(rest): - r = rest.get('/clients') - print(r.json()) diff --git a/ogcli/objects/og_scopes.py b/ogcli/objects/og_scopes.py deleted file mode 100644 index 0bf3ca8..0000000 --- a/ogcli/objects/og_scopes.py +++ /dev/null @@ -1,6 +0,0 @@ -class OgScope(): - - @staticmethod - def list_scopes(rest): - r = rest.get('/scopes') - print(r.json()) diff --git a/ogcli/ogcli.py b/ogcli/ogcli.py deleted file mode 100644 index d8e66b7..0000000 --- a/ogcli/ogcli.py +++ /dev/null @@ -1,39 +0,0 @@ -from ogcli.objects.og_client import OgClient -from ogcli.objects.og_scopes import OgScope -from ogcli.objects.modes import OgModes -import argparse -import requests -import sys - -class OgREST(): - def __init__(self, ip, port, api_token): - self.URL = f'http://{ip}:{port}' - self.HEADERS = {'Authorization' : api_token} - - def get(self, path): - try: - r = requests.get(f'{self.URL}{path}', - headers=self.HEADERS) - if r.status_code != 200: - sys.exit(f"Cannot connect to ogServer: " - f"{r.status_code} HTTP status code") - except IOError as e: - sys.exit(f"Cannot connect to ogServer: {e}") - return r - -class OgCLI(): - def __init__(self, cfg): - self.rest = OgREST(cfg['ip'], cfg['port'], cfg['api_token']) - - def list(self, args): - choices = ['clients', 'scopes', 'modes'] - parser = argparse.ArgumentParser() - parser.add_argument('item', choices=choices) - args = parser.parse_args(args) - - if args.item == 'clients': - OgClient.list_clients(self.rest) - elif args.item == 'modes': - OgModes.list_available_modes(self.rest) - elif args.item == 'scopes': - OgScope.list_scopes(self.rest) -- cgit v1.2.3-18-g5258