Hide keyboard shortcuts

Hot-keys 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

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

28from .opt import (delete_option, 

29 task_option) 

30 

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, regex='.*', valueType=str): 

55 self.action = action 

56 self.regex = re.compile(regex) 

57 self.valueType = valueType 

58 

59 def __call__(self, value): 

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

77 """String representation of this class. 

78 """ 

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

80 

81 

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

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

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

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

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

87 

88 

89def makePipelineActions(args, 

90 taskFlags=task_option.opts(), 

91 deleteFlags=delete_option.opts(), 

92 configFlags=config_option.opts(), 

93 configFileFlags=config_file_option.opts(), 

94 instrumentFlags=instrument_option.opts()): 

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

96 values. 

97 

98 Parameters 

99 ---------- 

100 args : `list` [`str`] 

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

102 passed in on the command line. 

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

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

105 task_option.opts() 

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

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

108 delete_option.opts() 

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

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

111 config_option.opts() 

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

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

114 config_file_option.opts() 

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

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

117 instrument_option.opts() 

118 

119 Returns 

120 ------- 

121 pipelineActions : `list` [`_PipelineActionType`] 

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

123 in the order they appeared in args. 

124 """ 

125 pipelineActions = [] 

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

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

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

129 if args[i] in taskFlags: 

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

131 elif args[i] in deleteFlags: 

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

133 elif args[i] in configFlags: 

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

135 elif args[i] in configFileFlags: 

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

137 elif args[i] in instrumentFlags: 

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

139 return pipelineActions