Coverage for python/lsst/ctrl/mpexec/cli/utils.py: 39%

49 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-24 01:54 -0700

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/>. 

21 

22 

23import collections 

24import re 

25 

26from lsst.daf.butler.cli.opt import config_file_option, config_option 

27from lsst.daf.butler.cli.utils import MWCommand 

28from lsst.pipe.base.cli.opt import instrument_option 

29 

30from .opt import delete_option, task_option 

31 

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

38 

39 

40class _PipelineActionType: 

41 """Class defining a callable type which converts strings into 

42 ``_PipelineAction`` instances. 

43 

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 """ 

53 

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 

58 

59 def __call__(self, value: str) -> _PipelineAction: 

60 match = self.regex.match(value) 

61 if not match: 

62 raise TypeError("Unrecognized option syntax: " + value) 

63 # get "label" group or use None as label 

64 try: 

65 label = match.group("label") 

66 except IndexError: 

67 label = None 

68 # if "value" group is not defined use whole string 

69 try: 

70 value = match.group("value") 

71 except IndexError: 

72 pass 

73 value = self.valueType(value) 

74 return _PipelineAction(self.action, label, value) 

75 

76 def __repr__(self) -> str: 

77 """String representation of this class.""" 

78 return f"_PipelineActionType(action={self.action})" 

79 

80 

81_ACTION_ADD_TASK = _PipelineActionType("new_task", "(?P<value>[^:]+)(:(?P<label>.+))?") 

82_ACTION_DELETE_TASK = _PipelineActionType("delete_task", "(?P<value>)(?P<label>.+)") 

83_ACTION_CONFIG = _PipelineActionType("config", "(?P<label>.+):(?P<value>.+=.+)") 

84_ACTION_CONFIG_FILE = _PipelineActionType("configfile", "(?P<label>.+):(?P<value>.+)") 

85_ACTION_ADD_INSTRUMENT = _PipelineActionType("add_instrument", "(?P<value>[^:]+)") 

86 

87 

88def makePipelineActions( 

89 args: list[str], 

90 taskFlags: list[str] = task_option.opts(), 

91 deleteFlags: list[str] = delete_option.opts(), 

92 configFlags: list[str] = config_option.opts(), 

93 configFileFlags: list[str] = config_file_option.opts(), 

94 instrumentFlags: list[str] = instrument_option.opts(), 

95) -> list[_PipelineAction]: 

96 """Make a list of pipline actions from a list of option flags and 

97 values. 

98 

99 Parameters 

100 ---------- 

101 args : `list` [`str`] 

102 The arguments, option flags, and option values in the order they were 

103 passed in on the command line. 

104 taskFlags : `list` [`str`], optional 

105 The option flags to use to recoginze a task action, by default 

106 task_option.opts() 

107 deleteFlags : `list` [`str`], optional 

108 The option flags to use to recoginze a delete action, by default 

109 delete_option.opts() 

110 configFlags : `list` [`str`], optional 

111 The option flags to use to recoginze a config action, by default 

112 config_option.opts() 

113 configFileFlags : `list` [`str`], optional 

114 The option flags to use to recoginze a config-file action, by default 

115 config_file_option.opts() 

116 instrumentFlags : `list` [`str`], optional 

117 The option flags to use to recoginze an instrument action, by default 

118 instrument_option.opts() 

119 

120 Returns 

121 ------- 

122 pipelineActions : `list` [`_PipelineActionType`] 

123 A list of pipeline actions constructed form their arguments in args, 

124 in the order they appeared in args. 

125 """ 

126 pipelineActions = [] 

127 # iterate up to the second-to-last element, if the second to last element 

128 # is a key we're looking for, the last item will be its value. 

129 for i in range(len(args) - 1): 

130 if args[i] in taskFlags: 

131 pipelineActions.append(_ACTION_ADD_TASK(args[i + 1])) 

132 elif args[i] in deleteFlags: 

133 pipelineActions.append(_ACTION_DELETE_TASK(args[i + 1])) 

134 elif args[i] in configFlags: 

135 pipelineActions.append(_ACTION_CONFIG(args[i + 1])) 

136 elif args[i] in configFileFlags: 

137 pipelineActions.append(_ACTION_CONFIG_FILE(args[i + 1])) 

138 elif args[i] in instrumentFlags: 

139 pipelineActions.append(_ACTION_ADD_INSTRUMENT(args[i + 1])) 

140 return pipelineActions 

141 

142 

143class PipetaskCommand(MWCommand): 

144 """Command subclass with pipetask-command specific overrides.""" 

145 

146 extra_epilog = "See 'pipetask --help' for more options." # type: ignore