Skip to content

Commit

Permalink
Simplify argparse usage
Browse files Browse the repository at this point in the history
Refactor argument parsing to use argparse Namespace object for
structured access.
  • Loading branch information
William-Brown5515 committed Nov 20, 2024
1 parent 98f3e5a commit 94ec96b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
18 changes: 9 additions & 9 deletions bin/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import ssm.agents
from ssm import __version__, LOG_BREAK

from argparse import ArgumentParser
from argparse import ArgumentParser, Namespace

Check warning on line 24 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L24

Added line #L24 was not covered by tests
import logging
import os
import sys
Expand Down Expand Up @@ -51,23 +51,23 @@ def main():
action='version',
version=ver)

# Using the vars function to output a dict-like view rather than Namespace object.
options = vars(arg_parser.parse_args())
# Parsing arguments into an argparse.Namespace object for structured access.
options: Namespace = arg_parser.parse_args()

Check warning on line 55 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L55

Added line #L55 was not covered by tests

# 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 warning on line 63 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L63

Added line #L63 was not covered by tests

# 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)

Check warning on line 68 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L68

Added line #L68 was not covered by tests
else:
print("Config file not found at", options['config'])
print("Config file not found at", options.config)

Check warning on line 70 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L70

Added line #L70 was not covered by tests
sys.exit(1)

# Check for pidfile
Expand All @@ -90,7 +90,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__':
Expand Down
14 changes: 7 additions & 7 deletions bin/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import ssm.agents
from ssm import __version__, LOG_BREAK

from argparse import ArgumentParser
from argparse import ArgumentParser, Namespace

Check warning on line 24 in bin/sender.py

View check run for this annotation

Codecov / codecov/patch

bin/sender.py#L24

Added line #L24 was not covered by tests
import logging
import os
import sys
Expand All @@ -46,20 +46,20 @@ def main():
action='version',
version=ver)

# Using the vars function to output a dict-like view rather than Namespace object.
options = vars(arg_parser.parse_args())
# Parsing arguments into an argparse.Namespace object for structured access.
options: Namespace = arg_parser.parse_args()

Check warning on line 50 in bin/sender.py

View check run for this annotation

Codecov / codecov/patch

bin/sender.py#L50

Added line #L50 was not covered by tests

# 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)

Check warning on line 60 in bin/sender.py

View check run for this annotation

Codecov / codecov/patch

bin/sender.py#L60

Added line #L60 was not covered by tests
else:
print("Config file not found at", options['config'])
print("Config file not found at", options.config)

Check warning on line 62 in bin/sender.py

View check run for this annotation

Codecov / codecov/patch

bin/sender.py#L62

Added line #L62 was not covered by tests
sys.exit(1)

ssm.agents.logging_helper(cp)
Expand Down

0 comments on commit 94ec96b

Please sign in to comment.