pvl/syslog/filter.py
changeset 74 952ee07efd7a
parent 68 bea41de5cc98
child 99 8d60eb5604e4
equal deleted inserted replaced
73:ef01c4639689 74:952ee07efd7a
    14         """
    14         """
    15         
    15         
    16         # drop None's
    16         # drop None's
    17         self.filters = dict((attr, regex) for attr, regex in filters.iteritems() if regex is not None)
    17         self.filters = dict((attr, regex) for attr, regex in filters.iteritems() if regex is not None)
    18 
    18 
    19     def match_prog (self, attr, glob, prog=None) :
    19     def match_glob (self, attr, glob, value=None) :
    20         """
    20         """
    21             XXX: Match given prog as glob?
    21             Match prog as glob.
    22         """
    22         """
    23 
    23 
    24         if not glob :
    24         if not value :
    25             # ignore
       
    26             return None
       
    27 
       
    28         if not prog :
       
    29             # require
    25             # require
    30             return False
    26             return False
    31 
    27 
    32         # normalize
    28         # normalize
    33         prog = prog.strip().lower()
    29         value = value.strip()
    34         _, prog = os.path.split(prog)
       
    35 
    30 
    36         # match
    31         # match
    37         if fnmatch.fnmatch(prog, glob) :
    32         if fnmatch.fnmatch(value, glob) :
    38             return { attr: prog }
    33             return { attr: value }
    39         else :
    34         else :
    40             return False
    35             return False
       
    36  
       
    37     match_facility = match_glob
    41 
    38 
    42     def match (self, attr, regex, value=None) :
    39     def match_prog (self, attr, glob, prog=None) :
       
    40         """
       
    41             Match prog as glob.
       
    42         """
       
    43 
       
    44         if prog :
       
    45             # normalize
       
    46             prog = prog.strip().lower()
       
    47             _, prog = os.path.split(prog)
       
    48 
       
    49         # match
       
    50         return self.match_glob(attr, glob, prog)
       
    51 
       
    52     REGEX_TYPE = type(re.compile(''))
       
    53 
       
    54     def match_regex (self, attr, regex, value=None) :
    43         """
    55         """
    44             Match given value against given pattern.
    56             Match given value against given pattern.
    45         """
    57         """
    46 
       
    47         if not regex :
       
    48             # ignore
       
    49             return None
       
    50 
    58 
    51         if not value :
    59         if not value :
    52             # XXX: optional = match empty string?
    60             # XXX: optional = match empty string?
    53             value = ''
    61             value = ''
    54         else :
    62         else :
    76 
    84 
    77         match = None
    85         match = None
    78         matches = {}
    86         matches = {}
    79 
    87 
    80         for attr in self.filters :
    88         for attr in self.filters :
    81             # lookup match-func
       
    82             match = getattr(self, 'match_{attr}'.format(attr=attr), self.match)
       
    83 
       
    84             # filter
    89             # filter
    85             filter = self.filters[attr]
    90             filter = self.filters[attr]
       
    91 
       
    92             if not filter :
       
    93                 # ignore
       
    94                 continue
    86             
    95             
       
    96             # lookup match-func
       
    97             match = getattr(self, 'match_{attr}'.format(attr=attr), None)
       
    98 
       
    99             if match :
       
   100                 pass
       
   101 
       
   102             elif isinstance(filter, self.REGEX_TYPE) :
       
   103                 match = self.match_regex
       
   104 
       
   105             else :
       
   106                 match = self.match_glob
       
   107 
    87             # apply match
   108             # apply match
    88             if attr in item :
   109             if attr in item :
    89                 match = match(attr, filter, item[attr])
   110                 match = match(attr, filter, item[attr])
    90             else :
   111             else :
    91                 match = match(attr, filter)
   112                 match = match(attr, filter)
    94 
   115 
    95             if match :
   116             if match :
    96                 # match
   117                 # match
    97                 matches.update(match)
   118                 matches.update(match)
    98             
   119             
    99             elif match is None :
       
   100                 # ignore
       
   101                 continue
       
   102 
       
   103             else :
   120             else :
   104                 # reject
   121                 # reject
   105                 return
   122                 return
   106         
   123         
   107         # XXX: test last match, in case they were all None
   124         # test last match
   108         if match is None :
   125         if match is None :
   109             # XXX: empty filter!?
   126             # empty filter -> all None
   110             return True
   127             return True
   111         else :
   128         else :
   112             return matches
   129             return matches
   113 
   130 
   114     def process (self, items) :
   131     def process (self, items) :