From dc79b3fa01c2cafd0924ac63a44fb1dc75381c17 Mon Sep 17 00:00:00 2001 From: William-Brown5515 Date: Wed, 4 Sep 2024 13:15:12 +0100 Subject: [PATCH 1/2] Replace optparse with argparse Optparse is deprecated - no longer developed. Mostly a like-for-like replacement, but we use vars on the argparse object to output the values in a similar way to optparse. The old version option on the constructor is unavailable, and so it has been replaced by adding an argument with the 'version' action. --- bin/receiver.py | 48 +++++++++++++++++++++++++++--------------------- bin/sender.py | 36 +++++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/bin/receiver.py b/bin/receiver.py index 3efc8783..1ebedc20 100644 --- a/bin/receiver.py +++ b/bin/receiver.py @@ -21,10 +21,10 @@ import ssm.agents from ssm import __version__, LOG_BREAK +from argparse import ArgumentParser import logging import os import sys -from optparse import OptionParser import configparser @@ -34,35 +34,41 @@ def main(): ver = "SSM %s.%s.%s" % __version__ default_conf_location = '/etc/apel/receiver.cfg' default_dns_location = '/etc/apel/dns' - op = OptionParser(description=__doc__, version=ver) - op.add_option('-c', '--config', - help=('location of config file, ' - 'default path: ' + default_conf_location), - default=default_conf_location) - op.add_option('-l', '--log_config', - help='DEPRECATED - location of logging config file (optional)', - default=None) - op.add_option('-d', '--dn_file', - help=('location of the file containing valid DNs, ' - 'default path: ' + default_dns_location), - default=default_dns_location) - - options, unused_args = op.parse_args() + arg_parser = ArgumentParser(description=__doc__) + + arg_parser.add_argument('-c', '--config', + help='location of config file, default path: ' + '%s' % default_conf_location, + default=default_conf_location) + arg_parser.add_argument('-l', '--log_config', + help='DEPRECATED - location of logging config ' + 'file (optional)', + default=None) + arg_parser.add_argument('-d', '--dn_file', + help='location of the file containing valid DNs, ' + 'default path: %s' % default_dns_location, + default=default_dns_location) + arg_parser.add_argument('-v', '--version', + action='version', + version='ver: %s' % ver) + + # Using the vars function to output a dict-like view rather than Namespace object. + options = vars(arg_parser.parse_args()) # Deprecating functionality. old_log_config_default_path = '/etc/apel/logging.cfg' - if (os.path.exists(old_log_config_default_path) or options.log_config is not None): + if (os.path.exists(old_log_config_default_path) or options['log_config'] is not None): logging.warning('Separate logging config file option has been deprecated.') # Absolute file path required when refreshing dn_file, relative path resulted in an error. - options.dn_file = os.path.abspath(options.dn_file) + options['dn_file'] = os.path.abspath(options['dn_file']) # Check if config file exists using os.path.isfile function. - if os.path.isfile(options.config): + if os.path.isfile(options['config']): cp = configparser.ConfigParser({'use_ssl': 'true'}) - cp.read(options.config) + cp.read(options['config']) else: - print("Config file not found at", options.config) + print("Config file not found at", options['config']) sys.exit(1) # Check for pidfile @@ -85,7 +91,7 @@ def main(): brokers, project, token = ssm.agents.get_ssm_args(protocol, cp, log) ssm.agents.run_receiver(protocol, brokers, project, token, - cp, log, options.dn_file) + cp, log, options['dn_file']) if __name__ == '__main__': diff --git a/bin/sender.py b/bin/sender.py index cb4bfac4..7c85b784 100644 --- a/bin/sender.py +++ b/bin/sender.py @@ -21,8 +21,8 @@ import ssm.agents from ssm import __version__, LOG_BREAK +from argparse import ArgumentParser import logging -from optparse import OptionParser import os import sys @@ -33,28 +33,34 @@ def main(): """Set up connection, send all messages and quit.""" ver = "SSM %s.%s.%s" % __version__ default_conf_location = '/etc/apel/sender.cfg' - op = OptionParser(description=__doc__, version=ver) - op.add_option('-c', '--config', - help=('location of config file, ' - 'default path: ' + default_conf_location), - default=default_conf_location) - op.add_option('-l', '--log_config', - help='DEPRECATED - location of logging config file (optional)', - default=None) - - options, unused_args = op.parse_args() + arg_parser = ArgumentParser(description=__doc__) + + arg_parser.add_argument('-c', '--config', + help=('location of config file, default path: ' + '%s' % default_conf_location), + default=default_conf_location) + arg_parser.add_argument('-l', '--log_config', + help='DEPRECATED - location of logging config' + 'file (optional)', + default=None) + arg_parser.add_argument('-v', '--version', + action='version', + version='ver: %s' % ver) + + # Using the vars function to output a dict-like view rather than Namespace object. + options = vars(arg_parser.parse_args()) # Deprecating functionality. old_log_config_default_path = '/etc/apel/logging.cfg' - if (os.path.exists(old_log_config_default_path) or options.log_config is not None): + if (os.path.exists(old_log_config_default_path) or options['log_config'] is not None): logging.warning('Separate logging config file option has been deprecated.') # Check if config file exists using os.path.isfile function. - if os.path.isfile(options.config): + if os.path.isfile(options['config']): cp = configparser.ConfigParser({'use_ssl': 'true'}) - cp.read(options.config) + cp.read(options['config']) else: - print("Config file not found at", options.config) + print("Config file not found at", options['config']) sys.exit(1) ssm.agents.logging_helper(cp) From 6e623eb11dfa6d2322fb7d1f6bf889977856d8d4 Mon Sep 17 00:00:00 2001 From: William-Brown5515 Date: Wed, 11 Sep 2024 11:49:57 +0100 Subject: [PATCH 2/2] Code structure fixes Removal of 'optional' on deprecated arguments, using only 'ver' for version numbers, removal of unnecessary parentheses. --- bin/receiver.py | 5 ++--- bin/sender.py | 9 ++++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/bin/receiver.py b/bin/receiver.py index 1ebedc20..bdeac65f 100644 --- a/bin/receiver.py +++ b/bin/receiver.py @@ -41,8 +41,7 @@ def main(): '%s' % default_conf_location, default=default_conf_location) arg_parser.add_argument('-l', '--log_config', - help='DEPRECATED - location of logging config ' - 'file (optional)', + help='DEPRECATED - location of logging config file', default=None) arg_parser.add_argument('-d', '--dn_file', help='location of the file containing valid DNs, ' @@ -50,7 +49,7 @@ def main(): default=default_dns_location) arg_parser.add_argument('-v', '--version', action='version', - version='ver: %s' % ver) + version=ver) # Using the vars function to output a dict-like view rather than Namespace object. options = vars(arg_parser.parse_args()) diff --git a/bin/sender.py b/bin/sender.py index 7c85b784..f228d919 100644 --- a/bin/sender.py +++ b/bin/sender.py @@ -36,16 +36,15 @@ def main(): arg_parser = ArgumentParser(description=__doc__) arg_parser.add_argument('-c', '--config', - help=('location of config file, default path: ' - '%s' % default_conf_location), + help='location of config file, default path: ' + '%s' % default_conf_location, default=default_conf_location) arg_parser.add_argument('-l', '--log_config', - help='DEPRECATED - location of logging config' - 'file (optional)', + help='DEPRECATED - location of logging config file', default=None) arg_parser.add_argument('-v', '--version', action='version', - version='ver: %s' % ver) + version=ver) # Using the vars function to output a dict-like view rather than Namespace object. options = vars(arg_parser.parse_args())