Skip to content

Commit

Permalink
Refactor to preserve original parseAndRun.
Browse files Browse the repository at this point in the history
  • Loading branch information
ktlim committed Nov 20, 2019
1 parent 63d19c0 commit c0bbfb6
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions python/lsst/pipe/tasks/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,28 @@ def __init__(self, *args, **kwargs):
self.makeSubtask("register")

@classmethod
def parseAndRun(cls, root=None, dryrun=False, mode="move", create=False,
def _parse(cls):
"""Parse the command-line arguments and return them along with a Task
instance."""
config = cls.ConfigClass()
parser = cls.ArgumentParser(name=cls._DefaultName)
args = parser.parse_args(config)
task = cls(config=args.config)
return task, args

@classmethod
def parseAndRun(cls):
"""Parse the command-line arguments and run the Task."""
task, args = cls._parse()
task.run(args)

@classmethod
def prepareTask(cls, root=None, dryrun=False, mode="move", create=False,
ignoreIngested=False):
"""Parse the command-line arguments and run the Task.
"""Prepare for running the task repeatedly with `ingestFiles`.
If the ``root`` argument is set, prepare for running the task
repeatedly with `lsst.pipe.tasks.ingest.IngestTask.runList` rather
than directly calling `lsst.pipe.tasks.ingest.IngestTask.run`.
Saves the parsed arguments, including the Butler and log, as a
private instance variable.
Parameters
----------
Expand All @@ -440,31 +455,21 @@ def parseAndRun(cls, root=None, dryrun=False, mode="move", create=False,
task : `IngestTask`
If `root` was provided, the IngestTask instance
"""
config = cls.ConfigClass()
parser = cls.ArgumentParser(name=cls._DefaultName)

if root is not None:
# Setup for being called from Python
sys.argv = ["IngestTask"]
sys.argv.append(root)
if dryrun:
sys.argv.append("--dry-run")
sys.argv.append("--mode")
sys.argv.append(mode)
if create:
sys.argv.append("--create")
if ignoreIngested:
sys.argv.append("--ignore-ingested")
sys.argv.append("__fakefile__")

args = parser.parse_args(config)
task = cls(config=args.config)

if root is None:
task.run(args)
else:
task._args = args
return task
sys.argv = ["IngestTask"]
sys.argv.append(root)
if dryrun:
sys.argv.append("--dry-run")
sys.argv.append("--mode")
sys.argv.append(mode)
if create:
sys.argv.append("--create")
if ignoreIngested:
sys.argv.append("--ignore-ingested")
sys.argv.append("__fakefile__") # needed for parsing, not used

task, args = cls._parse()
task._args = args
return task

def ingest(self, infile, outfile, mode="move", dryrun=False):
"""Ingest a file into the image repository.
Expand Down Expand Up @@ -609,19 +614,18 @@ def run(self, args):
except Exception as exc:
raise IngestError(f"Failed to register file {infile}", infile, pos) from exc

def runList(self, fileList):
"""Ingest specified list of files and add them to the registry.
def ingestFiles(self, fileList):
"""Ingest specified file or list of files and add them to the registry.
This method can only be called if `parseAndRun` was invoked with a
repository root.
This method can only be called if `prepareTask` was used.
Parameters
----------
fileList : `str` or `list` [`str`]
Pathname or list of pathnames of files to ingest.
"""
if not hasattr(self, "_args"):
raise RuntimeError("No previous parseAndRun with root")
raise RuntimeError("Task not created with prepareTask")
if isinstance(fileList, str):
fileList = [fileList]
self._args.files = fileList
Expand Down

0 comments on commit c0bbfb6

Please sign in to comment.