def _construct_week(self, week): """ constructs a CColumns week from a week of datetime.date objects. Also prepends the month name if the first day of the month is included in that week. :param week: list of datetime.date objects :returns: the week as an CColumns object and True or False depending on if today is in this week :rtype: tuple(urwid.CColumns, bool) """ if self.monthdisplay == 'firstday' and 1 in [day.day for day in week]: month_name = calendar.month_abbr[week[-1].month].ljust(4) attr = 'monthname' elif self.monthdisplay == 'firstfullweek' and week[0].day <= 7: month_name = calendar.month_abbr[week[-1].month].ljust(4) attr = 'monthname' elif self.weeknumbers == 'left': month_name = ' {:2} '.format(getweeknumber(week[0])) attr = 'weeknumber_left' else: month_name = ' ' attr = None this_week = [(get_month_abbr_len(), urwid.AttrMap(urwid.Text(month_name), attr))] for number, day in enumerate(week): new_date = Date(day, self.get_styles) this_week.append((2, new_date)) new_date.set_styles(self.get_styles(new_date.date, False)) if self.weeknumbers == 'right': this_week.append( (2, urwid.AttrMap( urwid.Text('{:2}'.format(getweeknumber(week[0]))), 'weeknumber_right'))) week = DateCColumns(this_week, on_date_change=self.on_date_change, on_press=self.on_press, keybindings=self.keybindings, dividechars=1, get_styles=self.get_styles) return week
def _construct_week(self, week): """ constructs a CColumns week from a week of datetime.date objects. Also prepends the month name if the first day of the month is included in that week. :param week: list of datetime.date objects :returns: the week as an CColumns object and True or False depending on if today is in this week :rtype: tuple(urwid.CColumns, bool) """ if self.monthdisplay == 'firstday' and 1 in [day.day for day in week]: month_name = calendar.month_abbr[week[-1].month].ljust(4) attr = 'monthname' elif self.monthdisplay == 'firstfullweek' and week[0].day <= 7: month_name = calendar.month_abbr[week[-1].month].ljust(4) attr = 'monthname' elif self.weeknumbers == 'left': month_name = ' {:2} '.format(getweeknumber(week[0])) attr = 'weeknumber_left' else: month_name = ' ' attr = None this_week = [(get_month_abbr_len(), urwid.AttrMap(urwid.Text(month_name), attr))] for number, day in enumerate(week): new_date = Date(day, self.get_styles) this_week.append((2, new_date)) new_date.set_styles(self.get_styles(new_date.date, False)) if self.weeknumbers == 'right': this_week.append((2, urwid.AttrMap( urwid.Text('{:2}'.format(getweeknumber(week[0]))), 'weeknumber_right'))) week = DateCColumns(this_week, on_date_change=self.on_date_change, on_press=self.on_press, keybindings=self.keybindings, dividechars=1, get_styles=self.get_styles) return week
def __init__(self, on_date_change, keybindings, on_press, firstweekday=0, weeknumbers=False, monthdisplay='firstday', get_styles=None, initial=None): """ :param on_date_change: a function that is called every time the selected date is changed with the newly selected date as a first (and only argument) :type on_date_change: function :param keybindings: bind keys to specific functionionality, keys are the available commands, values are lists of keys that should be bound to those commands. See below for the defaults. Available commands: 'left', 'right', 'up', 'down': move cursor in direction 'today': refocus on today 'mark': toggles selection mode :type keybindings: dict :param on_press: dictonary of functions that are called when the key is pressed and is not already bound to one of the internal functionions via `keybindings`. These functions must accept two arguments, in normal mode the first argument is the currently selected date (datetime.date) and the second is `None`. When a date range is selected, the first argument is the earlier, the second argument is the later date. The function's return values are interpreted as pressed keys, which are handed to the widget containing the CalendarWidget. :type on_press: dict """ if initial is None: self._initial = dt.date.today() else: self._initial = initial default_keybindings = { 'left': ['left'], 'down': ['down'], 'right': ['right'], 'up': ['up'], 'today': ['t'], 'view': [], 'mark': ['v'], } on_press = defaultdict(lambda: lambda x: x, on_press) default_keybindings.update(keybindings) calendar.setfirstweekday(firstweekday) try: mylocale = '.'.join(getlocale(LC_TIME)) except TypeError: # language code and encoding may be None mylocale = 'C' _calendar = calendar.LocaleTextCalendar(firstweekday, mylocale) weekheader = _calendar.formatweekheader(2) dnames = weekheader.split(' ') def _get_styles(date, focus): if focus: if date == dt.date.today(): return 'today focus' else: return 'reveal focus' else: if date == dt.date.today(): return 'today' else: return None if get_styles is None: get_styles = _get_styles if weeknumbers == 'right': dnames.append('#w') month_names_length = get_month_abbr_len() dnames = urwid.Columns( [(month_names_length, urwid.Text(' ' * month_names_length))] + [(2, urwid.AttrMap(urwid.Text(name), 'dayname')) for name in dnames], dividechars=1) self.walker = CalendarWalker( on_date_change, on_press, default_keybindings, firstweekday, weeknumbers, monthdisplay, get_styles, initial=self._initial) self.box = CListBox(self.walker) frame = urwid.Frame(self.box, header=dnames) urwid.WidgetWrap.__init__(self, frame) self.set_focus_date(self._initial)