How to use the karmabot.core.register.facet_registry.register function in karmabot

To help you get started, we’ve selected a few karmabot examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github chromakode / karmabot / karmabot / extensions / lmgtfy.py View on Github external
else:
                    return unichr(int(text[2:-1]))
            except ValueError:
                pass
        else:
            # named entity
            try:
                text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
            except KeyError:
                pass
        # leave as is
        return text
    return re.sub("&#?\w+;", fixup, text)


@facet_registry.register
class LmgtfyFacet(ThingFacet):
    name = "lmgtfy"
    commands = thing.add_child(CommandSet(name))

    @classmethod
    def does_attach(cls, thing):
        return thing.name == "lmgtfy"

    @commands.add(u"lmgtfy {item}",
                  u"googles for a {item}")
    def lmgtfy(self, context, item):
        api_url = "http://ajax.googleapis.com/ajax/services/search/web?"
        response = urlopen(api_url + urlencode(dict(v="1.0",
                                                    q=item)))
        response = dict(JSONDecoder().decode(response.read()))
        top_result = {}
github chromakode / karmabot / karmabot / core / facets / help.py View on Github external
#
# This file is part of 'karmabot' and is distributed under the BSD license.
# See LICENSE for more details.
from karmabot.core.client import thing
from karmabot.core.register import facet_registry
from karmabot.core.thing import ThingFacet
from karmabot.core.commands.sets import CommandSet
from itertools import chain


def numbered(strs):
    return (u"{0}. {1}".format(num + 1, line)
            for num, line in enumerate(strs))


@facet_registry.register
class HelpFacet(ThingFacet):
    name = "help"
    commands = thing.add_child(CommandSet(name))
    short_template = u"\"{0}\""
    full_template = short_template + u": {1}"

    @classmethod
    def does_attach(cls, thing):
        return True

    def get_topics(self, this_thing):
        topics = dict()
        for cmd in chain(thing, this_thing.iter_commands()):
            if cmd.visible:
                topic = cmd.format.replace("{thing}", thing.name)
                help = cmd.help.replace("{thing}", thing.name)
github chromakode / karmabot / karmabot / extensions / cs_schedule.py View on Github external
from urllib2 import urlopen

from BeautifulSoup import BeautifulSoup

from karmabot.core.client import thing
from karmabot.core.facets import Facet
from karmabot.core.register import facet_registry
from karmabot.core.commands.sets import CommandSet

#TODO:
#      Get rid of course command syntax? maybe?
#      Add error handing to webpage fetching.
#      Search with different keys.


@facet_registry.register
class ScheduleFacet(Facet):
    """
    Class which implements the ThingFacet interface and provides the new
    karmabot course info reporting functionality.
    """
    name = "course"
    commands = thing.add_child(CommandSet(name))
    URL = "http://cs.pdx.edu/schedule/termschedule?"

    @commands.add(u"course {CSXXX} {TERM} {YEAR}",
                  u"Get course information from CS website.")
    def course(self, context, CSXXX, TERM, YEAR):
        """
        High level command handler for the course command. Manages
        course cache and recrawls if timeout is exceeded.
        """
github chromakode / karmabot / karmabot / core / facets / karma.py View on Github external
# Copyright the Karmabot authors and contributors.
# All rights reserved.  See AUTHORS.
#
# This file is part of 'karmabot' and is distributed under the BSD license.
# See LICENSE for more details.
from karmabot.core.client import listen, thing
from karmabot.core.thing import ThingFacet
from karmabot.core.commands.sets import CommandSet
from karmabot.core.register import facet_registry, presenter_registry


@facet_registry.register
class KarmaFacet(ThingFacet):
    name = "karma"
    listens = listen.add_child(CommandSet(name))

    @classmethod
    def does_attach(cls, thing):
        return True

    @listens.add(u"{thing}++", help_str=u"add 1 to karma")
    def inc(self, thing, context):
        self.data.setdefault(context.who, 0)
        self.data[context.who] += 1
        return thing.name

    @listens.add(u"{thing}--", help_str=u"subtract 1 from karma")
    def dec(self, thing, context):
github chromakode / karmabot / karmabot / core / facets / manager.py View on Github external
def load_core(self):
        for facet in self.core_facets:
            facet_registry.register(facet)
github chromakode / karmabot / karmabot / core / facets / name.py View on Github external
# Copyright the Karmabot authors and contributors.
# All rights reserved.  See AUTHORS.
#
# This file is part of 'karmabot' and is distributed under the BSD license.
# See LICENSE for more details.
from karmabot.core import ircutils
from karmabot.core.client import thing
from karmabot.core.commands.sets import CommandSet
from karmabot.core.thing import ThingFacet
from karmabot.core.register import facet_registry, presenter_registry


@facet_registry.register
class NameFacet(ThingFacet):
    name = "name"
    commands = thing.add_child(CommandSet(name))

    @classmethod
    def does_attach(cls, thing):
        return True

    @commands.add(u"{thing}\?*", help_str=u"show information about {thing}")
    def describe(self, context, thing):
        # this is a thing object not the list of things
        context.reply(thing.describe(context))


@presenter_registry.register(set(["name"]), order=-10)
def present(thing, context):
github chromakode / karmabot / karmabot / core / facets / bot.py View on Github external
# Copyright the Karmabot authors and contributors.
# All rights reserved.  See AUTHORS.
#
# This file is part of 'karmabot' and is distributed under the BSD license.
# See LICENSE for more details.
from karmabot.core.client import VERSION
from karmabot.core import thing
from karmabot.core.register import facet_registry, presenter_registry


@facet_registry.register
class KarmaBotFacet(thing.ThingFacet):
    name = "karmabot"

    @classmethod
    def does_attach(cls, thing):
        return thing.name == "karmabot"

    #TODO: add save/reload/quit commands, customizable messages and behavior


@presenter_registry.register(set(["karmabot", "name", "karma", "description"]))
def present(thing, context):
    output_str = u"{name}[v{version}]({karma}): {descriptions} ({things} things)"
    text = output_str.format(
        name=thing.describe(context, facets=set(["name"])),
        karma=thing.facets["karma"].karma,
github chromakode / karmabot / karmabot / core / facets / irc.py View on Github external
def topic(self):
        return self.data.get("topic", None)

    @topic.setter
    def topic(self, value):
        self.data["topic"] = value


@presenter_registry.register(set(["ircchannel"]))
def present(thing, context):
    facet = thing.facets["ircchannel"]
    if facet.topic:
        return u"Topic: {topic}".format(topic=facet.topic)


@facet_registry.register
class IRCUserFacet(ThingFacet):
    #TODO: IRCUser facet, with trusted/admin types and verified hostmasks
    name = "ircuser"

    @classmethod
    def does_attach(cls, thing):
        # Attached by the listener
        return False

    @property
    def is_verified(self):
        return self.data.get("verified", False)

    @is_verified.setter
    def is_verified(self, value):
        self.data["verified"] = value