<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> from docutils import languages , ApplicationError , TransformSpec <EOL> class TransformError ( ApplicationError ) : pass <EOL> class Transform : <EOL> """<STR_LIT>""" <EOL> default_priority = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , document , startnode = None ) : <EOL> """<STR_LIT>""" <EOL> self . document = document <EOL> """<STR_LIT>""" <EOL> self . startnode = startnode <EOL> """<STR_LIT>""" <EOL> self . language = languages . get_language ( <EOL> document . settings . language_code ) <EOL> """<STR_LIT>""" <EOL> def apply ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> class Transformer ( TransformSpec ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , document ) : <EOL> self . transforms = [ ] <EOL> """<STR_LIT>""" <EOL> self . unknown_reference_resolvers = [ ] <EOL> """<STR_LIT>""" <EOL> self . document = document <EOL> """<STR_LIT>""" <EOL> self . applied = [ ] <EOL> """<STR_LIT>""" <EOL> self . sorted = <NUM_LIT:0> <EOL> """<STR_LIT>""" <EOL> self . components = { } <EOL> """<STR_LIT>""" <EOL> self . serialno = <NUM_LIT:0> <EOL> """<STR_LIT>""" <EOL> def add_transform ( self , transform_class , priority = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if priority is None : <EOL> priority = transform_class . default_priority <EOL> priority_string = self . get_priority_string ( priority ) <EOL> self . transforms . append ( <EOL> ( priority_string , transform_class , None , kwargs ) ) <EOL> self . sorted = <NUM_LIT:0> <EOL> def add_transforms ( self , transform_list ) : <EOL> """<STR_LIT>""" <EOL> for transform_class in transform_list : <EOL> priority_string = self . get_priority_string ( <EOL> transform_class . default_priority ) <EOL> self . transforms . append ( <EOL> ( priority_string , transform_class , None , { } ) ) <EOL> self . sorted = <NUM_LIT:0> <EOL> def add_pending ( self , pending , priority = None ) : <EOL> """<STR_LIT>""" <EOL> transform_class = pending . transform <EOL> if priority is None : <EOL> priority = transform_class . default_priority <EOL> priority_string = self . get_priority_string ( priority ) <EOL> self . transforms . append ( <EOL> ( priority_string , transform_class , pending , { } ) ) <EOL> self . sorted = <NUM_LIT:0> <EOL> def get_priority_string ( self , priority ) : <EOL> """<STR_LIT>""" <EOL> self . serialno += <NUM_LIT:1> <EOL> return '<STR_LIT>' % ( priority , self . serialno ) <EOL> def populate_from_components ( self , components ) : <EOL> """<STR_LIT>""" <EOL> for component in components : <EOL> if component is None : <EOL> continue <EOL> self . add_transforms ( component . get_transforms ( ) ) <EOL> self . components [ component . component_type ] = component <EOL> self . sorted = <NUM_LIT:0> <EOL> unknown_reference_resolvers = [ ] <EOL> for i in components : <EOL> unknown_reference_resolvers . extend ( i . unknown_reference_resolvers ) <EOL> decorated_list = [ ( f . priority , f ) for f in unknown_reference_resolvers ] <EOL> decorated_list . sort ( ) <EOL> self . unknown_reference_resolvers . extend ( [ f [ <NUM_LIT:1> ] for f in decorated_list ] ) <EOL> def apply_transforms ( self ) : <EOL> """<STR_LIT>""" <EOL> self . document . reporter . attach_observer ( <EOL> self . document . note_transform_message ) <EOL> while self . transforms : <EOL> if not self . sorted : <EOL> self . transforms . sort ( ) <EOL> self . transforms . reverse ( ) <EOL> self . sorted = <NUM_LIT:1> <EOL> priority , transform_class , pending , kwargs = self . transforms . pop ( ) <EOL> transform = transform_class ( self . document , startnode = pending ) <EOL> transform . apply ( ** kwargs ) <EOL> self . applied . append ( ( priority , transform_class , pending , kwargs ) ) </s>
