Skip to content
Snippets Groups Projects
Commit 78c3ddd1 authored by Nathan Hertz's avatar Nathan Hertz
Browse files

Removed pymygdala integration; made logging self-contained; added two more functions

parent c9677766
No related branches found
No related tags found
No related merge requests found
......@@ -7,9 +7,8 @@ from setuptools import setup
VERSION = open('src/null/_version.py').readlines()[-1].split()[-1].strip("\"'")
README = Path('README.md').read_text()
requires = [
'pycapo>=0.3.0,<1.0',
]
# requires = [
# ]
# tests_require = [
# 'pytest>=5.4,<6.0'
......@@ -23,7 +22,7 @@ setup(
author_email='dms-ssa@nrao.edu',
url='TBD',
license="GPL",
install_requires=requires,
# install_requires=requires,
# tests_require=tests_require,
keywords=[],
packages=['null'],
......
""" Module for the null executable. Performs some very basic actions
and utilizes pymygdala's LogHandler for logging. """
import os
import sys
import time
import random
import logging
import argparse
from pymygdala import LogHandler
from pycapo import CapoConfig
from ._version import ___version___ as version
_DESCRIPTION = """Workspaces null executable, a status capture test of the system. Version {}"""
# logging.basicConfig()
logger = logging.getLogger("null")
logger.setLevel(logging.INFO)
config = CapoConfig()
class Null:
def __init__(self, args, verbose):
......@@ -21,19 +21,34 @@ class Null:
if verbose:
logger.setLevel(logging.DEBUG)
self.args_to_funcs = {
'print_error': self.print_error,
'greeting': self.print_greeting,
'exit': self.exit_with_failure,
'exit-fail': self.exit_with_failure,
'exit-random': self.exit_randomly,
'nap': self.take_nap,
'dump': self.dump_core
}
def print_error(self):
print("in print_error")
handler = logging.StreamHandler(stream=sys.stderr)
logger.addHandler(handler)
print(logger.getEffectiveLevel())
logger.debug("STOP! YOU VIOLATED THE LAW! Pay the court a fine or serve your sentence.")
logger.error("This is an error...")
def print_greeting(self):
logger.info("Hello, world!")
logger.debug("And goodbye, world...")
def exit_with_failure(self):
logger.error("Error purposefully induced.")
sys.exit('Exiting with status code -1')
logger.error("Error purposefully induced. Exiting with status code -1...")
sys.exit(-1)
def exit_randomly(self):
status_code = random.randint(-50, 50)
logger.debug("Exiting with status code {}".format(status_code))
sys.exit(status_code)
def take_nap(self):
logger.debug("Going to sleep...")
......@@ -45,27 +60,38 @@ class Null:
os.abort()
def execute(self):
"""
Executes command specified by CL arguments.
"""
for arg, val in vars(self.args).items():
if val and arg in self.args_to_funcs:
self.args_to_funcs[arg]()
def make_arg_parser():
"""
Creates an argparse arguments parser with appropriate options
:return: Said argument parser
"""
parser = argparse.ArgumentParser(description=_DESCRIPTION.format(version),
formatter_class=argparse.RawTextHelpFormatter)
options = parser.add_argument_group('options', 'settings for altering program behavior')
options.add_argument('-v', '--verbose', action='store_true',
required=False, dest='verbose', default=False,
help='allow the program the gift of speech')
options.add_argument('-P', '--profile', action='store',
required=True, dest='profile', default=False,
help='profile name to use, e.g. test, production')
functions = parser.add_mutually_exclusive_group(required=False)
functions.add_argument('-pe', '--print-error', action='store_true',
required=False, dest='print-error', default=False,
help='print out aggressive message to stderr')
functions.add_argument('-g', '--greeting', action='store_true',
required=False, dest='greeting', default=False,
help='print out a friendly greeting')
functions.add_argument('-e', '--exit', action='store_true',
required=False, dest='exit', default=False,
help='print out a friendly greeting to stdout')
functions.add_argument('-ef', '--exit-fail', action='store_true',
required=False, dest='exit-fail', default=False,
help='print error message and exit with status code -1')
functions.add_argument('-er', '--exit-random', action='store_true',
required=False, dest='exit-random', default=False,
help='print error message and exit with random status code within [-50, 50]')
functions.add_argument('-n', '--nap', action='store_true',
required=False, dest='nap', default=False,
help='take a short nap')
......@@ -77,20 +103,11 @@ def make_arg_parser():
def main():
arg_parser = make_arg_parser()
args = arg_parser.parse_args()
handler = LogHandler(profile=args.profile, application='null_executable')
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)
# Shamelessly stolen from epilogue with a twist: allow for explict profile setting via the CL
if 'CAPO_PROFILE' not in os.environ and '' == args.profile:
# try to synthesize a profile from our installation root
profile = os.path.abspath(sys.argv[0]).split(os.path.sep)[-3]
os.environ['CAPO_PROFILE'] = profile
print('No CAPO_PROFILE explicitly set, synthesizing {0} by path'.format(str(profile)))
elif '' != args.profile:
os.environ['CAPO_PROFILE'] = args.profile
null = Null(args, args.verbose)
null.execute()
executable = Null(args, args.verbose)
executable.execute()
if __name__ == '__main__':
main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment