Skip to content

Commit

Permalink
First version of main help page and bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrakhtman committed Feb 7, 2020
1 parent 8e283d7 commit 3da24d7
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 14 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ History
------------------

* First release on PyPI.

1.0.7 (2020-02-07)
------------------

* First version of main help page and bug fixes.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ By default, `alcli` uses ~/.alertlogic/config configuration file in a user's hom

[default]
access_key_id=1111111111111111
secret_key=eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
secret_key=eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
global_endpoint=integration

[production]
Expand Down
2 changes: 1 addition & 1 deletion alcli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '1.0.6'
__version__ = '1.0.7'
__author__ = 'Alert Logic, Inc.'
34 changes: 25 additions & 9 deletions alcli/alertlogic_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import argparse
import pydoc

from urllib.parse import urlparse
from collections import OrderedDict
from pydoc import pager

Expand All @@ -24,6 +25,7 @@

from alcli.cliparser import ALCliArgsParser
from alcli.cliparser import USAGE
from alcli.clihelp import ALCliMainHelpFormatter
from alcli.clihelp import ALCliServiceHelpFormatter
from alcli.clihelp import ALCliOperationHelpFormatter
from alcli import __version__ as alcli_version
Expand Down Expand Up @@ -62,13 +64,16 @@ def main(self, args=None):
logger.debug(f"Parsed Arguments: {parsed_args}, Remaining: {remaining}")

if parsed_args.service == 'help' or parsed_args.service is None:
sys.stderr.write(f"usage: {USAGE}\n")
help_formatter = ALCliMainHelpFormatter(services.keys())
self._show_help(help_formatter)
return 128
#sys.stderr.write(f"usage: {USAGE}\n")
#return 128

if parsed_args.operation == 'help':
help_page = ALCliServiceHelpFormatter(self._services[parsed_args.service])
pydoc.pipepager(help_page.format_page() + '\n', 'less -R')
return 0
help_formatter = ALCliServiceHelpFormatter(self._services[parsed_args.service])
self._show_help(help_formatter)
return 128

if hasattr(parsed_args, 'help') and \
hasattr(parsed_args, 'service') and \
Expand All @@ -80,11 +85,12 @@ def main(self, args=None):
return 0


try:
return services[parsed_args.service](remaining, parsed_args)
except Exception as e:
sys.stderr.write(f"Caught exception in main(). Error: {str(e)}\n")
return 255
return services[parsed_args.service](remaining, parsed_args)
#try:
# return services[parsed_args.service](remaining, parsed_args)
#except Exception as e:
# sys.stderr.write(f"Caught exception in main(). Error: {str(e)}\n")
# return 255

def _get_services(self):
if self._services is None:
Expand Down Expand Up @@ -114,6 +120,10 @@ def _create_parser(self, services):
parser.add_argument('--debug', dest='debug', default=False, action="store_true")
return parser

def _show_help(self, help_formatter):
pydoc.pipepager(help_formatter.format_page() + '\n', 'less -R')
return 0

class ServiceOperation(object):
"""
A service operation. For example: alcli aetuner would create
Expand Down Expand Up @@ -183,6 +193,12 @@ def _init_service(self, parsed_globals):
profile=parsed_globals.profile)

def _encode(self, operation, param_name, param_value):
p = urlparse(param_value)
if p.scheme == "file":
value_file_path = os.path.abspath(os.path.join(p.netloc, p.path))
with open (value_file_path, "r") as value_file:
param_value = value_file.read()

schema = operation.get_schema()
parameter = schema[OpenAPIKeyWord.PARAMETERS][param_name]
type = parameter[OpenAPIKeyWord.TYPE]
Expand Down
46 changes: 46 additions & 0 deletions alcli/clihelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,26 @@ class ALCliHelpFormatter():
def __init__(self,
width=80,
indent_increment=2):
self._name = ""
self._width = width
self._indent_increment = indent_increment
self._initial_indent = '\t'
self._subsequent_indent = '\t'

@property
def name(self):
return self._name

@name.setter
def name(self, name):
self._name = name

def bold(self, msg):
return f"{FormatHelp.BOLD}{msg}{FormatHelp.END}"

def underline(self, msg):
return f"{FormatHelp.UNDERLINE}{msg}{FormatHelp.END}"

def wraptext(self, text):
if text is None:
return ""
Expand Down Expand Up @@ -60,6 +72,40 @@ def make_description(self, description):
result.extend(self.wraptext(description))
return '\n'.join(result) + '\n'

class ALCliMainHelpFormatter(ALCliHelpFormatter):
def __init__(self,
servcices,
width=80,
indent_increment=2
):
super().__init__(width, indent_increment)
self._services = servcices
self._name = "alcli"
self._description = "The Alert Logic Command Line Interface is a tool to help you manage Alert Logic Services."

def make_synopsis(self):
command = "\talcli [options] <command> <subcommand> [parameters]"
description = f"Use {self.underline('alcli')} {self.underline('command')} {self.underline('help')} for information on a specific command."
return self.bold("SYNOPSIS") + '\n' + '\n\t'.join([command, description]) + '\n'


def make_services(self, services):
available_services = [
'\to ' + service
for service in services
]
return self.bold("AVAIABLE SERVICES") + '\n' + '\n\n'.join(available_services)

def format_page(self):
page = []
page.append(self.make_header(self._name))
page.append(self.make_description(self._description))
page.append(self.make_synopsis())
page.append(self.make_services(self._services))
page.append(self.make_footer(self._name))
return '\n'.join(page)


class ALCliServiceHelpFormatter(ALCliHelpFormatter):
def __init__(self,
service,
Expand Down
3 changes: 0 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
version = str(ast.literal_eval(_version_re.search(
f.read().decode('utf-8')).group(1)))

with open('HISTORY.rst') as history_file:
history = history_file.read()

with open('README.md') as readme_file:
readme = readme_file.read()

Expand Down

0 comments on commit 3da24d7

Please sign in to comment.