Changeset 250
- Timestamp:
- 08/17/10 15:06:40 (14 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/examples/new_rack_pbsmon.py
r248 r250 37 37 38 38 import pbs 39 from AdvancedParser import AdvancedParser 39 40 from PBSQuery import PBSQuery 40 41 from PBSQuery import PBSError … … 100 101 pbs_ND_total : ' ' 101 102 } 102 103 ###### Some Useful Class ##################104 #105 # Author: Dennis Stam106 # Date : 09-04-2009107 # Desc. : This class allows you use ranges within the given arguments. Very108 # useful for specifying mutliple hosts. This class extends the109 # OptionParser class.110 #111 # SVN Info:112 # $Id$113 #114 115 class AdvancedParser(OptionParser):116 """117 This class extends from OptionParser, where the method check_values has been118 overrided.119 120 In this function a extra parsing is done on the the rest of the arguments. This121 extra parsing is the creating of multiple hostnames from a pattern/range.122 123 When a user specifies this argument dr-r[15,17-20]n[1-5,10] then this class124 returns 24 hosts. Besides using numbers you can also specify lower cased125 letters from a-z.126 127 Doctest:128 >>> parser = AdvancedParser()129 >>> parser.return_range('12-15,20')130 [12, 13, 14, 15, '20']131 132 >>> option, args = parser.parse_args(['dr-r7n[1-5]'])133 >>> print args134 ['dr-r7n1', 'dr-r7n2', 'dr-r7n3', 'dr-r7n4', 'dr-r7n5']135 """136 137 def return_range(self, string):138 """139 This method uses the given numbers and converts them to ranges. When140 ower cased letters are specified they will be converted to integer141 ordinal of a one-character string.142 (ie. a = 97, z = 122)143 144 The ranges will be return as lists145 """146 parts = string.split( ',' )147 numbers_chars = list()148 equal_width_length = 0149 150 for part in parts:151 part_range = part.split( '-' )152 if len( part_range ) == 2:153 try:154 if part_range[0][0] == '0' or part_range[1][0] == '0':155 if len( part_range[0] ) > len( part_range[1] ):156 equal_width_length = len( part_range[0] )157 else:158 equal_width_length = len( part_range[1] )159 160 numbers_chars += range( int( part_range[0] ), int( part_range[1] ) + 1 )161 except ValueError:162 begin = ord( part_range[0] )163 end = ord( part_range[1] )164 tmplist = list()165 166 if begin > 96 and end < 123:167 tmplist = range( begin, end + 1)168 169 for letter in tmplist:170 numbers_chars.append( chr( letter ) )171 else:172 if equal_width_length != 0 and len( part ) > equal_width_length:173 equal_width_length = len( part )174 175 numbers_chars.append( part )176 177 if equal_width_length > 0:178 tmplist = list()179 180 for number_char in numbers_chars:181 try:182 nnum = int( number_char )183 tmplist.append( '%0*d' % (equal_width_length, nnum) )184 except ValueError:185 tmplist.append( number_char )186 187 numbers_chars = tmplist188 189 return numbers_chars190 191 def combine( self, pre, post):192 '''193 This method is used to combine a possibility of a combination194 '''195 if pre == '':196 return post197 else:198 return '%s %s' % (pre, post)199 200 def combinations( self, listin, prefix=''):201 '''202 This method creates from the given ranges all possible combinations203 '''204 outlist = list()205 206 if len( listin ) > 1:207 for item in listin[0]:208 outlist += self.combinations( listin[1:], self.combine( prefix, str( item ) ) )209 else:210 for item in listin[0]:211 outlist.append( tuple( self.combine( prefix, str( item ) ).split( ' ') ) )212 213 return outlist214 215 def args_parser(self, args):216 '''217 This method checks all given extra arguments for the given ranges between the218 [ and ]219 '''220 findregex = re.compile( r'\[([0-9a-z\-,]+)\]', re.VERBOSE )221 nodenames = list()222 223 for arg in args:224 found = findregex.findall( arg )225 ranges = list()226 227 if found:228 pattern = findregex.sub( '%s', arg )229 230 for part in found:231 ranges.append( self.return_range( part ) )232 233 combs = self.combinations( ranges )234 235 for comb in combs:236 # Here the %s in the pattern are237 # replaced by the correct value238 nodenames.append( pattern % comb )239 else:240 nodenames.append( arg )241 242 return nodenames243 244 def check_values(self, values, args):245 '''246 Here we override the default method in OptionParser to247 enable our extra parsing on the given Arguments248 '''249 return values, self.args_parser( args )250 251 #### End Useful Class #####252 103 253 104 def sanitize_jobs( jobs ):
Note: See TracChangeset
for help on using the changeset viewer.