<commit_before># coding: utf-8

# Copyright 2015 <NAME>
# NO WARRANTY. See LICENSE for details.

import urllib

from hamper import interfaces
import pokedex.db
import pokedex.db.tables as t
import pokedex.lookup

__all__ = ('Plugin',)

class Plugin(interfaces.ChatCommandPlugin):
    name = 'pokedex'

    base_url = "http://veekun.com/dex"

    valid_types = [
        u'pokemon_species',
        u'pokemon_form',
        u'ability',
        u'item',
        u'move',
        u'nature',
        u'type',
    ]

    def setup(self, loader):
        interfaces.ChatCommandPlugin.setup(self, loader)

        self.config = loader.config.get("pokedex", {})
        uri = self.config.get('db')
        self.session = pokedex.db.connect(uri)

        lookup_dir = self.config.get('lookup')
        self.lookup = pokedex.lookup.PokedexLookup(
            directory=lookup_dir,
            session=self.session,
        )

    def do_lookup(self, bot, comm, query):
        if type(query) is str:
            query = query.decode('utf-8', 'replace')
        try:
            results = self.lookup.lookup(query, valid_types=self.valid_types)
        except pokedex.lookup.UninitializedIndex.UninitializedIndexError:
            return bot.reply(comm, u"error: the lookup index does not exist. type !reindex to create it.")

        if len(results) == 0:
            return bot.reply(comm, u"I don't know what that is")

        if len(results) > 1:
            suggestions = []
            for r in results:
                suggestions.append(r.name)
            return bot.reply(comm, u"did you mean {0}", vars=[", ".join(suggestions)])

        thing = results[0].object
        bot.reply(comm, u"{0}", vars=[self.format_thing(thing)])

    def format_thing(self, thing):
        if type(thing) is t.PokemonForm:
            return self.format_pokemon(thing.species, thing)
        if type(thing) is t.PokemonSpecies:
            return self.format_pokemon(thing, thing.default_form)

        if type(thing) is t.Ability:
            return self.format_ability(thing)
        if type(thing) is t.Item:
            return self.format_item(thing)
        if type(thing) is t.Move:
            return self.format_move(thing)
        if type(thing) is t.Nature:
            return self.format_nature(thing)
        if type(thing) is t.Type:
            return self.format_type(thing)

        return thing.name

    def format_pokemon(self, species, form):
        types = u"/".join(type.name for type in form.pokemon.types)
        stats = [
            form.pokemon.base_stat(u'hp'),
            form.pokemon.base_stat(u'attack'),
            form.pokemon.base_stat(u'defense'),
            form.pokemon.base_stat(u'special-attack'),
            form.pokemon.base_stat(u'special-defense'),
            form.pokemon.base_stat(u'speed'),
        ]
        stat_text = u"{0} HP, {1}/{2} physical, {3}/{4} special, {5} speed".format(*stats)
        url = urljoin(self.base_url, "pokemon", species.name.lower())
        if not form.pokemon.is_default:
            url += "?form=" + urllib.quote(form.form_identifier)
        return u"#{0.id} {1.pokemon.name}, the {0.genus} Pokémon. {types}-type. {stats}; {total} total. {url}".format(
            species, form, types=types, stats=stat_text, total=sum(stats), url=url)

    def format_ability(self, ability):
        url = urljoin(self.base_url, "abilities", ability.name.lower())
        return u"{0.name}, an ability. {0.short_effect} {url}".format(ability, url=url)

    def format_item(self, item):
        url = urljoin(self.base_url, "items", item.pocket.identifier, item.name.lower())
        return u"{0.name}, an item. {0.short_effect} {url}".format(item, url=url)

    def format_move(self, move):
        if move.power is not None:
            power = "{0.power}".format(move)
        else:
            power = "variable"

        if move.accuracy is not None:
            accuracy = "{0.accuracy}%".format(move)
        else:
            accuracy = "perfect"

        if move.damage_class.identifier == 'status':
            stats = "{0} accuracy".format(accuracy)
        else:
            stats = "{0} power; {1} accuracy".format(power, accuracy)

        url = urljoin(self.base_url, "moves", move.name.lower())
        return u"{0.name}, a {0.type.name}-type move. {stats}; {0.pp} PP. {0.short_effect} {url}".format(
            move, stats=stats.capitalize(), url=url)

    def format_nature(self, nature):
        if nature.is_neutral:
            template = u"{0.name}, a neutral nature. {url}"
        else:
            template = u"{0.name}, a nature. Raises {0.increased_stat.name}; lowers {0.decreased_stat.name}. {url}"
        url = urljoin(self.base_url, "natures", nature.name.lower())
        return template.format(nature, url=url)

    def format_type(self, type):
        url = urljoin(self.base_url, "types", type.name.lower())
        return u"{0.name}, a type. {url}".format(type, url=url)


    class Dex(interfaces.Command):
        name = 'pokedex'
        regex = '(?:dex|pokedex)(?: (.*)|$)'

        def command(self, bot, comm, groups):
            if not groups or not groups[0]:
                return bot.reply(comm, u"Please specify a pokemon to look up")

            query = groups[0].strip()
            self.plugin.do_lookup(bot, comm, query)

    class Reindex(interfaces.Command):
        name = 'reindex'<commit_msg>Add short help for pokedex command.<commit_after># coding: utf-8

# Copyright 2015 <NAME>
# NO WARRANTY. See LICENSE for details.

import urllib

from hamper import interfaces
import pokedex.db
import pokedex.db.tables as t
import pokedex.lookup

__all__ = ('Plugin',)

class Plugin(interfaces.ChatCommandPlugin):
    name = 'pokedex'

    base_url = "http://veekun.com/dex"

    valid_types = [
        u'pokemon_species',
        u'pokemon_form',
        u'ability',
        u'item',
        u'move',
        u'nature',
        u'type',
    ]

    def setup(self, loader):
        interfaces.ChatCommandPlugin.setup(self, loader)

        self.config = loader.config.get("pokedex", {})
        uri = self.config.get('db')
        self.session = pokedex.db.connect(uri)

        lookup_dir = self.config.get('lookup')
        self.lookup = pokedex.lookup.PokedexLookup(
            directory=lookup_dir,
            session=self.session,
        )

    def do_lookup(self, bot, comm, query):
        if type(query) is str:
            query = query.decode('utf-8', 'replace')
        try:
            results = self.lookup.lookup(query, valid_types=self.valid_types)
        except pokedex.lookup.UninitializedIndex.UninitializedIndexError:
            return bot.reply(comm, u"error: the lookup index does not exist. type !reindex to create it.")

        if len(results) == 0:
            return bot.reply(comm, u"I don't know what that is")

        if len(results) > 1:
            suggestions = []
            for r in results:
                suggestions.append(r.name)
            return bot.reply(comm, u"did you mean {0}", vars=[", ".join(suggestions)])

        thing = results[0].object
        bot.reply(comm, u"{0}", vars=[self.format_thing(thing)])

    def format_thing(self, thing):
        if type(thing) is t.PokemonForm:
            return self.format_pokemon(thing.species, thing)
        if type(thing) is t.PokemonSpecies:
            return self.format_pokemon(thing, thing.default_form)

        if type(thing) is t.Ability:
            return self.format_ability(thing)
        if type(thing) is t.Item:
            return self.format_item(thing)
        if type(thing) is t.Move:
            return self.format_move(thing)
        if type(thing) is t.Nature:
            return self.format_nature(thing)
        if type(thing) is t.Type:
            return self.format_type(thing)

        return thing.name

    def format_pokemon(self, species, form):
        types = u"/".join(type.name for type in form.pokemon.types)
        stats = [
            form.pokemon.base_stat(u'hp'),
            form.pokemon.base_stat(u'attack'),
            form.pokemon.base_stat(u'defense'),
            form.pokemon.base_stat(u'special-attack'),
            form.pokemon.base_stat(u'special-defense'),
            form.pokemon.base_stat(u'speed'),
        ]
        stat_text = u"{0} HP, {1}/{2} physical, {3}/{4} special, {5} speed".format(*stats)
        url = urljoin(self.base_url, "pokemon", species.name.lower())
        if not form.pokemon.is_default:
            url += "?form=" + urllib.quote(form.form_identifier)
        return u"#{0.id} {1.pokemon.name}, the {0.genus} Pokémon. {types}-type. {stats}; {total} total. {url}".format(
            species, form, types=types, stats=stat_text, total=sum(stats), url=url)

    def format_ability(self, ability):
        url = urljoin(self.base_url, "abilities", ability.name.lower())
        return u"{0.name}, an ability. {0.short_effect} {url}".format(ability, url=url)

    def format_item(self, item):
        url = urljoin(self.base_url, "items", item.pocket.identifier, item.name.lower())
        return u"{0.name}, an item. {0.short_effect} {url}".format(item, url=url)

    def format_move(self, move):
        if move.power is not None:
            power = "{0.power}".format(move)
        else:
            power = "variable"

        if move.accuracy is not None:
            accuracy = "{0.accuracy}%".format(move)
        else:
            accuracy = "perfect"

        if move.damage_class.identifier == 'status':
            stats = "{0} accuracy".format(accuracy)
        else:
            stats = "{0} power; {1} accuracy".format(power, accuracy)

        url = urljoin(self.base_url, "moves", move.name.lower())
        return u"{0.name}, a {0.type.name}-type move. {stats}; {0.pp} PP. {0.short_effect} {url}".format(
            move, stats=stats.capitalize(), url=url)

    def format_nature(self, nature):
        if nature.is_neutral:
            template = u"{0.name}, a neutral nature. {url}"
        else:
            template = u"{0.name}, a nature. Raises {0.increased_stat.name}; lowers {0.decreased_stat.name}. {url}"
        url = urljoin(self.base_url, "natures", nature.name.lower())
        return template.format(nature, url=url)

    def format_type(self, type):
        url = urljoin(self.base_url, "types", type.name.lower())
        return u"{0.name}, a type. {url}".format(type, url=url)


    class Dex(interfaces.Command):
        name = 'pokedex'
        regex = '(?:dex|pokedex)(?: (.*)|$)'
        short_desc = u'pokedex or dex [name] - looks up info about a pokemon, move, or item'

        def command(self, bot, comm, groups):
            if not groups or not groups[0]:
                return bot.reply(comm, u"Please specify a pokemon to look up")

            query = groups[0].strip()
            self.plugin.do_lookup(bot, comm, query)

    class Reindex(interfaces.Command):
        name = 'reindex'
{'fraction_non_alphanumeric': 0.1375491900796193, 'fraction_numerical': 0.007321314175894573, 'mean_word_length': 2.3418960244648317, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1630238', 'n_tokens_mistral': 3567, 'n_tokens_neox': 3465, 'n_words': 786}