<s> from datetime import datetime , timedelta <EOL> from tzlocal import get_localzone <EOL> import six <EOL> from apscheduler . triggers . base import BaseTrigger <EOL> from apscheduler . triggers . cron . fields import ( <EOL> BaseField , WeekField , DayOfMonthField , DayOfWeekField , DEFAULT_VALUES ) <EOL> from apscheduler . util import datetime_ceil , convert_to_datetime , datetime_repr , astimezone <EOL> class CronTrigger ( BaseTrigger ) : <EOL> """<STR_LIT>""" <EOL> FIELD_NAMES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> FIELDS_MAP = { <EOL> '<STR_LIT>' : BaseField , <EOL> '<STR_LIT>' : BaseField , <EOL> '<STR_LIT>' : WeekField , <EOL> '<STR_LIT>' : DayOfMonthField , <EOL> '<STR_LIT>' : DayOfWeekField , <EOL> '<STR_LIT>' : BaseField , <EOL> '<STR_LIT>' : BaseField , <EOL> '<STR_LIT>' : BaseField <EOL> } <EOL> __slots__ = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> def __init__ ( self , year = None , month = None , day = None , week = None , day_of_week = None , hour = None , <EOL> minute = None , second = None , start_date = None , end_date = None , timezone = None ) : <EOL> if timezone : <EOL> self . timezone = astimezone ( timezone ) <EOL> elif start_date and start_date . tzinfo : <EOL> self . timezone = start_date . tzinfo <EOL> elif end_date and end_date . tzinfo : <EOL> self . timezone = end_date . tzinfo <EOL> else : <EOL> self . timezone = get_localzone ( ) <EOL> self . start_date = convert_to_datetime ( start_date , self . timezone , '<STR_LIT>' ) <EOL> self . end_date = convert_to_datetime ( end_date , self . timezone , '<STR_LIT>' ) <EOL> values = dict ( ( key , value ) for ( key , value ) in six . iteritems ( locals ( ) ) <EOL> if key in self . FIELD_NAMES and value is not None ) <EOL> self . fields = [ ] <EOL> assign_defaults = False <EOL> for field_name in self . FIELD_NAMES : <EOL> if field_name in values : <EOL> exprs = values . pop ( field_name ) <EOL> is_default = False <EOL> assign_defaults = not values <EOL> elif assign_defaults : <EOL> exprs = DEFAULT_VALUES [ field_name ] <EOL> is_default = True <EOL> else : <EOL> exprs = '<STR_LIT:*>' <EOL> is_default = True <EOL> field_class = self . FIELDS_MAP [ field_name ] <EOL> field = field_class ( field_name , exprs , is_default ) <EOL> self . fields . append ( field ) <EOL> def _increment_field_value ( self , dateval , fieldnum ) : <EOL> """<STR_LIT>""" <EOL> values = { } <EOL> i = <NUM_LIT:0> <EOL> while i < len ( self . fields ) : <EOL> field = self . fields [ i ] <EOL> if not field . REAL : <EOL> if i == fieldnum : <EOL> fieldnum -= <NUM_LIT:1> <EOL> i -= <NUM_LIT:1> <EOL> else : <EOL> i += <NUM_LIT:1> <EOL> continue <EOL> if i < fieldnum : <EOL> values [ field . name ] = field . get_value ( dateval ) <EOL> i += <NUM_LIT:1> <EOL> elif i > fieldnum : <EOL> values [ field . name ] = field . get_min ( dateval ) <EOL> i += <NUM_LIT:1> <EOL> else : <EOL> value = field . get_value ( dateval ) <EOL> maxval = field . get_max ( dateval ) <EOL> if value == maxval : <EOL> fieldnum -= <NUM_LIT:1> <EOL> i -= <NUM_LIT:1> <EOL> else : <EOL> values [ field . name ] = value + <NUM_LIT:1> <EOL> i += <NUM_LIT:1> <EOL> difference = datetime ( ** values ) - dateval . replace ( tzinfo = None ) <EOL> return self . timezone . normalize ( dateval + difference ) , fieldnum <EOL> def _set_field_value ( self , dateval , fieldnum , new_value ) : <EOL> values = { } <EOL> for i , field in enumerate ( self . fields ) : <EOL> if field . REAL : <EOL> if i < fieldnum : <EOL> values [ field . name ] = field . get_value ( dateval ) <EOL> elif i > fieldnum : <EOL> values [ field . name ] = field . get_min ( dateval ) <EOL> else : <EOL> values [ field . name ] = new_value <EOL> return self . timezone . localize ( datetime ( ** values ) ) <EOL> def get_next_fire_time ( self , previous_fire_time , now ) : <EOL> if previous_fire_time : <EOL> start_date = min ( now , previous_fire_time + timedelta ( microseconds = <NUM_LIT:1> ) ) <EOL> else : <EOL> start_date = max ( now , self . start_date ) if self . start_date else now <EOL> fieldnum = <NUM_LIT:0> <EOL> next_date = datetime_ceil ( start_date ) . astimezone ( self . timezone ) <EOL> while <NUM_LIT:0> <= fieldnum < len ( self . fields ) : <EOL> field = self . fields [ fieldnum ] <EOL> curr_value = field . get_value ( next_date ) <EOL> next_value = field . get_next_value ( next_date ) <EOL> if next_value is None : <EOL> next_date , fieldnum = self . _increment_field_value ( next_date , fieldnum - <NUM_LIT:1> ) <EOL> elif next_value > curr_value : <EOL> if field . REAL : <EOL> next_date = self . _set_field_value ( next_date , fieldnum , next_value ) <EOL> fieldnum += <NUM_LIT:1> <EOL> else : <EOL> next_date , fieldnum = self . _increment_field_value ( next_date , fieldnum ) <EOL> else : <EOL> fieldnum += <NUM_LIT:1> <EOL> if self . end_date and next_date > self . end_date : <EOL> return None <EOL> if fieldnum >= <NUM_LIT:0> : <EOL> return next_date <EOL> def __getstate__ ( self ) : <EOL> return { <EOL> '<STR_LIT:version>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : self . timezone , <EOL> '<STR_LIT>' : self . start_date , <EOL> '<STR_LIT>' : self . end_date , <EOL> '<STR_LIT>' : self . fields <EOL> } <EOL> def __setstate__ ( self , state ) : <EOL> if isinstance ( state , tuple ) : <EOL> state = state [ <NUM_LIT:1> ] <EOL> if state . get ( '<STR_LIT:version>' , <NUM_LIT:1> ) > <NUM_LIT:1> : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % <EOL> ( state [ '<STR_LIT:version>' ] , self . __class__ . __name__ ) ) <EOL> self . timezone = state [ '<STR_LIT>' ] <EOL> self . start_date = state [ '<STR_LIT>' ] <EOL> self . end_date = state [ '<STR_LIT>' ] <EOL> self . fields = state [ '<STR_LIT>' ] <EOL> def __str__ ( self ) : <EOL> options = [ "<STR_LIT>" % ( f . name , f ) for f in self . fields if not f . is_default ] <EOL> return '<STR_LIT>' % ( '<STR_LIT:U+002CU+0020>' . join ( options ) ) <EOL> def __repr__ ( self ) : <EOL> options = [ "<STR_LIT>" % ( f . name , f ) for f in self . fields if not f . is_default ] <EOL> if self . start_date : <EOL> options . append ( "<STR_LIT>" % datetime_repr ( self . start_date ) ) <EOL> return "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , '<STR_LIT:U+002CU+0020>' . join ( options ) , self . timezone ) </s>
