<s> from datetime import timedelta <EOL> class RecordingPlanner : <EOL> MINUTES_START_BEFORE = <NUM_LIT:2> <EOL> MINUTES_CONTINUE_AFTER = <NUM_LIT:3> <EOL> def __init__ ( self ) : <EOL> self . _recordings = [ ] <EOL> def read ( self ) : <EOL> from . master_import import Schedule , Programme <EOL> from datetime import datetime <EOL> from sqlalchemy import or_ <EOL> self . _recordings = [ ] <EOL> clauses = [ Schedule . stop_time == None , Schedule . stop_time >= datetime . now ( ) ] <EOL> specifications = Schedule . query . filter ( Schedule . type == '<STR_LIT>' ) . filter ( or_ ( * clauses ) ) . all ( ) <EOL> exceptions = Schedule . query . filter ( Schedule . type == '<STR_LIT>' ) . filter ( or_ ( * clauses ) ) . all ( ) <EOL> for specification in specifications : <EOL> title = specification . title <EOL> if specification . channel and specification . start_time : <EOL> programmes = Programme . on_channel_with_title_and_start_time ( specification . channel , specification . title , specification . start_time ) <EOL> elif specification . channel : <EOL> programmes = Programme . on_channel_with_title ( specification . channel , specification . title ) <EOL> else : <EOL> programmes = Programme . with_title ( specification . title ) <EOL> programmes_with_exceptions_removed = self . remove_excluded_programmes ( programmes , exceptions ) <EOL> self . _add_programmes ( title , programmes_with_exceptions_removed , specification ) <EOL> if self . _recordings : <EOL> from . pvr_initializer import scheduler <EOL> scheduler ( ) . recordings ( self . _recordings ) <EOL> def simple ( self , title , channel , start_time , duration ) : <EOL> self . _add_recording ( self , title , channel , start_time , duration ) <EOL> def remove_excluded_programmes ( self , programmes , exceptions ) : <EOL> for programme in programmes : <EOL> if self . _matches_exception ( programme , exceptions ) : <EOL> programmes . remove ( programme ) <EOL> return programmes <EOL> def _matches_exception ( self , programme , exceptions ) : <EOL> match = False <EOL> for exception in exceptions : <EOL> match = ( programme . title == exception . title and programme . channel == exception . channel and programme . start_time == exception . start_time ) <EOL> if match : <EOL> return True <EOL> return False <EOL> def _add_programmes ( self , title , programmes , schedule ) : <EOL> for programme in programmes : <EOL> start_time = programme . start_time - timedelta ( minutes = RecordingPlanner . MINUTES_START_BEFORE ) <EOL> stop_time = programme . stop_time + timedelta ( minutes = RecordingPlanner . MINUTES_CONTINUE_AFTER ) <EOL> duration = ( stop_time - start_time ) . seconds <EOL> self . _add_recording ( title , programme . channel , start_time , stop_time , duration , programme , schedule ) <EOL> self . _recordings . sort ( key = lambda rec : rec . start_time ) <EOL> def _add_recording ( self , title , channel , start_time , stop_time , duration , programme = None , schedule = None ) : <EOL> from . master_import import Recording <EOL> self . _recordings . append ( Recording ( channel = channel , show_name = title , start_time = start_time , stop_time = stop_time , duration = duration , programme = programme , schedule = schedule ) ) </s>
