<s> import sys <EOL> import os <EOL> import types <EOL> import re <EOL> import traceback <EOL> from cStringIO import StringIO <EOL> from romeo import entity <EOL> class DirectiveException ( Exception ) : pass <EOL> class Preprocessor ( entity . Entity ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , rootdir ) : <EOL> self . root = rootdir <EOL> self . group_states = { } <EOL> self . directives = [ ] <EOL> self . load_directives ( [ Directive ] ) <EOL> def load_directives ( self , inherits ) : <EOL> '''<STR_LIT>''' <EOL> m = self . __class__ . __module__ <EOL> exclude = [ '<STR_LIT>' ] <EOL> relpath = sys . modules [ m ] . __file__ <EOL> reldir = os . path . split ( relpath ) [ <NUM_LIT:0> ] <EOL> fulldir = os . path . realpath ( reldir ) <EOL> pyfiles = [ ] <EOL> for f in os . listdir ( fulldir ) : <EOL> ff = os . path . join ( fulldir , f ) <EOL> if os . path . isfile ( ff ) : pyfiles . append ( f ) <EOL> pyfiles = [ f for f in pyfiles if f . endswith ( "<STR_LIT>" ) ] <EOL> pyfiles = [ f for f in pyfiles if f not in exclude ] <EOL> pyfiles = [ f [ : - <NUM_LIT:3> ] for f in pyfiles ] <EOL> candidates = [ ] <EOL> for py in pyfiles : <EOL> m = __import__ ( py , globals ( ) , locals ( ) , "<STR_LIT>" ) <EOL> for i in dir ( m ) : <EOL> attr = getattr ( m , i ) <EOL> if not type ( attr ) == types . TypeType : continue <EOL> matches = [ cls for cls in inherits <EOL> if issubclass ( attr , cls ) <EOL> if attr . __name__ != cls . __name__ ] <EOL> if len ( matches ) == len ( inherits ) : candidates . append ( attr ) <EOL> for c in candidates : <EOL> cinst = c ( c . name , self . root , * c . init_args ( self ) , ** c . init_kwargs ( self ) ) <EOL> self . directives . append ( cinst ) <EOL> def _process ( self , name , data , mode ) : <EOL> '''<STR_LIT>''' <EOL> td = data <EOL> for d in self . directives : <EOL> if not d . supports ( mode ) : continue <EOL> d . set_mode ( mode ) <EOL> try : <EOL> d . load ( name , data ) <EOL> if not d . is_used ( ) : continue <EOL> d . is_valid ( ) <EOL> data = d . apply ( ) <EOL> d . reset ( ) <EOL> except DirectiveException : <EOL> d . reset ( ) <EOL> traceback . print_exc ( ) <EOL> return data <EOL> def pre_process ( self , fd , name ) : <EOL> '''<STR_LIT>''' <EOL> data = fd . read ( ) <EOL> mdata = self . _process ( name , data , "<STR_LIT>" ) <EOL> while mdata != data : <EOL> data = mdata <EOL> mdata = self . _process ( name , data , "<STR_LIT>" ) <EOL> return mdata <EOL> def post_process ( self , obj , name ) : <EOL> '''<STR_LIT>''' <EOL> data = obj <EOL> mdata = self . _process ( name , data , '<STR_LIT>' ) <EOL> while mdata != data : <EOL> data = mdata <EOL> mdata = self . _process ( name , data , "<STR_LIT>" ) <EOL> return mdata <EOL> def shutdown ( self ) : <EOL> del self . directives [ : ] <EOL> for k in self . group_states . keys ( ) : <EOL> del self . group_states [ k ] <EOL> def get_group_state ( self , groupID ) : <EOL> '''<STR_LIT>''' <EOL> if groupID in self . group_states : <EOL> return self . group_states [ groupID ] <EOL> else : <EOL> self . group_states [ groupID ] = { } <EOL> return self . group_states [ groupID ] <EOL> class Directive ( object ) : <EOL> '''<STR_LIT>''' <EOL> name = "<STR_LIT>" <EOL> modes = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def __init__ ( self , name , rootdir , * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> self . root = rootdir <EOL> self . name = name <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> self . data = None <EOL> self . filename = None <EOL> self . raw_pattern = "<STR_LIT>" % self . name <EOL> self . used_pattern = re . compile ( self . raw_pattern ) <EOL> self . mode = "<STR_LIT:none>" <EOL> def supports ( self , mode ) : <EOL> return mode in self . modes <EOL> def set_mode ( self , mode ) : <EOL> self . mode = mode <EOL> @ classmethod <EOL> def init_kwargs ( cls , preprocessor ) : <EOL> '''<STR_LIT>''' <EOL> return { } <EOL> @ classmethod <EOL> def init_args ( cls , preprocessor ) : <EOL> '''<STR_LIT>''' <EOL> return [ ] <EOL> def extract_args ( self , match_str ) : <EOL> '''<STR_LIT>''' <EOL> match = match_str . strip ( ) . replace ( "<STR_LIT:}>" , "<STR_LIT>" ) <EOL> match = match . split ( "<STR_LIT:U+0020>" ) [ <NUM_LIT:1> ] . strip ( ) <EOL> match = match . split ( "<STR_LIT:U+002C>" ) <EOL> return tuple ( match ) <EOL> def get_lines ( self ) : <EOL> '''<STR_LIT>''' <EOL> for line in self . data : <EOL> yield line <EOL> def load ( self , filename , data ) : <EOL> '''<STR_LIT>''' <EOL> self . filename = filename <EOL> if type ( data ) == str : <EOL> data = StringIO ( data ) <EOL> self . data = data <EOL> def reset ( self ) : <EOL> '''<STR_LIT>''' <EOL> if hasattr ( self . data , "<STR_LIT>" ) and callable ( self . data . close ) : <EOL> self . data . close ( ) <EOL> self . data = None <EOL> self . filename = None <EOL> def is_used ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . mode != "<STR_LIT>" : return True <EOL> strdata = self . data . read ( ) <EOL> self . data . seek ( <NUM_LIT:0> ) <EOL> if self . used_pattern . search ( strdata ) : return True <EOL> else : return False <EOL> def is_valid ( self ) : <EOL> '''<STR_LIT>''' <EOL> msg = "<STR_LIT>" <EOL> msg += "<STR_LIT>" <EOL> raise DirectiveException ( msg ) <EOL> def apply ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . data </s>
