<s> from datetime import datetime , time <EOL> import urwid <EOL> from . widgets import DateWidget , TimeWidget , NColumns , NPile , ValidatedEdit <EOL> from . calendarwidget import CalendarWidget <EOL> class DateConversionError ( Exception ) : <EOL> pass <EOL> class StartEnd ( object ) : <EOL> def __init__ ( self , startdate , starttime , enddate , endtime ) : <EOL> """<STR_LIT>""" <EOL> self . startdate = startdate <EOL> self . starttime = starttime <EOL> self . enddate = enddate <EOL> self . endtime = endtime <EOL> class CalendarPopUp ( urwid . PopUpLauncher ) : <EOL> def __init__ ( self , widget , conf , on_date_change ) : <EOL> self . _conf = conf <EOL> self . _on_date_change = on_date_change <EOL> self . __super . __init__ ( widget ) <EOL> def keypress ( self , size , key ) : <EOL> if key == '<STR_LIT>' : <EOL> self . open_pop_up ( ) <EOL> else : <EOL> return super ( ) . keypress ( size , key ) <EOL> def create_pop_up ( self ) : <EOL> def on_change ( new_date ) : <EOL> self . _get_base_widget ( ) . set_value ( new_date ) <EOL> self . _on_date_change ( new_date ) <EOL> keybindings = self . _conf [ '<STR_LIT>' ] <EOL> on_press = { '<STR_LIT>' : lambda _ , __ : self . close_pop_up ( ) , <EOL> '<STR_LIT>' : lambda _ , __ : self . close_pop_up ( ) } <EOL> pop_up = CalendarWidget ( <EOL> on_change , keybindings , on_press , <EOL> firstweekday = self . _conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> weeknumbers = self . _conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> initial = self . base_widget . _get_current_value ( ) ) <EOL> pop_up = urwid . LineBox ( pop_up ) <EOL> return pop_up <EOL> def get_pop_up_parameters ( self ) : <EOL> width = <NUM_LIT> if self . _conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] == '<STR_LIT:right>' else <NUM_LIT> <EOL> return { '<STR_LIT:left>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : width , '<STR_LIT>' : <NUM_LIT:8> } <EOL> class StartEndEditor ( urwid . WidgetWrap ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , start , end , conf , on_date_change = lambda x : None ) : <EOL> """<STR_LIT>""" <EOL> self . allday = not isinstance ( start , datetime ) <EOL> self . conf = conf <EOL> self . _startdt , self . _original_start = start , start <EOL> self . _enddt , self . _original_end = end , end <EOL> self . on_date_change = on_date_change <EOL> self . _datewidth = len ( start . strftime ( self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) ) <EOL> self . _timewidth = len ( start . strftime ( self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) ) <EOL> self . widgets = StartEnd ( None , None , None , None ) <EOL> self . checkallday = urwid . CheckBox ( '<STR_LIT>' , state = self . allday , <EOL> on_state_change = self . toggle ) <EOL> self . toggle ( None , self . allday ) <EOL> def keypress ( self , size , key ) : <EOL> return super ( ) . keypress ( size , key ) <EOL> @ property <EOL> def startdt ( self ) : <EOL> if self . allday and isinstance ( self . _startdt , datetime ) : <EOL> return self . _startdt . date ( ) <EOL> else : <EOL> return self . _startdt <EOL> @ property <EOL> def _start_time ( self ) : <EOL> try : <EOL> return self . _startdt . time ( ) <EOL> except AttributeError : <EOL> return time ( <NUM_LIT:0> ) <EOL> @ property <EOL> def localize_start ( self ) : <EOL> if getattr ( self . startdt , '<STR_LIT>' , None ) is None : <EOL> return self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] . localize <EOL> else : <EOL> return self . startdt . tzinfo . localize <EOL> @ property <EOL> def localize_end ( self ) : <EOL> if getattr ( self . enddt , '<STR_LIT>' , None ) is None : <EOL> return self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] . localize <EOL> else : <EOL> return self . enddt . tzinfo . localize <EOL> @ property <EOL> def enddt ( self ) : <EOL> if self . allday and isinstance ( self . _enddt , datetime ) : <EOL> return self . _enddt . date ( ) <EOL> else : <EOL> return self . _enddt <EOL> @ property <EOL> def _end_time ( self ) : <EOL> try : <EOL> return self . _enddt . time ( ) <EOL> except AttributeError : <EOL> return time ( <NUM_LIT:0> ) <EOL> def _validate_start_time ( self , text ) : <EOL> try : <EOL> startval = datetime . strptime ( text , self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . _startdt = self . localize_start ( <EOL> datetime . combine ( self . _startdt . date ( ) , startval . time ( ) ) ) <EOL> except ValueError : <EOL> return False <EOL> else : <EOL> return startval <EOL> def _validate_start_date ( self , text ) : <EOL> try : <EOL> startval = datetime . strptime ( text , self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . _startdt = self . localize_start ( <EOL> datetime . combine ( startval . date ( ) , self . _start_time ) ) <EOL> except ValueError : <EOL> return False <EOL> else : <EOL> return startval <EOL> def _validate_end_time ( self , text ) : <EOL> try : <EOL> endval = datetime . strptime ( text , self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . _enddt = self . localize_end ( datetime . combine ( self . _enddt . date ( ) , endval . time ( ) ) ) <EOL> except ValueError : <EOL> return False <EOL> else : <EOL> return endval <EOL> def _validate_end_date ( self , text ) : <EOL> try : <EOL> endval = datetime . strptime ( text , self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . _enddt = self . localize_end ( datetime . combine ( endval . date ( ) , self . _end_time ) ) <EOL> except ValueError : <EOL> return False <EOL> else : <EOL> return endval <EOL> def toggle ( self , checkbox , state ) : <EOL> """<STR_LIT>""" <EOL> if self . allday is True and state is False : <EOL> self . _startdt = datetime . combine ( self . _startdt , datetime . min . time ( ) ) <EOL> self . _enddt = datetime . combine ( self . _enddt , datetime . min . time ( ) ) <EOL> elif self . allday is False and state is True : <EOL> self . _startdt = self . _startdt . date ( ) <EOL> self . _enddt = self . _enddt . date ( ) <EOL> self . allday = state <EOL> datewidth = self . _datewidth + <NUM_LIT:7> <EOL> edit = ValidatedEdit ( <EOL> dateformat = self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> EditWidget = DateWidget , <EOL> validate = self . _validate_start_date , <EOL> caption = ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> edit_text = self . startdt . strftime ( self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <EOL> on_date_change = self . on_date_change ) <EOL> edit = CalendarPopUp ( edit , self . conf , self . on_date_change ) <EOL> edit = urwid . Padding ( edit , align = '<STR_LIT:left>' , width = datewidth , left = <NUM_LIT:0> , right = <NUM_LIT:1> ) <EOL> self . widgets . startdate = edit <EOL> edit = ValidatedEdit ( <EOL> dateformat = self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> EditWidget = DateWidget , <EOL> validate = self . _validate_end_date , <EOL> caption = ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> edit_text = self . enddt . strftime ( self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <EOL> on_date_change = self . on_date_change ) <EOL> edit = CalendarPopUp ( edit , self . conf , self . on_date_change ) <EOL> edit = urwid . Padding ( edit , align = '<STR_LIT:left>' , width = datewidth , left = <NUM_LIT:0> , right = <NUM_LIT:1> ) <EOL> self . widgets . enddate = edit <EOL> if state is True : <EOL> timewidth = <NUM_LIT:1> <EOL> self . widgets . starttime = urwid . Text ( '<STR_LIT>' ) <EOL> self . widgets . endtime = urwid . Text ( '<STR_LIT>' ) <EOL> elif state is False : <EOL> timewidth = self . _timewidth + <NUM_LIT:1> <EOL> edit = ValidatedEdit ( <EOL> dateformat = self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> EditWidget = TimeWidget , <EOL> validate = self . _validate_start_time , <EOL> edit_text = self . startdt . strftime ( self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <EOL> ) <EOL> edit = urwid . Padding ( <EOL> edit , align = '<STR_LIT:left>' , width = self . _timewidth + <NUM_LIT:1> , left = <NUM_LIT:1> ) <EOL> self . widgets . starttime = edit <EOL> edit = ValidatedEdit ( <EOL> dateformat = self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> EditWidget = TimeWidget , <EOL> validate = self . _validate_end_time , <EOL> edit_text = self . enddt . strftime ( self . conf [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <EOL> ) <EOL> edit = urwid . Padding ( <EOL> edit , align = '<STR_LIT:left>' , width = self . _timewidth + <NUM_LIT:1> , left = <NUM_LIT:1> ) <EOL> self . widgets . endtime = edit <EOL> columns = NPile ( [ <EOL> self . checkallday , <EOL> NColumns ( [ ( datewidth , self . widgets . startdate ) , ( <EOL> timewidth , self . widgets . starttime ) ] , dividechars = <NUM_LIT:1> ) , <EOL> NColumns ( <EOL> [ ( datewidth , self . widgets . enddate ) , <EOL> ( timewidth , self . widgets . endtime ) ] , <EOL> dividechars = <NUM_LIT:1> ) <EOL> ] , focus_item = <NUM_LIT:1> ) <EOL> urwid . WidgetWrap . __init__ ( self , columns ) <EOL> @ property <EOL> def changed ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . startdt != self . _original_start ) or ( self . enddt != self . _original_end ) <EOL> def validate ( self ) : <EOL> return self . startdt <= self . enddt </s>
