<commit_before># Copyright (c) 2013-2016 <NAME> et al.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from datetime import datetime

import urwid

from .widgets import DateWidget, TimeWidget
from .calendarwidget import CalendarWidget


class DateConversionError(Exception):
    pass


class StartEnd(object):
    # TODO convert to namespace

    def __init__(self, startdate, starttime, enddate, endtime):
        """collecting some common properties"""
        self.startdate = startdate
        self.starttime = starttime
        self.enddate = enddate
        self.endtime = endtime


class CalendarPopUp(urwid.PopUpLauncher):
    def __init__(self, widget, conf, on_date_change):
        self._conf = conf
        self._on_date_change = on_date_change
        self.__super.__init__(widget)

    def keypress(self, size, key):
        if key == 'enter':
            self.open_pop_up()
        else:
            return super().keypress(size, key)

    def create_pop_up(self):
        def on_change(new_date):
            self._original_widget.set_value(new_date)
            self._on_date_change(new_date)

        keybindings = {}  # TODO use same keybindings as before
        on_press = {'enter': lambda _, __: self.close_pop_up(),
                    'esc': lambda _, __: self.close_pop_up()}
        pop_up = CalendarWidget(
            on_change, keybindings, on_press,
            firstweekday=self._conf['locale']['firstweekday'],
            initial=self._original_widget._get_current_value())
        pop_up = urwid.LineBox(pop_up)
        return pop_up

    def get_pop_up_parameters(self):
        return {'left': 0, 'top': 1, 'overlay_width': 28, 'overlay_height': 8}


class StartEndEditor(urwid.WidgetWrap):
    """
    Wigdet for editing start and end times (of an event)

    we cannot change timezones ATM  # TODO
    pop up on strings not matching timeformat # TODO
    """

    def __init__(self, start, end, conf, on_date_change=lambda x: None):
        self.allday = not isinstance(start, datetime)
        self.conf = conf
        self.startdt = start
        self.enddt = end
        self.on_date_change = on_date_change
        self.dts = StartEnd(
            startdate=start.strftime(self.conf['locale']['longdateformat']),
            starttime=start.strftime(self.conf['locale']['timeformat']),
            enddate=end.strftime(self.conf['locale']['longdateformat']),
            endtime=end.strftime(self.conf['locale']['timeformat']))
        # this will contain the widgets for [start|end] [date|time]
        self.widgets = StartEnd(None, None, None, None)
        # and these are their background colors
        self.bgs = StartEnd('edit', 'edit', 'edit', 'edit')

        self.checkallday = urwid.CheckBox('Allday', state=self.allday,
                                          on_state_change=self.toggle)
        self.toggle(None, self.allday)
<commit_msg>pop up calendar: keybindings and weeknumbers<commit_after># Copyright (c) 2013-2016 <NAME> et al.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from datetime import datetime

import urwid

from .widgets import DateWidget, TimeWidget
from .calendarwidget import CalendarWidget


class DateConversionError(Exception):
    pass


class StartEnd(object):
    # TODO convert to namespace

    def __init__(self, startdate, starttime, enddate, endtime):
        """collecting some common properties"""
        self.startdate = startdate
        self.starttime = starttime
        self.enddate = enddate
        self.endtime = endtime


class CalendarPopUp(urwid.PopUpLauncher):
    def __init__(self, widget, conf, on_date_change):
        self._conf = conf
        self._on_date_change = on_date_change
        self.__super.__init__(widget)

    def keypress(self, size, key):
        if key == 'enter':
            self.open_pop_up()
        else:
            return super().keypress(size, key)

    def create_pop_up(self):
        def on_change(new_date):
            self._original_widget.set_value(new_date)
            self._on_date_change(new_date)

        keybindings = self._conf['keybindings']
        on_press = {'enter': lambda _, __: self.close_pop_up(),
                    'esc': lambda _, __: self.close_pop_up()}
        pop_up = CalendarWidget(
            on_change, keybindings, on_press,
            firstweekday=self._conf['locale']['firstweekday'],
            weeknumbers=self._conf['locale']['weeknumbers'],
            initial=self._original_widget._get_current_value())
        pop_up = urwid.LineBox(pop_up)
        return pop_up

    def get_pop_up_parameters(self):
        width = 31 if self._conf['locale']['weeknumbers'] == 'right' else 28
        return {'left': 0, 'top': 1, 'overlay_width': width, 'overlay_height': 8}


class StartEndEditor(urwid.WidgetWrap):
    """
    Wigdet for editing start and end times (of an event)

    we cannot change timezones ATM  # TODO
    pop up on strings not matching timeformat # TODO
    """

    def __init__(self, start, end, conf, on_date_change=lambda x: None):
        self.allday = not isinstance(start, datetime)
        self.conf = conf
        self.startdt = start
        self.enddt = end
        self.on_date_change = on_date_change
        self.dts = StartEnd(
            startdate=start.strftime(self.conf['locale']['longdateformat']),
            starttime=start.strftime(self.conf['locale']['timeformat']),
            enddate=end.strftime(self.conf['locale']['longdateformat']),
            endtime=end.strftime(self.conf['locale']['timeformat']))
        # this will contain the widgets for [start|end] [date|time]
        self.widgets = StartEnd(None, None, None, None)
        # and these are their background colors
        self.bgs = StartEnd('edit', 'edit', 'edit', 'edit')

        self.checkallday = urwid.CheckBox('Allday', state=self.allday,
                                          on_state_change=self.toggle)
        self.toggle(None, self.allday)

{'fraction_non_alphanumeric': 0.09307576517812344, 'fraction_numerical': 0.0035122930255895636, 'mean_word_length': 3.082437275985663, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4593574', 'n_tokens_mistral': 2284, 'n_tokens_neox': 2177, 'n_words': 728}