dbrtools/dbr/dbrfilter.py
branchDBRToolsDev
changeset 242 9fd4819bf104
child 245 fd0a8d235c70
equal deleted inserted replaced
220:4df7a7370ae2 242:9fd4819bf104
       
     1 # Copyright (c) 2010 Symbian Foundation Ltd
       
     2 # This component and the accompanying materials are made available
       
     3 # under the terms of the License "Eclipse Public License v1.0"
       
     4 # which accompanies this distribution, and is available
       
     5 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     6 #
       
     7 # Initial Contributors:
       
     8 # Symbian Foundation Ltd - initial contribution.
       
     9 #
       
    10 # Contributors:
       
    11 # mattd <mattd@symbian.org>
       
    12 #
       
    13 # Description:
       
    14 # DBRFilter - DBR Filtering classes  
       
    15 
       
    16 import dbrresults
       
    17 import dbrutils
       
    18 import re
       
    19 
       
    20 from optparse import OptionParser
       
    21 
       
    22 def CreateFilter(args):
       
    23 #  print args
       
    24   
       
    25   parser = OptionParser()
       
    26   parser.add_option('-i', '--iregex', dest='iregex', action='append')
       
    27   parser.add_option('-x', '--xregex', dest='xregex', action='append')
       
    28   parser.add_option('-@', '--ifile', dest='ifile', action='append')
       
    29   parser.add_option('-!', '--xfile', dest='xfile', action='append')
       
    30 
       
    31 #  if(os.path.isfile(arg)):
       
    32 #    return DBRFileFilterInclude(args)
       
    33 #  return DBRRegexFileFilterExclude(args)
       
    34   (options, args) = parser.parse_args(args)
       
    35 #  print options
       
    36   filter = DBRComplexFilter()
       
    37   if options.iregex:
       
    38     for include in options.iregex:
       
    39       filter.addInclude(DBRRegexFileFilter(include))
       
    40   if options.ifile:
       
    41     for include in options.ifile:
       
    42       filter.addInclude(DBRFileFilter(include))
       
    43   if options.xregex:
       
    44     for exclude in options.xregex:
       
    45      filter.addExclude(DBRRegexFileFilter(exclude))
       
    46   if options.xfile:
       
    47     for exclude in options.xfile:
       
    48       filter.addExclude(DBRFileFilter(exclude))
       
    49 
       
    50   return filter     
       
    51 
       
    52 
       
    53 class DBRFilter:
       
    54   info = ''
       
    55   def __init__(self):
       
    56     self.info = 'Null Filter'
       
    57   def filter(self, results):
       
    58     return results
       
    59   def include(self, results):
       
    60     return set()
       
    61   def exclude(self, results):
       
    62     return results
       
    63 
       
    64   
       
    65 class DBRFileFilter (DBRFilter):
       
    66   filename = ''
       
    67   def __init__(self, filename):
       
    68     DBRFilter.__init__(self)
       
    69     self.filename = filename
       
    70     self.files = dbrutils.readfilenamesfromfile(self.filename)
       
    71 #    for file in sorted(self.files):
       
    72 #      print file
       
    73 
       
    74   def include(self, results):
       
    75     return dbrresults.DBRResults(results.added & self.files, results.removed & self.files, results.touched & self.files, results.changed & self.files, results.unknown & self.files)
       
    76   def exclude(self, results):
       
    77     return dbrresults.DBRResults(results.added - self.files, results.removed - self.files, results.touched - self.files, results.changed - self.files, results.unknown - self.files)
       
    78 
       
    79 class DBRFileFilterInclude (DBRFileFilter):
       
    80   def __init__(self, filename):
       
    81     DBRFileFilter.__init__(self, filename)
       
    82   def filter(self, results):
       
    83     return self.include(results)
       
    84 
       
    85 class DBRFileFilterExclude (DBRFileFilter):
       
    86   def __init__(self, filename):
       
    87     DBRFileFilter.__init__(self, filename)
       
    88   def filter(self, results):
       
    89     return self.exclude(results)
       
    90         
       
    91 class DBRRegexFileFilter (DBRFilter):
       
    92   regex = ''
       
    93   def __init__(self, regex):
       
    94     DBRFilter.__init__(self)
       
    95     #This can throw a compiler error. It would be nicer to have this display help.
       
    96     try: 
       
    97       self.regex = re.compile(regex, re.IGNORECASE) # doing case-insensitive regexes at the moment
       
    98     except re.error:
       
    99       print 'WARNING: Bad Regular Expression:%s', regex
       
   100       self.regex = re.compile('')
       
   101 
       
   102   #might be able to do this nicer using 'itertools'
       
   103   def inc(self, files):
       
   104     results = set()
       
   105     for candidate in files:
       
   106       if self.regex.search(candidate):
       
   107         results.add(candidate)
       
   108     return results
       
   109 
       
   110   def exc(self, files):
       
   111     results = set()
       
   112     for candidate in files:
       
   113       if not self.regex.search(candidate):
       
   114         results.add(candidate)
       
   115     return results
       
   116     
       
   117   def include(self, results):
       
   118     return dbrresults.DBRResults(self.inc(results.added), self.inc(results.removed), self.inc(results.touched), self.inc(results.changed), self.inc(results.unknown))
       
   119 
       
   120   def exclude(self, results):
       
   121     return dbrresults.DBRResults(self.exc(results.added), self.exc(results.removed), self.exc(results.touched), self.exc(results.changed), self.exc(results.unknown))
       
   122 
       
   123 class DBRRegexFileFilterInclude (DBRRegexFileFilter):
       
   124   def __init__(self, regex):
       
   125     DBRRegexFileFilter.__init__(self, regex)
       
   126   def filter(self, results):
       
   127     return self.include(results)
       
   128       
       
   129 class DBRRegexFileFilterExclude (DBRRegexFileFilter):
       
   130   def __init__(self, regex):
       
   131     DBRRegexFileFilter.__init__(self, regex)
       
   132   def filter(self, results):
       
   133     return self.exclude(results)
       
   134 
       
   135 class DBRComplexFilter (DBRFilter):
       
   136   exc = set()
       
   137   inc = set()
       
   138   def __init__(self):
       
   139     DBRFilter.__init__(self)
       
   140     self.exc = set()
       
   141     self.inc = set()
       
   142   
       
   143   def addInclude(self, filter):
       
   144     self.inc.add(filter)
       
   145   
       
   146   def addExclude(self, filter):
       
   147     self.exc.add(filter)
       
   148 
       
   149   def include(self, results):
       
   150     res = dbrresults.DBRResults(set(),set(),set(),set(),set())
       
   151     if self.inc:
       
   152       for filter in self.inc:  
       
   153         res |= filter.include(results) 
       
   154     return res
       
   155     
       
   156   def exclude(self, results):
       
   157     res = results
       
   158     if self.exc:
       
   159       for filter in self.exc:
       
   160         res &= filter.exclude(results)
       
   161     return res
       
   162   
       
   163   def filter(self, results):
       
   164     return self.include(results) | self.exclude(results)
       
   165      
       
   166