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

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

49 statements  

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.obs.base.cli.opt import instrument_option 

29from .opt import (delete_option, 

30 task_option) 

31 

32 

33# Class which determines an action that needs to be performed 

34# when building pipeline, its attributes are: 

35# action: the name of the action, e.g. "new_task", "delete_task" 

36# label: task label, can be None if action does not require label 

37# value: argument value excluding task label. 

38_PipelineAction = collections.namedtuple("_PipelineAction", "action,label,value") 

39 

40 

41class _PipelineActionType: 

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

43 ``_PipelineAction`` instances. 

44 

45 Parameters 

46 ---------- 

47 action : `str` 

48 Name of the action, will become `action` attribute of instance. 

49 regex : `str` 

50 Regular expression for argument value, it can define groups 'label' 

51 and 'value' which will become corresponding attributes of a 

52 returned instance. 

53 """ 

54 

55 def __init__(self, action, regex='.*', valueType=str): 

56 self.action = action 

57 self.regex = re.compile(regex) 

58 self.valueType = valueType 

59 

60 def __call__(self, value): 

61 match = self.regex.match(value) 

62 if not match: 

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

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

65 try: 

66 label = match.group("label") 

67 except IndexError: 

68 label = None 

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

70 try: 

71 value = match.group("value") 

72 except IndexError: 

73 pass 

74 value = self.valueType(value) 

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

76 

77 def __repr__(self): 

78 """String representation of this class. 

79 """ 

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

81 

82 

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

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

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

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

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

88 

89 

90def makePipelineActions(args, 

91 taskFlags=task_option.opts(), 

92 deleteFlags=delete_option.opts(), 

93 configFlags=config_option.opts(), 

94 configFileFlags=config_file_option.opts(), 

95 instrumentFlags=instrument_option.opts()): 

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