<s> from calvin . actor . actor import Actor , ActionResult , manage , condition , guard <EOL> from calvin . runtime . north . calvin_token import EOSToken , ExceptionToken <EOL> def absolute_filename ( filename ) : <EOL> """<STR_LIT>""" <EOL> import os . path <EOL> return os . path . join ( os . path . dirname ( __file__ ) , filename ) <EOL> class FileReader2 ( Actor ) : <EOL> """<STR_LIT>""" <EOL> @ manage ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def init ( self , status_OK = <NUM_LIT:0> , status_ERR = <NUM_LIT:1> ) : <EOL> self . status_OK = status_OK <EOL> self . status_ERR = status_ERR <EOL> self . file_not_found = False <EOL> self . file = None <EOL> self . use ( '<STR_LIT>' , shorthand = "<STR_LIT:file>" ) <EOL> @ condition ( [ '<STR_LIT:filename>' ] , [ ] ) <EOL> @ guard ( lambda self , filename : not self . file ) <EOL> def open_file ( self , filename ) : <EOL> try : <EOL> self . file = self [ '<STR_LIT:file>' ] . open ( filename , "<STR_LIT:r>" ) <EOL> except : <EOL> self . file = None <EOL> self . file_not_found = True <EOL> return ActionResult ( ) <EOL> @ condition ( [ ] , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> @ guard ( lambda self : self . file_not_found ) <EOL> def file_not_found ( self ) : <EOL> self . file_not_found = False <EOL> return ActionResult ( production = ( EOSToken ( ) , self . status_ERR ) ) <EOL> @ condition ( [ ] , [ '<STR_LIT>' ] ) <EOL> @ guard ( lambda self : self . file and not self . file . eof ( ) and self . file . has_data ( ) ) <EOL> def readline ( self ) : <EOL> line = self . file . read_line ( ) <EOL> return ActionResult ( production = ( line , ) ) <EOL> @ condition ( [ ] , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> @ guard ( lambda self : self . file and self . file . eof ( ) ) <EOL> def eof ( self ) : <EOL> self [ '<STR_LIT:file>' ] . close ( self . file ) <EOL> self . file = None <EOL> return ActionResult ( production = ( EOSToken ( ) , self . status_OK ) ) <EOL> action_priority = ( readline , eof , open_file , file_not_found ) <EOL> requires = [ '<STR_LIT>' ] <EOL> test_set = [ <EOL> { <EOL> '<STR_LIT>' : { '<STR_LIT:filename>' : "<STR_LIT>" } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ "<STR_LIT>" ] , '<STR_LIT>' : [ <NUM_LIT:1> ] } <EOL> } , <EOL> { <EOL> '<STR_LIT>' : { '<STR_LIT:filename>' : absolute_filename ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:C>' , '<STR_LIT:D>' ] } <EOL> } , <EOL> { <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ '<STR_LIT:E>' , '<STR_LIT:F>' , '<STR_LIT>' , '<STR_LIT:H>' ] } <EOL> } , <EOL> { <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ '<STR_LIT:I>' , "<STR_LIT>" ] , '<STR_LIT>' : [ <NUM_LIT:0> ] } <EOL> } <EOL> ] </s>
