arrfab / centos-git-common

Forked from centos-git-common 5 years ago
Clone

Blame mqtt/example-safe-command.py

0b0750
#!/usr/bin/env python3.6
0b0750
#pylint: disable=line-too-long
0b0750
#
0b0750
#  Copyright (2019).  Fermi Research Alliance, LLC.
0b0750
#  Initial Author: Pat Riehecky <riehecky@fnal.gov>
0b0750
#
0b0750
'''
0b0750
    Example command to run from listen-on-dbus-for-mqtt-signals.py
0b0750
'''
0b0750
0b0750
## Uncomment these for python2 support
0b0750
#from __future__ import unicode_literals
0b0750
#from __future__ import absolute_import
0b0750
#from __future__ import print_function
0b0750
0b0750
import json
0b0750
import sys
0b0750
import textwrap
0b0750
0b0750
from pprint import pprint
0b0750
0b0750
try:
0b0750
    from argparse import ArgumentParser
0b0750
except ImportError:  # pragma: no cover
0b0750
    print("Please install argparse - rpm: python-argparse", file=sys.stderr)
0b0750
    raise
0b0750
0b0750
##########################################
0b0750
def setup_args():
0b0750
    '''
0b0750
        Setup the argparse object.
0b0750
0b0750
        Make sure all fields have defaults so we could use this as an object
0b0750
    '''
0b0750
    parser = ArgumentParser(description=textwrap.dedent(__doc__))
0b0750
0b0750
    parser.add_argument('signal', help='The dbus signal is set here')
0b0750
0b0750
    return parser
0b0750
0b0750
##########################################
0b0750
0b0750
##########################################
0b0750
##########################################
0b0750
if __name__ == '__main__':
0b0750
0b0750
    PARSER = setup_args()
0b0750
    ARGS = PARSER.parse_args()
0b0750
0b0750
    MESSAGE = json.loads(sys.stdin.read())
0b0750
    print("Your dbus-signal was %s" % ARGS.signal)
0b0750
    print("Your message was decoded as %s (between the lines)" % type(MESSAGE))
0b0750
    print("------------------------------------------------")
0b0750
    pprint(MESSAGE)
0b0750
    print("------------------------------------------------")