Skip to content

Commit

Permalink
use configargparse in all scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHancock committed Jan 14, 2025
1 parent 736f182 commit f287a2d
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 24 deletions.
20 changes: 11 additions & 9 deletions AegeanTools/CLI/AeReg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env python

import argparse
import configargparse
import os
import sys

Expand All @@ -13,25 +13,22 @@
from AegeanTools.logging import logger, logging

__author__ = ["PaulHancock"]
__date__ = "2024-01-24"
__version__ = "0.9"
__version__ = "0.9.1"
__date__ = "2025-01-14"


def main():
"""
A regrouping tool to accompany the Aegean source finding program.
"""

parser = argparse.ArgumentParser(prog="regroup", prefix_chars="-")
parser = configargparse.ArgumentParser(prog="AeReg", prefix_chars="-")
group1 = parser.add_argument_group("Required")
group1.add_argument(
"--input", dest="input", type=str, required=True, help="The input catalogue."
)
group1.add_argument("--input", dest="input", type=str, help="The input catalogue.")
group1.add_argument(
"--table",
dest="tables",
type=str,
required=True,
help="Table outputs, format inferred from extension.",
)

Expand Down Expand Up @@ -72,15 +69,20 @@ def main():
group4.add_argument(
"--debug", dest="debug", action="store_true", default=False, help="Debug mode."
)
group4.add_argument("--config", is_config_file=True, help="Path to the config file")

options = parser.parse_args()

if not options.input or not options.tables:
parser.print_help()
return 0

invocation_string = " ".join(sys.argv)

# configure logging
logging_level = logging.DEBUG if options.debug else logging.INFO
logger.setLevel(logging_level)
logger.info("This is regroup {0}-({1})".format(__version__, __date__))
logger.info("This is AeReg {0}-({1})".format(__version__, __date__))
logger.debug("Run as:\n{0}".format(invocation_string))

# check that a valid intput filename was entered
Expand Down
10 changes: 4 additions & 6 deletions AegeanTools/CLI/AeRes.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
#! /usr/bin/env python

import argparse
import configargparse

from AegeanTools.AeRes import make_residual
from AegeanTools.logging import logger, logging

__author__ = "Paul Hancock"
__version__ = "v0.2.7"
__date__ = "2024-01-24"


# global constants
__date__ = "2025-01-14"


def main():
"""
Tool for making residual images with Aegean tables as input
"""

parser = argparse.ArgumentParser(prog="AeRes", prefix_chars="-")
parser = configargparse.ArgumentParser(prog="AeRes", prefix_chars="-")
group1 = parser.add_argument_group("I/O arguments")
group1.add_argument(
"-c",
Expand Down Expand Up @@ -100,6 +97,7 @@ def main():
group4.add_argument(
"--debug", dest="debug", action="store_true", default=False, help="Debug mode."
)
group4.add_argument("--config", is_config_file=True, help="Path to the config file")

options = parser.parse_args()

Expand Down
9 changes: 7 additions & 2 deletions AegeanTools/CLI/MIMAS.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#! /usr/bin/env python
import argparse
import configargparse
import sys

from AegeanTools import MIMAS, __citation__
from AegeanTools.logging import logger, logging

__author__ = "Paul Hancock"
__date__ = "2025-01-14"


def main():
"""
Expand All @@ -15,7 +18,7 @@ def main():
"+r -r +c -c +p -p. This means that you might have to take "
"multiple passes to construct overly complicated regions."
)
parser = argparse.ArgumentParser(epilog=epilog, prefix_chars="+-")
parser = configargparse.ArgumentParser(epilog=epilog, prefix_chars="+-")

group1 = parser.add_argument_group(
"Creating/modifying regions", "Must specify -o, plus or more [+-][cr]"
Expand Down Expand Up @@ -247,6 +250,8 @@ def main():
help="Show citation information.",
)

group4.add_argument("--config", is_config_file=True, help="Path to the config file")

results = parser.parse_args()

# print help if the user enters no options or filename
Expand Down
14 changes: 10 additions & 4 deletions AegeanTools/CLI/SR6.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /usr/bin/env python
import argparse
import configargparse
import os
import sys

Expand All @@ -12,8 +12,8 @@
from AegeanTools.logging import logger, logging

__author__ = "Paul Hancock"
__version__ = "v1.2"
__date__ = "2024-01-24"
__version__ = "v1.2.1"
__date__ = "2025-01-14"


# command line version of this program runs from here.
Expand All @@ -24,7 +24,7 @@ def main():
"""

epilog = ""
parser = argparse.ArgumentParser(epilog=epilog, prefix_chars="-")
parser = configargparse.ArgumentParser(epilog=epilog, prefix_chars="-")

# tools for shrinking files
group1 = parser.add_argument_group("Shrinking and expanding files")
Expand Down Expand Up @@ -81,8 +81,10 @@ def main():
default=False,
help="Show citation information.",
)
group2.add_argument("--config", is_config_file=True, help="Path to the config file")

results = parser.parse_args()

# print help if the user enters no options or filename
if len(sys.argv) == 1:
parser.print_help()
Expand All @@ -92,6 +94,10 @@ def main():
print(__citation__)
return 0

if results.infile is None:
parser.print_help()
return 0

logging_level = logging.DEBUG if results.debug else logging.INFO
logger.setLevel(logging_level)
logger.info("This is SR6 {0}-({1})".format(__version__, __date__))
Expand Down
6 changes: 4 additions & 2 deletions AegeanTools/CLI/aegean.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env python

import argparse
import configargparse

# import logging.config
import multiprocessing
Expand Down Expand Up @@ -51,10 +51,12 @@ def main():
The Aegean source finding program.
"""

parser = argparse.ArgumentParser(prog="aegean", prefix_chars="-")
parser = configargparse.ArgumentParser(prog="aegean", prefix_chars="-")
parser.add_argument("image", nargs="?", default=None)
group1 = parser.add_argument_group("Configuration Options")

group1.add_argument("--config", is_config_file=True, help="Path to the config file")

group1.add_argument(
"--find",
dest="find",
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ dependencies = [
"astropy>=2.0",
"healpy >=1.10",
"lmfit>=0.9.2",
"scikit-learn>=0.24.2"
"scikit-learn>=0.24.2",
"configargparse>=1.7"
]
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/test_script_AeReg.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ def test_broken_catalogue():
os.remove(tempfile + "_comp")


def test_configfile():
cfile = "AeReg.ini"
try:
with open(cfile, "w") as config:
config.write("debug = False")
sys.argv = ["", "--config", cfile]
AeReg.main()
finally:
if os.path.exists(cfile):
os.remove(cfile)


if __name__ == "__main__":
# introspect and run all the functions starting with 'test'
for f in dir():
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/test_script_AeRes.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ def test_nocat():
os.remove(tempfile + "_model")


def test_configfile():
cfile = "AeRes.ini"
try:
with open(cfile, "w") as config:
config.write("debug = False")
sys.argv = ["", "--config", cfile]
AeRes.main()
finally:
if os.path.exists(cfile):
os.remove(cfile)


if __name__ == "__main__":
# introspect and run all the functions starting with 'test'
for f in dir():
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/test_script_MIMAS.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ def test_make_region():
os.remove(tempfile)


def test_configfile():
cfile = "MIMAS.ini"
try:
with open(cfile, "w") as config:
config.write("debug = False")
sys.argv = ["", "--config", cfile]
MIMAS.main()
finally:
if os.path.exists(cfile):
os.remove(cfile)


if __name__ == "__main__":
# introspect and run all the functions starting with 'test'
for f in dir():
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/test_script_SR6.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ def test_expand():
os.remove(tempfile + ".fits")


def test_configfile():
cfile = "SR6.ini"
try:
with open(cfile, "w") as config:
config.write("debug = False")
sys.argv = ["", "--config", cfile]
SR6.main()
finally:
if os.path.exists(cfile):
os.remove(cfile)


if __name__ == "__main__":
# introspect and run all the functions starting with 'test'
for f in dir():
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/test_script_aegean.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ def test_priorized():
aegean.main()


def test_configfile():
cfile = "aegean.ini"
try:
with open(cfile, "w") as config:
config.write("debug = False")
sys.argv = ["", "--config", cfile]
aegean.main()
finally:
if os.path.exists(cfile):
os.remove(cfile)


if __name__ == "__main__":
# introspect and run all the functions starting with 'test'
for f in dir():
Expand Down

0 comments on commit f287a2d

Please sign in to comment.