URI:
       tparser.py - tomb - the crypto undertaker
  HTML git clone git://parazyd.org/tomb.git
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tparser.py (1184B)
       ---
            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 
           14 
           15 def parse_line(line):
           16     '''Analyze a single line.
           17     Return None if no standard format is detected, a dict otherwise.
           18     The fields 'type' and 'content' are always in the dict; 'content' may be
           19     empty
           20     'type' can be 'error', 'progress'
           21     '''
           22 
           23     match = _found_regex.match(line)
           24     if match:
           25         return {'type': types.get(match.group(1)) or match.group(1),
           26                 'content': match.group(2), 'scheme': match.group(3),
           27                 'path': match.group(4)}
           28     match = _generic_regex.search(line)
           29     if match:
           30         return {'type': types.get(match.group(1)) or match.group(1),
           31                 'content': match.group(2)}
           32 
           33     return None