Ejemplo n.º 1
0
def update_user(bot, trigger):
    """Set your preferred timezone.

    Most timezones will work, but it's best to use one from
    <https://sopel.chat/tz>.
    """
    argument = trigger.group(2)
    if not argument:
        bot.reply("What timezone do you want to set? Try one from "
                  "https://sopel.chat/tz")
        return

    try:
        zone = validate_timezone(argument)
    except ValueError:
        bot.say(
            'I don\'t know that timezone. Try one from https://sopel.chat/tz')
        return

    bot.db.set_nick_value(trigger.nick, 'timezone', zone)

    if len(zone) < 4:
        bot.say('Okay, %s, but you should use one from https://sopel.chat/tz '
                'if you use DST.' % trigger.nick)
    else:
        bot.reply('I now have you in the %s timezone.' % zone)
Ejemplo n.º 2
0
Archivo: remind.py Proyecto: r4f4/sopel
def parse_regex_match(match, default_timezone=None):
    """Parse a time reminder from ``match``

    :param match: :obj:`~.REGEX_AT`'s matching result
    :param default_timezone: timezone used when ``match`` doesn't have one;
                             defaults to ``UTC``
    :rtype: :class:`TimeReminder`
    """
    try:
        # Removing the `or` clause will BREAK the fallback to default_timezone!
        # We need some invalid value other than None to trigger the ValueError.
        # validate_timezone(None) excepting would be easier, but it doesn't.
        timezone = validate_timezone(match.group('tz') or '')
    except ValueError:
        timezone = default_timezone or 'UTC'

    return TimeReminder(
        int(match.group('hours')),
        int(match.group('minutes')),
        int(match.group('seconds') or '0'),
        timezone,
        match.group('date1'),
        match.group('date2'),
        match.group('date3'),
        match.group('message')
    )
def test_validate_timezone_invalid():
    with pytest.raises(ValueError):
        time.validate_timezone('Invalid/Timezone')

    with pytest.raises(ValueError):
        time.validate_timezone('Europe Paris')

    with pytest.raises(ValueError):
        time.validate_timezone('Paris/Europe')

    with pytest.raises(ValueError):
        time.validate_timezone('Paris,Europe')
Ejemplo n.º 4
0
def parse_regex_match(match, default_timezone=None):
    """Parse a time reminder from ``match``

    :param match: :obj:`~.REGEX_AT`'s matching result
    :param default_timezone: timezone used when ``match`` doesn't have one;
                             defaults to ``UTC``
    :rtype: :class:`TimeReminder`
    """
    try:
        timezone = validate_timezone(match.group('tz') or 'UTC')
    except ValueError:
        timezone = default_timezone or 'UTC'

    return TimeReminder(int(match.group('hours')), int(match.group('minutes')),
                        int(match.group('seconds') or '0'), timezone,
                        match.group('date1'), match.group('date2'),
                        match.group('date3'), match.group('message'))
Ejemplo n.º 5
0
def f_time_zone(bot, trigger):
    """Return the current time in a timezone.

    Unlike the ``time`` command, it requires an argument, and that argument
    must be a valid timezone.
    """
    argument = trigger.group(2)
    if not argument:
        bot.reply(ERROR_NO_TIMEZONE)
        return

    zone = None
    argument = argument.strip()
    try:
        zone = validate_timezone(argument)
    except ValueError:
        bot.reply(ERROR_INVALID_TIMEZONE % argument)
        return

    time = format_time(bot.db, bot.config, zone, trigger.nick, trigger.sender)
    bot.say(time)
Ejemplo n.º 6
0
def update_channel(bot, trigger):
    """Set the preferred timezone for the current channel."""
    argument = trigger.group(2)
    if not argument:
        bot.reply(ERROR_NO_TIMEZONE)
        return

    try:
        zone = validate_timezone(argument)
    except ValueError:
        bot.reply(ERROR_INVALID_TIMEZONE % argument)
        return

    channel = trigger.sender
    bot.db.set_channel_value(channel, 'timezone', zone)

    if len(zone) < 4:
        bot.reply('Okay, but you should use one from https://sopel.chat/tz '
                  'if you use DST.')
    else:
        bot.reply('I now have %s in the %s timezone.' % (channel, zone))
Ejemplo n.º 7
0
def f_time_zone(bot, trigger):
    """Return the current time in a timezone.

    Unlike the ``.t`` command, it requires an argument, and that argument
    must be a valid timezone.
    """
    argument = trigger.group(2)
    if not argument:
        bot.say('Please provide a timezone.')
        return

    zone = None
    argument = argument.strip()
    try:
        zone = validate_timezone(argument)
    except ValueError:
        bot.say('Cannot display time: "%s" is not a valid timezone.' %
                argument)
        return

    time = format_time(bot.db, bot.config, zone, trigger.nick, trigger.sender)
    bot.say(time)
Ejemplo n.º 8
0
def update_channel(bot, trigger):
    """Set the preferred timezone for the channel."""
    argument = trigger.group(2)
    if not argument:
        bot.reply("What timezone do you want to set? Try one from "
                  "https://sopel.chat/tz")
        return

    try:
        zone = validate_timezone(argument)
    except ValueError:
        bot.say(
            'I don\'t know that timezone. Try one from https://sopel.chat/tz')
        return

    channel = trigger.sender
    bot.db.set_channel_value(channel, 'timezone', zone)

    if len(zone) < 4:
        bot.say('Okay, %s, but you should use one from https://sopel.chat/tz '
                'if you use DST.' % trigger.nick)
    else:
        bot.reply('I now have %s in the %s timezone.' % (channel, zone))
Ejemplo n.º 9
0
def update_user(bot, trigger):
    """Set your preferred timezone.

    Most timezones will work, but it's best to use one from
    <https://sopel.chat/tz>.
    """
    argument = trigger.group(2)
    if not argument:
        bot.reply(ERROR_NO_TIMEZONE)
        return

    try:
        zone = validate_timezone(argument)
    except ValueError:
        bot.reply(ERROR_INVALID_TIMEZONE % argument)
        return

    bot.db.set_nick_value(trigger.nick, 'timezone', zone)

    if len(zone) < 4:
        bot.reply('Okay, but you should use one from https://sopel.chat/tz '
                  'if you use DST.')
    else:
        bot.reply('I now have you in the %s timezone.' % zone)
Ejemplo n.º 10
0
def f_time(bot, trigger):
    """Return the current time.

    The command takes an optional parameter: it will try to guess if it's a
    nick, a channel, or a timezone (in that order).

    If it's a known nick or channel but there is no configured timezone, then
    it will complain. If nothing can be found, it'll complain that the argument
    is not a valid timezone.

    .. seealso::

        Function :func:`~sopel.tools.time.format_time` is used to format
        the current datetime according to the timezone (if found).

    """
    argument = trigger.group(2)

    if not argument:
        # get default timezone from nick, or sender, or bot, or UTC
        zone = get_timezone(bot.db, bot.config, None, trigger.nick,
                            trigger.sender)
    else:
        # guess if the argument is a nick, a channel, or a timezone
        zone = None
        argument = argument.strip()
        channel_or_nick = tools.Identifier(argument)

        # first, try to get nick or channel's timezone
        help_prefix = bot.config.core.help_prefix
        if channel_or_nick.is_nick():
            zone = get_nick_timezone(bot.db, channel_or_nick)
            if zone is None and channel_or_nick in bot.users:
                # zone not found for a known nick: error case
                set_command = '%ssettz <zone>' % help_prefix
                if channel_or_nick != trigger.nick:
                    bot.say('Could not find a timezone for this nick. '
                            '%s can set a timezone with `%s`' %
                            (argument, set_command))
                else:
                    bot.say('Could not find a timezone for you. '
                            'You can set your timezone with `%s`' %
                            set_command)
                return
        else:
            zone = get_channel_timezone(bot.db, channel_or_nick)
            if zone is None and channel_or_nick in bot.channels:
                # zone not found for an existing channel: error case
                set_command = '%ssetctz <zone>' % help_prefix
                bot.say('Could not find timezone for channel %s. '
                        'It can be set with `.setctz <zone>`. '
                        '(requires OP privileges)' % argument)
                return

        # then, fallback on timezone detection
        if zone is None:
            # argument not found as nick or channel timezone
            try:
                zone = validate_timezone(argument)
            except ValueError:
                bot.say('Could not find timezone "%s".' % argument)
                return

    time = format_time(bot.db, bot.config, zone, trigger.nick, trigger.sender)
    bot.say(time)
def test_validate_timezone_none():
    assert time.validate_timezone(None) is None
def test_validate_timezone():
    assert time.validate_timezone('Europe/Paris') == 'Europe/Paris'
    assert time.validate_timezone('America/New York') == 'America/New_York'
    assert time.validate_timezone('Paris, Europe') == 'Europe/Paris'
    assert time.validate_timezone('New York, America') == 'America/New_York'
    assert time.validate_timezone('Israel') == 'Israel'