<commit_before>import asyncio
import io
import os
import os.path
import re
import shlex
import time
import urllib.parse
from datetime import datetime

import aiohttp
import discord.game
import psutil
import pytz
from lxml import html
from pytz import timezone

import discordant.utils as utils
from .discordant import Discordant


#region general
@Discordant.register_command("help")
async def _help(self, args, message):
    section = args
    default = "general"
    if not section:
        section = default
    cmds = [cmd.aliases[0] for cmd in self._commands.values()
            if cmd.section == section]
    output = "Commands: " + ", ".join(cmds)
    if not args:
        sections = list({cmd.section + (" *(default)*"
                        if cmd.section == default else "")
                        for cmd in self._commands.values()})
        output += "\n\nType !help [section] to display more commands.\n" + \
                  "Sections: " + ", ".join(sections) + "\n\n" + \
                  "Command help syntax:\n" + \
                  "[] - optional argument\n" + \
                  "<> - required argument\n" + \
                  "* - any number of arguments\n" + \
                  "key=value - kwargs style argument " + \
                  "(each key-value pair is separated by space, " + \
                  "and the key and value are separated by the \"=\" character)."
    await self.send_message(message.channel, output.strip())


@Discordant.register_command("timezone")
async def _convert_timezone(self, args, message):
    def get_timezone_by_code(code):
        code = code.upper()
        for tz_str in pytz.all_timezones:
            tz = timezone(tz_str)
            if tz.tzname(datetime.now()) == code:
                return tz
        raise ValueError(code + ": not a valid time zone code")

    def read_time(dt_str):
        formats = ["%I%p", "%I:%M%p", "%H", "%H:%M"]
        for f in formats:
            try:
                read_dt = datetime.strptime(dt_str, f)
                return datetime.now().replace(hour=read_dt.hour,
                                              minute=read_dt.minute)
            except ValueError:
                pass
        raise ValueError(dt_str + ": not a valid time format")

    def is_time(dt_str):
        try:
            read_time(dt_str)
        except ValueError:
            return False
        return True

    def relative_date_str(dt_1, dt_2):
        delta = (dt_2 - dt_1).days
        if delta == 0:
            return "same day"
        else:
            return "{} day{} {}".format(abs(delta),
                                        "s" if abs(delta) != 1 else "",
                                        "ahead" if delta > 0 else "behind")

    def dt_format(dt, tz_str, relative):
        new_dt = dt.astimezone(get_timezone_by_code(tz_str))
        return new_dt.strftime("%I:%M %p %Z") + (
            ", " + relative_date_str(dt, new_dt) if relative else "")

    if not args:
        await self.send_message(
            message.channel,
            "!timezone <time> <from> <*to>\n!timezone <*timezone>")
        return
    split = args.split()
    try:
        is_t = is_time(split[0])
        if is_t:
            dt = get_timezone_by_code(split[1]).localize(read_time(split[0]))
            tz_strs = split[2:]
            output = "{} is{}".format(
                dt.strftime("%I:%M %p %Z"),
                ":\n" if len(tz_strs) > 1 else " ")
        else:
            dt = pytz.utc.localize(datetime.utcnow())
            tz_strs = split<commit_msg>!tz fix<commit_after>import asyncio
import io
import os
import os.path
import re
import shlex
import time
import urllib.parse
from datetime import datetime

import aiohttp
import discord.game
import psutil
import pytz
from lxml import html
from pytz import timezone

import discordant.utils as utils
from .discordant import Discordant


#region general
@Discordant.register_command("help")
async def _help(self, args, message):
    section = args
    default = "general"
    if not section:
        section = default
    cmds = [cmd.aliases[0] for cmd in self._commands.values()
            if cmd.section == section]
    output = "Commands: " + ", ".join(cmds)
    if not args:
        sections = list({cmd.section + (" *(default)*"
                        if cmd.section == default else "")
                        for cmd in self._commands.values()})
        output += "\n\nType !help [section] to display more commands.\n" + \
                  "Sections: " + ", ".join(sections) + "\n\n" + \
                  "Command help syntax:\n" + \
                  "[] - optional argument\n" + \
                  "<> - required argument\n" + \
                  "* - any number of arguments\n" + \
                  "key=value - kwargs style argument " + \
                  "(each key-value pair is separated by space, " + \
                  "and the key and value are separated by the \"=\" character)."
    await self.send_message(message.channel, output.strip())


@Discordant.register_command("timezone")
async def _convert_timezone(self, args, message):
    def get_timezone_by_code(code):
        code = code.upper()
        for tz_str in pytz.all_timezones:
            tz = timezone(tz_str)
            if tz.tzname(datetime.now()) == code:
                return tz
        raise ValueError(code + ": not a valid time zone code")

    def read_time(dt_str):
        formats = ["%I%p", "%I:%M%p", "%H", "%H:%M"]
        for f in formats:
            try:
                read_dt = datetime.strptime(dt_str, f)
                return datetime.now().replace(hour=read_dt.hour,
                                              minute=read_dt.minute)
            except ValueError:
                pass
        raise ValueError(dt_str + ": not a valid time format")

    def is_time(dt_str):
        try:
            read_time(dt_str)
        except ValueError:
            return False
        return True

    def relative_date_str(dt_1, dt_2):
        delta = dt_2.day - dt_1.day
        if delta > 1:
            delta = -1
        elif delta < -1:
            delta = 1
        return "same day" if delta == 0 else "1 day " + (
            "ahead" if delta > 0 else "behind")

    def dt_format(dt, tz_str, relative):
        new_dt = dt.astimezone(get_timezone_by_code(tz_str))
        return new_dt.strftime("%I:%M %p %Z") + (
            ", " + relative_date_str(dt, new_dt) if relative else "")

    if not args:
        await self.send_message(
            message.channel,
            "!timezone <time> <from> <*to>\n!timezone <*timezone>")
        return
    split = args.split()
    try:
        is_t = is_time(split[0])
        if is_t:
            dt = get_timezone_by_code(split[1]).localize(read_time(split[0]))
            tz_strs = split[2:]
            output = "{} is{}".format(
                dt.strftime("%I:%M %p %Z"),
                ":\n" if len(tz_strs) > 1 else " ")
        else:
            dt = pytz.utc.localize(datetime.utcnow())
            tz_strs = split
{'fraction_non_alphanumeric': 0.11873051856049872, 'fraction_numerical': 0.004250495891187305, 'mean_word_length': 1.7791338582677165, 'pattern_counts': {'":': 6, '<': 14, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7423456', 'n_tokens_mistral': 2144, 'n_tokens_neox': 2082, 'n_words': 564}