<s> import os <EOL> import IECore <EOL> import Gaffer <EOL> import GafferDispatch <EOL> class TextWriter ( GafferDispatch . ExecutableNode ) : <EOL> def __init__ ( self , name = "<STR_LIT>" , requiresSequenceExecution = False ) : <EOL> GafferDispatch . ExecutableNode . __init__ ( self , name ) <EOL> self . __requiresSequenceExecution = requiresSequenceExecution <EOL> self . addChild ( Gaffer . StringPlug ( "<STR_LIT>" , Gaffer . Plug . Direction . In ) ) <EOL> self . addChild ( Gaffer . StringPlug ( "<STR_LIT>" , defaultValue = "<STR_LIT:w>" , direction = Gaffer . Plug . Direction . In ) ) <EOL> self . addChild ( Gaffer . StringPlug ( "<STR_LIT:text>" , Gaffer . Plug . Direction . In ) ) <EOL> def execute ( self ) : <EOL> context = Gaffer . Context . current ( ) <EOL> fileName = self [ "<STR_LIT>" ] . getValue ( ) <EOL> directory = os . path . dirname ( fileName ) <EOL> if directory : <EOL> try : <EOL> os . makedirs ( directory ) <EOL> except OSError : <EOL> if not os . path . isdir ( directory ) : <EOL> raise <EOL> text = self . __processText ( context ) <EOL> with file ( fileName , self [ "<STR_LIT>" ] . getValue ( ) ) as f : <EOL> f . write ( text ) <EOL> def executeSequence ( self , frames ) : <EOL> if not self . __requiresSequenceExecution : <EOL> GafferDispatch . ExecutableNode . executeSequence ( self , frames ) <EOL> return <EOL> context = Gaffer . Context ( Gaffer . Context . current ( ) ) <EOL> fileName = self [ "<STR_LIT>" ] . getValue ( ) <EOL> with file ( fileName , self [ "<STR_LIT>" ] . getValue ( ) ) as f : <EOL> with context : <EOL> for frame in frames : <EOL> context . setFrame ( frame ) <EOL> text = self . __processText ( context ) <EOL> f . write ( text ) <EOL> def hash ( self , context ) : <EOL> h = GafferDispatch . ExecutableNode . hash ( self , context ) <EOL> h . append ( context . getFrame ( ) ) <EOL> h . append ( context . get ( "<STR_LIT>" , IECore . StringVectorData ( ) ) ) <EOL> self [ "<STR_LIT>" ] . hash ( h ) <EOL> self [ "<STR_LIT>" ] . hash ( h ) <EOL> self [ "<STR_LIT:text>" ] . hash ( h ) <EOL> return h <EOL> def requiresSequenceExecution ( self ) : <EOL> return self . __requiresSequenceExecution <EOL> def __processText ( self , context ) : <EOL> text = self [ "<STR_LIT:text>" ] . getValue ( ) <EOL> replace = context . get ( "<STR_LIT>" , IECore . StringVectorData ( ) ) <EOL> if replace and len ( replace ) == <NUM_LIT:2> : <EOL> text = text . replace ( replace [ <NUM_LIT:0> ] , replace [ <NUM_LIT:1> ] ) <EOL> return text <EOL> IECore . registerRunTimeTyped ( TextWriter , typeName = "<STR_LIT>" ) </s>
