<commit_before>from telegram.ext import Updater, CommandHandler
import logging
import pickledb

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')


class DefinitionManager(object):

    def __init__(self, dispatcher):
        self.db = pickledb.load("defines.db", True)

        dispatcher.add_handler(CommandHandler('def', self.def_show))
        dispatcher.add_handler(CommandHandler('def_show', self.def_show))
        dispatcher.add_handler(CommandHandler('def_add', self.def_add))
        dispatcher.add_handler(CommandHandler('def_rm', self.def_rm))

    def def_show(self, bot, update):
        def_name = update.message.text.partition(" ")[2].strip()
        if self.db.get(def_name) is None:
            bot.sendMessage(update.message.chat_id,
                            text='No definition available for %s' %
                            (def_name))
            return
        def_str = "Definition for _%s_:\n" % (def_name)
        i = 1
        def_text = self.db.lgetall(def_name)
        for s in def_text:
            def_str += "*%d.* %s\n" % (i, s)
            i += 1
        bot.sendMessage(update.message.chat_id,
                        text=def_str,
                        parse_mode="Markdown")

    def def_add(self, bot, update):
        command = update.message.text.partition(" ")[2]
        (def_name, def_part, def_add) = command.partition(" ")
        def_name = def_name.strip()
        if self.db.get(def_name) is None:
            bot.sendMessage(update.message.chat_id,
                            text='Adding definition for _%s_.' %
                            (def_name),
                            parse_mode="Markdown")
            self.db.lcreate(def_name)
        else:
            bot.sendMessage(update.message.chat_id,
                            text='Definition for _%s_ already exists, extending.' %
                            (def_name),
                            parse_mode="Markdown")
        self.db.ladd(def_name, def_add.strip())

    def def_rm(self, bot, update):
        command = update.message.text.partition(" ")[2]
        (def_name, def_part, def_rm) = command.partition(" ")
        def_name = def_name.strip()
        if self.db.get(def_name) is None:
            bot.sendMessage(update.message.chat_id,
                            text='No definition available for _%s_' %
                            (def_name, def_name),
                            parse_mode="Markdown")
            return
        try:
            def_id = int(def_rm) - 1
            self.db.lpop(def_name, def_id)
        except:
            bot.sendMessage(update.message.chat_id,
                            text='Index %s is not valid for definition _%s_.' %
                            (def_rm, def_name),
                            parse_mode="Markdown")
            return
        if self.db.llen(def_name) is 0:
            self.db.lrem(def_name)
        bot.sendMessage(update.message.chat_id,
                        text='Index %s for definition _%s_ deleted.' %<commit_msg>More kludging of the definition module, mostly removing markdown markup<commit_after>from telegram.ext import Updater, CommandHandler
import logging
import pickledb

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')


class DefinitionManager(object):

    def __init__(self, dispatcher):
        self.db = pickledb.load("defines.db", True)

        dispatcher.add_handler(CommandHandler('def', self.def_show))
        dispatcher.add_handler(CommandHandler('def_show', self.def_show))
        dispatcher.add_handler(CommandHandler('def_add', self.def_add))
        dispatcher.add_handler(CommandHandler('def_rm', self.def_rm))

    def _contains_special_chars(self, defstr):
        """Returns True if string contains *[]()_`"""
        # Should probably do this as a regexp but eh.
        if any(c in defstr for c in ['(', ')', '*', '[', ']', '_', '`']):
            return True
        return False

    def send_char_error(self, bot, update):
        bot.sendMessage(update.message.chat_id,
                        text='The following characters are not allowed in definition queries: []()*_`')

    def def_show(self, bot, update):
        def_name = update.message.text.partition(" ")[2].strip()
        if self._contains_special_chars(def_name):
            self.send_char_error(bot, update)
            return
        if self.db.get(def_name) is None:
            bot.sendMessage(update.message.chat_id,
                            text='No definition available for %s' %
                            (def_name),
                            parse_mode="Markdown")
            return
        def_str = "Definition for _%s_:\n" % (def_name)
        i = 1
        def_text = self.db.lgetall(def_name)
        for d in def_text:
            def_str += "*%d.* %s\n" % (i, d["desc"])
            i += 1
        bot.sendMessage(update.message.chat_id,
                        text=def_str,
                        parse_mode="Markdown")

    def def_add(self, bot, update):
        command = update.message.text.partition(" ")[2]
        (def_name, def_part, def_add) = command.partition(" ")
        def_name = def_name.strip()
        def_add = def_add.strip()
        if (self._contains_special_chars(def_name) or self._contains_special_chars(def_add)):
            self.send_char_error(bot, update)
            return
        if self.db.get(def_name) is None:
            bot.sendMessage(update.message.chat_id,
                            text='Adding definition:\n_%s_\n for term _%s_.' %
                            (def_add,def_name),
                            parse_mode="Markdown")
            self.db.lcreate(def_name)
        else:
            bot.sendMessage(update.message.chat_id,
                            text='Definition for _%s_ already exists, extending with definition _%s_.' %
                            (def_name, def_add),
                            parse_mode="Markdown")
        d = {"user": update.message.from_user.id,
             "desc": def_add.strip()}
        self.db.ladd(def_name, d)

    def def_rm(self, bot, update):
        command = update.message.text.partition(" ")[2]
        (def_name, def_part, def_rm) = command.partition(" ")
        def_name = def_name.strip()
        if self._contains_special_chars(def_name):
            self.send_char_error(bot, update)
            return
        if self.db.get(def_name) is None:
            bot.sendMessage(update.message.chat_id,
                            text='No definition available for _%s_' %
                            (def_name),
                            parse_mode="Markdown")
            return
        try:
            def_id = int(def_rm) - 1
            self.db.lpop(def_name, def_id)
        except:
            bot.sendMessage(update.message.chat_id,
                            text='Index %s is not valid for definition _%s_.' %
                            (def_rm, def_name),
                            parse_mode="Markdown")
            return
        if self.db.llen(def_name) is 0:
            self.db.lrem(def_name)
        bot.sendMessage(update.message.chat_id,
                        text='Index %s for definition _%s_ deleted.' %
{'fraction_non_alphanumeric': 0.11282401091405184, 'fraction_numerical': 0.0019099590723055935, 'mean_word_length': 1.750844277673546, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7415400', 'n_tokens_mistral': 2153, 'n_tokens_neox': 2116, 'n_words': 416}