Coverage for python/lsst/ctrl/mpexec/cli/utils.py: 33%
50 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-09 03:32 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-09 03:32 -0800
1# This file is part of ctrl_mpexec.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
23import collections
24import re
26from lsst.daf.butler.cli.opt import config_file_option, config_option
27from lsst.daf.butler.cli.utils import MWCommand, split_commas
28from lsst.pipe.base.cli.opt import instrument_option
30from .opt import delete_option, task_option
32# Class which determines an action that needs to be performed
33# when building pipeline, its attributes are:
34# action: the name of the action, e.g. "new_task", "delete_task"
35# label: task label, can be None if action does not require label
36# value: argument value excluding task label.
37_PipelineAction = collections.namedtuple("_PipelineAction", "action,label,value")
40class _PipelineActionType:
41 """Class defining a callable type which converts strings into
42 ``_PipelineAction`` instances.
44 Parameters
45 ----------
46 action : `str`
47 Name of the action, will become `action` attribute of instance.
48 regex : `str`
49 Regular expression for argument value, it can define groups 'label'
50 and 'value' which will become corresponding attributes of a
51 returned instance.
52 """
54 def __init__(self, action: str, regex: str = ".*", valueType: type = str):
55 self.action = action
56 self.regex = re.compile(regex)
57 self.valueType = valueType
59 def __call__(self, value: str) -> _PipelineAction:
60 match = self.regex.match(value)
61 if not match:
62 raise TypeError(
63 f"Unrecognized syntax for option {self.action!r}: {value!r} "
64 f"(does not match pattern {self.regex.pattern})"
65 )
66 # get "label" group or use None as label
67 try:
68 label = match.group("label")
69 except IndexError:
70 label = None
71 # if "value" group is not defined use whole string
72 try:
73 value = match.group("value")
74 except IndexError:
75 pass
76 value = self.valueType(value)
77 return _PipelineAction(self.action, label, value)
79 def __repr__(self) -> str:
80 """String representation of this class."""
81 return f"_PipelineActionType(action={self.action})"
84_ACTION_ADD_TASK = _PipelineActionType("new_task", "(?P<value>[^:]+)(:(?P<label>.+))?")
85_ACTION_DELETE_TASK = _PipelineActionType("delete_task", "(?P<value>)(?P<label>.+)")
86_ACTION_CONFIG = _PipelineActionType("config", "(?P<label>.+):(?P<value>.+=.+)")
87_ACTION_CONFIG_FILE = _PipelineActionType("configfile", "(?P<label>.+):(?P<value>.+)")
88_ACTION_ADD_INSTRUMENT = _PipelineActionType("add_instrument", "(?P<value>[^:]+)")
91def makePipelineActions(
92 args: list[str],
93 taskFlags: list[str] = task_option.opts(),
94 deleteFlags: list[str] = delete_option.opts(),
95 configFlags: list[str] = config_option.opts(),
96 configFileFlags: list[str] = config_file_option.opts(),
97 instrumentFlags: list[str] = instrument_option.opts(),
98) -> list[_PipelineAction]:
99 """Make a list of pipline actions from a list of option flags and
100 values.
102 Parameters
103 ----------
104 args : `list` [`str`]
105 The arguments, option flags, and option values in the order they were
106 passed in on the command line.
107 taskFlags : `list` [`str`], optional
108 The option flags to use to recoginze a task action, by default
109 task_option.opts()
110 deleteFlags : `list` [`str`], optional
111 The option flags to use to recoginze a delete action, by default
112 delete_option.opts()
113 configFlags : `list` [`str`], optional
114 The option flags to use to recoginze a config action, by default
115 config_option.opts()
116 configFileFlags : `list` [`str`], optional
117 The option flags to use to recoginze a config-file action, by default
118 config_file_option.opts()
119 instrumentFlags : `list` [`str`], optional
120 The option flags to use to recoginze an instrument action, by default
121 instrument_option.opts()
123 Returns
124 -------
125 pipelineActions : `list` [`_PipelineActionType`]
126 A list of pipeline actions constructed form their arguments in args,
127 in the order they appeared in args.
128 """
129 pipelineActions = []
130 # iterate up to the second-to-last element, if the second to last element
131 # is a key we're looking for, the last item will be its value.
132 for i in range(len(args) - 1):
133 if args[i] in taskFlags:
134 pipelineActions.append(_ACTION_ADD_TASK(args[i + 1]))
135 elif args[i] in deleteFlags:
136 pipelineActions.append(_ACTION_DELETE_TASK(args[i + 1]))
137 elif args[i] in configFlags:
138 pipelineActions.append(_ACTION_CONFIG(args[i + 1]))
139 elif args[i] in configFileFlags:
140 # --config-file allows multiple comma-separated values.
141 configfile_args = split_commas(None, None, args[i + 1])
142 pipelineActions.extend(_ACTION_CONFIG_FILE(c) for c in configfile_args)
143 elif args[i] in instrumentFlags:
144 pipelineActions.append(_ACTION_ADD_INSTRUMENT(args[i + 1]))
145 return pipelineActions
148class PipetaskCommand(MWCommand):
149 """Command subclass with pipetask-command specific overrides."""
151 extra_epilog = "See 'pipetask --help' for more options."