tomb

the crypto undertaker
git clone git://parazyd.org/tomb.git
Log | Files | Refs | README | LICENSE

parser.py (1185B)


      1 '''
      2 Utilities to analyze tomb output
      3 '''
      4 import re
      5 
      6 #found: [m] followed by some ID (usually "found") inside square brackets, then
      7 #something else, then a space, then the content
      8 _found_regex = re.compile(r'^\[m\]\[([^]]+)\] +(([^:]+)://(.+))$')
      9 #generic: programname, then some identifiers in square (or round) brackets,
     10 #then maybe something else, then a space, then the context
     11 _generic_regex = re.compile(r'^[a-z-]+ [[(]([^]]+)[\])] +(.+)$')
     12 types = {'E':'error', 'W':'warning', 'D':'debug', '*':'success'}
     13 def parse_line(line):
     14     '''Analyze a single line.
     15     Return None if no standard format is detected, a dict otherwise.
     16     The fields 'type' and 'content' are always in the dict; 'content' may be
     17     empty
     18     'type' can be 'error', 'progress'
     19     '''
     20 
     21 
     22     match = _found_regex.match(line)
     23     if match:
     24         return { 'type': types.get(match.group(1)) or match.group(1),
     25                 'content': match.group(2), 'scheme': match.group(3),
     26                 'path': match.group(4) }
     27     match = _generic_regex.search(line)
     28     if match:
     29         return { 'type': types.get(match.group(1)) or match.group(1),
     30                 'content': match.group(2) }
     31 
     32     return None
     33 
     34