Changeset 250


Ignore:
Timestamp:
08/17/10 15:06:40 (14 years ago)
Author:
bas
Message:

Added AdvancedParser? from Dennis Stam as seperate module so we more people can make use of it.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/examples/new_rack_pbsmon.py

    r248 r250  
    3737
    3838import pbs
     39from AdvancedParser import AdvancedParser
    3940from PBSQuery import PBSQuery
    4041from PBSQuery import PBSError
     
    100101    pbs_ND_total            : ' '
    101102}
    102 
    103 ###### Some Useful Class ##################
    104 #
    105 # Author: Dennis Stam
    106 # Date  : 09-04-2009
    107 # Desc. : This class allows you use ranges within the given arguments. Very
    108 #         useful for specifying mutliple hosts. This class extends the
    109 #         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 been
    118         overrided.
    119 
    120         In this function a extra parsing is done on the the rest of the arguments. This
    121         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 class
    124         returns 24 hosts. Besides using numbers you can also specify lower cased
    125         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 args
    134         ['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. When
    140                 ower cased letters are specified they will be converted to integer
    141                 ordinal of a one-character string.
    142                 (ie. a = 97, z = 122)
    143 
    144                 The ranges will be return as lists
    145                 """
    146                 parts = string.split( ',' )
    147                 numbers_chars = list()
    148                 equal_width_length = 0
    149 
    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 = tmplist
    188 
    189                 return numbers_chars
    190 
    191         def combine( self, pre, post):
    192                 '''
    193                 This method is used to combine a possibility of a combination
    194                 '''
    195                 if pre == '':
    196                         return post
    197                 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 combinations
    203                 '''
    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 outlist
    214 
    215         def args_parser(self, args):
    216                 '''
    217                 This method checks all given extra arguments for the given ranges between the
    218                 [ 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 are
    237                                         # replaced by the correct value
    238                                         nodenames.append( pattern % comb )
    239                         else:
    240                                 nodenames.append( arg )
    241 
    242                 return nodenames
    243 
    244         def check_values(self, values, args):
    245                 '''
    246                 Here we override the default method in OptionParser to
    247                 enable our extra parsing on the given Arguments
    248                 '''
    249                 return values, self.args_parser( args )
    250 
    251 #### End Useful Class #####
    252103
    253104def sanitize_jobs( jobs ):
Note: See TracChangeset for help on using the changeset viewer.