<s> from __future__ import absolute_import <EOL> from apscheduler . jobstores . base import BaseJobStore , JobLookupError , ConflictingIdError <EOL> from apscheduler . util import datetime_to_utc_timestamp <EOL> class MemoryJobStore ( BaseJobStore ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( MemoryJobStore , self ) . __init__ ( ) <EOL> self . _jobs = [ ] <EOL> self . _jobs_index = { } <EOL> def lookup_job ( self , job_id ) : <EOL> return self . _jobs_index . get ( job_id , ( None , None ) ) [ <NUM_LIT:0> ] <EOL> def get_due_jobs ( self , now ) : <EOL> now_timestamp = datetime_to_utc_timestamp ( now ) <EOL> pending = [ ] <EOL> for job , timestamp in self . _jobs : <EOL> if timestamp is None or timestamp > now_timestamp : <EOL> break <EOL> pending . append ( job ) <EOL> return pending <EOL> def get_next_run_time ( self ) : <EOL> return self . _jobs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . next_run_time if self . _jobs else None <EOL> def get_all_jobs ( self ) : <EOL> return [ j [ <NUM_LIT:0> ] for j in self . _jobs ] <EOL> def add_job ( self , job ) : <EOL> if job . id in self . _jobs_index : <EOL> raise ConflictingIdError ( job . id ) <EOL> timestamp = datetime_to_utc_timestamp ( job . next_run_time ) <EOL> index = self . _get_job_index ( timestamp , job . id ) <EOL> self . _jobs . insert ( index , ( job , timestamp ) ) <EOL> self . _jobs_index [ job . id ] = ( job , timestamp ) <EOL> def update_job ( self , job ) : <EOL> old_job , old_timestamp = self . _jobs_index . get ( job . id , ( None , None ) ) <EOL> if old_job is None : <EOL> raise JobLookupError ( job . id ) <EOL> old_index = self . _get_job_index ( old_timestamp , old_job . id ) <EOL> new_timestamp = datetime_to_utc_timestamp ( job . next_run_time ) <EOL> if old_timestamp == new_timestamp : <EOL> self . _jobs [ old_index ] = ( job , new_timestamp ) <EOL> else : <EOL> del self . _jobs [ old_index ] <EOL> new_index = self . _get_job_index ( new_timestamp , job . id ) <EOL> self . _jobs . insert ( new_index , ( job , new_timestamp ) ) <EOL> self . _jobs_index [ old_job . id ] = ( job , new_timestamp ) <EOL> def remove_job ( self , job_id ) : <EOL> job , timestamp = self . _jobs_index . get ( job_id , ( None , None ) ) <EOL> if job is None : <EOL> raise JobLookupError ( job_id ) <EOL> index = self . _get_job_index ( timestamp , job_id ) <EOL> del self . _jobs [ index ] <EOL> del self . _jobs_index [ job . id ] <EOL> def remove_all_jobs ( self ) : <EOL> self . _jobs = [ ] <EOL> self . _jobs_index = { } <EOL> def shutdown ( self ) : <EOL> self . remove_all_jobs ( ) <EOL> def _get_job_index ( self , timestamp , job_id ) : <EOL> """<STR_LIT>""" <EOL> lo , hi = <NUM_LIT:0> , len ( self . _jobs ) <EOL> timestamp = float ( '<STR_LIT>' ) if timestamp is None else timestamp <EOL> while lo < hi : <EOL> mid = ( lo + hi ) // <NUM_LIT:2> <EOL> mid_job , mid_timestamp = self . _jobs [ mid ] <EOL> mid_timestamp = float ( '<STR_LIT>' ) if mid_timestamp is None else mid_timestamp <EOL> if mid_timestamp > timestamp : <EOL> hi = mid <EOL> elif mid_timestamp < timestamp : <EOL> lo = mid + <NUM_LIT:1> <EOL> elif mid_job . id > job_id : <EOL> hi = mid <EOL> elif mid_job . id < job_id : <EOL> lo = mid + <NUM_LIT:1> <EOL> else : <EOL> return mid <EOL> return lo </s>
