<s> """<STR_LIT>""" <EOL> import re <EOL> from nagcat import errors <EOL> class ObjectParser ( object ) : <EOL> """<STR_LIT>""" <EOL> UNESCAPE = re . compile ( r'<STR_LIT>' ) <EOL> def __init__ ( self , object_file , object_types = ( ) , object_select = ( ) ) : <EOL> self . _objects = { } <EOL> object_select = dict ( object_select ) <EOL> try : <EOL> if isinstance ( object_file , basestring ) : <EOL> fd = open ( object_file ) <EOL> try : <EOL> self . _parse ( fd , object_types , object_select ) <EOL> finally : <EOL> fd . close ( ) <EOL> else : <EOL> self . _parse ( object_file , object_types , object_select ) <EOL> except IOError , ex : <EOL> raise errors . InitError ( <EOL> "<STR_LIT>" % ex ) <EOL> def _parse ( self , object_file , object_types , object_select ) : <EOL> def unescape ( match ) : <EOL> esc = match . group ( <NUM_LIT:1> ) <EOL> if esc == r'<STR_LIT:\\>' : <EOL> return '<STR_LIT:\\>' <EOL> elif esc == r'<STR_LIT:\n>' : <EOL> return '<STR_LIT:\n>' <EOL> elif esc == r'<STR_LIT>' : <EOL> return '<STR_LIT:|>' <EOL> else : <EOL> assert <NUM_LIT:0> <EOL> for type_ in object_types : <EOL> self . _objects [ type_ ] = [ ] <EOL> splitter = None <EOL> object_data = None <EOL> object_type = None <EOL> for line in object_file : <EOL> line = line . strip ( ) <EOL> if object_data is None : <EOL> if line . startswith ( "<STR_LIT>" ) and line . endswith ( '<STR_LIT:{>' ) : <EOL> type_ = line [ <NUM_LIT:7> : - <NUM_LIT:2> ] <EOL> splitter = None <EOL> elif line . endswith ( '<STR_LIT>' ) : <EOL> splitter = '<STR_LIT:=>' <EOL> type_ = line [ : - <NUM_LIT:8> ] <EOL> elif line . endswith ( '<STR_LIT>' ) : <EOL> splitter = '<STR_LIT:=>' <EOL> type_ = line [ : - <NUM_LIT:2> ] <EOL> else : <EOL> continue <EOL> assert type_ <EOL> if object_types and type_ not in object_types : <EOL> continue <EOL> object_data = { } <EOL> object_type = type_ <EOL> elif line == '<STR_LIT:}>' : <EOL> if object_type not in self . _objects : <EOL> self . _objects [ object_type ] = [ object_data ] <EOL> else : <EOL> self . _objects [ object_type ] . append ( object_data ) <EOL> object_data = None <EOL> object_type = None <EOL> else : <EOL> split = line . split ( splitter , <NUM_LIT:1> ) <EOL> try : <EOL> key , value = split <EOL> except ValueError : <EOL> key = split [ <NUM_LIT:0> ] <EOL> value = "<STR_LIT>" <EOL> if object_select and key in object_select : <EOL> selector = object_select [ key ] <EOL> if isinstance ( selector , basestring ) : <EOL> if value != selector : <EOL> object_data = None <EOL> object_type = None <EOL> continue <EOL> else : <EOL> if value not in selector : <EOL> object_data = None <EOL> object_type = None <EOL> continue <EOL> if '<STR_LIT:\\>' in value : <EOL> value = self . UNESCAPE . sub ( unescape , value ) <EOL> object_data [ key ] = value <EOL> def __getitem__ ( self , key ) : <EOL> return self . _objects [ key ] <EOL> def __contains__ ( self , key ) : <EOL> return key in self . _objects <EOL> def types ( self ) : <EOL> return self . _objects . keys ( ) </s>
