Changeset 2873 for trunk/python


Ignore:
Timestamp:
12/03/13 13:57:35 (10 years ago)
Author:
Takeshi Nakazato
Message:

New Development: No

JIRA Issue: Yes CAS-5858

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No?

Module(s): Module Names change impacts.

Description: Describe your changes here...

Updated sd.selector.set_msselection_field so that it is able to handle
field id selection such as '<1', '<=1', '>1', '>=1', '1~2'.


File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/selector.py

    r2871 r2873  
    211211    def generate_query(self, selection_list):
    212212        for s in selection_list:
    213             if re.match('.*\*.*', s):
    214                 #print '"%s" is pattern match using *'%(s)
    215                 yield 'SRCNAME == pattern(\'%s\')'%(s)
    216             elif re.match('^<=?[0-9]*$', s):
     213            if s.isdigit() or re.match('^[<>]=?[0-9]*$', s) or \
     214                    re.match('^[0-9]+~[0-9]+$', s):
    217215                #print '"%s" is ID selection using < or <='%(s)
    218                 pass   
    219             elif re.match('^>=?[0-9]*$', s):
    220                 #print '"%s" is ID selection using > or >='%(s)
    221                 pass
    222             elif re.match('^[0-9]+~[0-9]+$', s):
    223                 #print '"%s" is ID selection using ~'%(s)
    224                 pass
    225             elif s.isdigit():
    226                 yield 'FIELDNAME == regex(\'.+__%s$\')'%(s)
    227             else:
    228                 #print '"%s" is exact match'%(s)
    229                 yield 'SRCNAME == pattern(\'%s\')'%(s)
     216                a = FieldIdRegexGenerator(s)
     217                yield '(%s)'%(a.get_regex())
     218            else:
     219                #print '"%s" is UNIX style pattern match'%(s)
     220                yield '(SRCNAME == pattern(\'%s\'))'%(s)
    230221       
    231222    def get_scans(self):
     
    304295                union.set_query(qs)
    305296        return union
     297
     298class FieldIdRegexGenerator(object):
     299    def __init__(self, pattern):
     300        if pattern.isdigit():
     301            self.regex = 'FIELDNAME == regex(\'.+__%s$\')'%(pattern)
     302        else:
     303            self.regex = None
     304            ineq = None
     305            if pattern.find('<') >= 0:
     306                ineq = '<'
     307                s = pattern.strip().lstrip(ineq).lstrip('=')
     308                if not s.isdigit():
     309                    raise RuntimeError('Invalid syntax: %s'%(pattern))
     310                self.id = int(s) + (-1 if pattern.find('=') < 0 else 0)
     311                self.template = string.Template('FIELDNAME == regex(\'.+__${reg}$\')')
     312            elif pattern.find('>') >= 0:
     313                ineq = '>'
     314                s = pattern.strip().lstrip(ineq).lstrip('=')
     315                if not s.isdigit():
     316                    raise RuntimeError('Invalid syntax: %s'%(pattern))
     317                self.id = int(s) + (-1 if pattern.find('=') >= 0 else 0)
     318                self.template = string.Template('FIELDNAME == regex(\'.+__[0-9]+$\') && FIELDNAME != regex(\'.+__${reg}$\')')
     319            elif pattern.find('~') >= 0:
     320                s = map(string.strip, pattern.split('~'))
     321                if len(s) == 2 and s[0].isdigit() and s[1].isdigit():
     322                    self.id = [int(s[0])-1,int(s[1])]
     323                else:
     324                    raise RuntimeError('Invalid syntax: %s'%(pattern))
     325                self.template = string.Template('FIELDNAME == regex(\'.+__${reg}$\') && FIELDNAME != regex(\'.+__${optreg}$\')')
     326            else:
     327                raise RuntimeError('Invalid syntax: %s'%(pattern))
     328            #print 'self.id=',self.id
     329
     330    def get_regex(self):
     331        if self.regex is not None:
     332            # 'X'
     333            return self.regex
     334        elif isinstance(self.id, list):
     335            # 'X~Y'
     336            return self.template.safe_substitute(reg=self.__compile(self.id[1]),
     337                                                 optreg=self.__compile(self.id[0]))
     338        else:
     339            # '<(=)X' or '>(=)X'
     340            return self.template.safe_substitute(reg=self.__compile(self.id))
     341
     342    def __compile(self, idx):
     343        pattern = ''
     344        if idx >= 0:
     345            if idx < 10:
     346                num_digits = 1
     347            else:
     348                num_digits = int(math.log10(idx)) + 1
     349            numerics = []
     350            modulo = 10
     351            divider = 1
     352            for i in xrange(num_digits):
     353                numerics.append(int((idx % modulo) / divider))
     354                modulo *= 10
     355                divider *= 10
     356            #print 'numerics=',numerics
     357            if num_digits == 1:
     358                if numerics[0] == 0:
     359                    pattern = '0'
     360                else:
     361                    pattern = '[0-%s]'%(numerics[0])
     362            elif num_digits == 2:
     363                pattern_list = ['[0-9]']
     364                pattern_list.append('[1-%s][0-9]'%(numerics[1]-1))
     365                if numerics[0] == 0:
     366                    pattern_list.append('%s%s'%(numerics[1],numerics[0]))
     367                else:
     368                    pattern_list.append('%s[0-%s]'%(numerics[1],numerics[0]))
     369                pattern = '(%s)'%('|'.join(pattern_list))
     370            elif num_digits == 3:
     371                pattern_list = ['[0-9]','[1-9][0-9]']
     372                if numerics[2] == 2:
     373                    pattern_list.append('1[0-9][0-9]')
     374                elif numerics[2] > 2:
     375                    pattern_list.append('[1-%s][0-9][0-9]'%(numerics[2]-1))
     376                if numerics[1] == 0:
     377                    if numerics[0] == 0:
     378                        pattern_list.append('%s00'%(numerics[2]))
     379                    else:
     380                        pattern_list.append('%s0[0-%s]'%(numerics[2],numerics[0]))
     381                else:
     382                    pattern_list.append('%s[0-%s][0-9]'%(numerics[2],numerics[1]-1))
     383                    if numerics[0] == 0:
     384                        pattern_list.append('%s%s%s'%(numerics[2],numerics[1],numerics[0]))
     385                    else:
     386                        pattern_list.append('%s%s[0-%s]'%(numerics[2],numerics[1],numerics[0]))
     387                pattern = '(%s)'%('|'.join(pattern_list))
     388            else:
     389                raise RuntimeError('ID > 999 is not supported')
     390                pattern = ''
     391        else:
     392            raise RuntimeError('ID must be >= 0')
     393        return pattern
Note: See TracChangeset for help on using the changeset viewer.