Skip to content

Commit

Permalink
feat(isd_generate.py): Added the ability to provide a kernel file.
Browse files Browse the repository at this point in the history
  • Loading branch information
rbeyer committed May 6, 2022
1 parent f59b6a4 commit 196782a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
39 changes: 34 additions & 5 deletions ale/isd_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@

def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"-k", "--kernel",
type=Path,
help="Typically this is an optional metakernel file, care should be "
"taken by the user that it is applicable to all of the input "
"files. It can also be a single ISIS cube, which is sometimes "
"needed if the input file is a label file."
)
parser.add_argument(
"--max_workers",
default=None,
Expand Down Expand Up @@ -68,9 +76,17 @@ def main():
logging.basicConfig(format="%(message)s", level=log_level)
logger.setLevel(log_level)

if args.kernel is None:
k = None
else:
try:
k = ale.util.generate_kernels_from_cube(args.kernel, expand=True)
except KeyError:
k = [args.kernel, ]

if len(args.input) == 1:
try:
file_to_isd(args.input[0], args.out, log_level=log_level)
file_to_isd(args.input[0], args.out, kernels=k, log_level=log_level)
except Exception as err:
# Seriously, this just throws a generic Exception?
sys.exit(f"File {args.input[0]}: {err}")
Expand All @@ -80,7 +96,7 @@ def main():
) as executor:
futures = {
executor.submit(
file_to_isd, f, **{"log_level": log_level}
file_to_isd, f, **{"kernels": k, "log_level": log_level}
): f for f in args.input
}
for f in concurrent.futures.as_completed(futures):
Expand All @@ -94,7 +110,17 @@ def main():
logger.error(f"File {futures[f]}: {err}")


def file_to_isd(file: os.PathLike, out=None, log_level=logging.WARNING):
def file_to_isd(
file: os.PathLike,
out: os.PathLike = None,
kernels: list = None,
log_level=logging.WARNING
):
"""
Returns nothing, but acts as a thin wrapper to take the *file* and generate
an ISD at *out* (if given, defaults to replacing the extension on *file*
with .json), optionally using the passed *kernels*.
"""
# Yes, it is aggravating to have to pass the log_level into the function.
# If this weren't trying to be fancy with multiprocessing, it wouldn't
# be needed, and if this program were more complex, you'd build different
Expand All @@ -103,7 +129,7 @@ def file_to_isd(file: os.PathLike, out=None, log_level=logging.WARNING):
if out is None:
isd_file = Path(file).with_suffix(".json")
else:
isd_file = out
isd_file = Path(out)

# These two lines might seem redundant, but they are the only
# way to guarantee that when file_to_isd() is spun up in its own
Expand All @@ -112,7 +138,10 @@ def file_to_isd(file: os.PathLike, out=None, log_level=logging.WARNING):
logger.setLevel(log_level)

logger.info(f"Reading: {file}")
usgscsm_str = ale.loads(file)
if kernels is not None:
usgscsm_str = ale.loads(file, props={'kernels': kernels})
else:
usgscsm_str = ale.loads(file)

logger.info(f"Writing: {isd_file}")
isd_file.write_text(usgscsm_str)
Expand Down
48 changes: 48 additions & 0 deletions tests/pytests/test_isd_generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""This module has unit tests for the isd_generate functions."""

# This is free and unencumbered software released into the public domain.
#
# The authors of ale do not claim copyright on the contents of this file.
# For more details about the LICENSE terms and the AUTHORS, you will
# find files of those names at the top level of this repository.
#
# SPDX-License-Identifier: CC0-1.0

import unittest
from unittest.mock import call, patch

import ale.isd_generate as isdg


class TestFile(unittest.TestCase):

@patch("ale.isd_generate.Path.write_text")
def test_file_to_isd(self, m_path_wt):

json_text = "some json text"
cube_str = "dummy.cub"

with patch("ale.loads", return_value=json_text) as m_loads:
cube_str = "dummy.cub"
isdg.file_to_isd(cube_str)
self.assertEqual(
m_loads.call_args_list, [call(cube_str)]
)
self.assertEqual(
m_path_wt.call_args_list, [call(json_text)]
)

m_path_wt.reset_mock()
with patch("ale.loads", return_value=json_text) as m_loads:
out_str = "dummy.json"
kernel_val = "list of kernels"
isdg.file_to_isd(cube_str, out=out_str, kernels=kernel_val)
self.assertEqual(
m_loads.call_args_list,
[call(cube_str, props={'kernels': kernel_val})]
)
self.assertEqual(
m_path_wt.call_args_list, [call(json_text)]
)

0 comments on commit 196782a

Please sign in to comment.