# Path: karmabot/core/client.py
# class Context(object):
# class KarmaBot(irc.IRCClient):
# class KarmaBotFactory(ReconnectingClientFactory):
#     def __init__(self, user, where, bot, private=False):
#     def nick(self):
#     def reply(self, msg, where=None, replied=True):
#     def connectionMade(self):
#     def init(self):
#     def connectionLost(self, reason):
#     def signedOn(self):
#     def join_with_key(self, channel):
#     def save(self):
#     def topicUpdated(self, user, channel, newTopic):
#     def error_msg(self, channel):
#     def msg(self, channel, message, length=160, priv=False):
#     def privmsg(self, user, channel, msg):
#     def tell_yes(self, who, nick):
#     def __init__(self, filename, nick, channels, trusted, password=None,
#                  extensions=[]):
#     def buildProtocol(self, addr):
#     def clientConnectionLost(self, connector, reason):
#     def clientConnectionFailed(self, connector, reason):
# 
# Path: karmabot/core/commands/sets.py
# class CommandSet(object):
# 
#     def __init__(self, name, regex_format="{0}", parent=None):
#         self.name = name
#         self.regex_format = regex_format
#         self.parent = parent
#         self.children = []
#         self.commands = []
# 
#     def __iter__(self):
#         return iter(self.commands)
# 
#     def add_child(self, command_set):
#         command_set.parent = self
#         self.children.append(command_set)
#         return command_set
# 
#     def add(self, format, help_str, exclusive=False, visible=True):
#         def decorator(f):
#             self.commands.append(
#                 Command(self, format, f, help_str, visible, exclusive))
#             return f
#         return decorator
# 
#     def compile(self):
#         def traverse_commands(cmdset):
#             child_commands = (child.commands for child in cmdset.children)
#             for command in chain(cmdset.commands, *child_commands):
#                 yield command
# 
#         command_infos = []
#         for command in traverse_commands(self):
#             regex = command.to_regex()
#             formatted_regex = self.regex_format.format(regex)
# 
#             command_info = {"re": re.compile(formatted_regex, re.U),
#                             "command": command,
#                             "exclusive": command.exclusive}
#             command_infos.append(command_info)
# 
#         # Sort exclusive commands before non-exclusive ones
#         command_infos.sort(key=lambda c: c["exclusive"], reverse=True)
# 
#         return CommandParser(command_infos)
# 
# Path: karmabot/core/register.py
# class FacetRegistry(dict):
#     def register(self, facet_class):
#     def __iter__(self):
#     def attach(self, subject, exclude=set()):
# 
# Path: karmabot/core/facets/base.py
# class Facet(object):
#     name = None
#     commands = None
#     listens = None
#     display_key = -1
# 
#     def __init__(self, subject):
#         self.subject = subject
#         if self.does_attach(subject):
#             subject.add_facet(self)
#             self.on_attach()
# 
#     def __str__(self):
#         return self.name
# 
#     @property
#     def data(self):
#         return self.subject.data.setdefault(self.name, {})
# 
#     def does_attach(self, subject):
#         raise NotImplementedError
# 
#     def on_attach(self):
#         pass
# 
#     def present(self, context):
#         return u""


from json import JSONDecoder
from urllib import urlencode
from urllib2 import urlopen
from karmabot.core.client import thing
from karmabot.core.commands.sets import CommandSet
from karmabot.core.register import facet_registry
from karmabot.core.facets import Facet
import re
import htmlentitydefs
def unescape(text):
    def fixup(m):
        text = m.group(0)
        if text[:2] == "&#":
            # character reference
            try:
                if text[:3] == "&#x":
                    return unichr(int(text[3:-1], 16))
                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(Facet):
    name = "lmgtfy"
    commands = thing.add_child(CommandSet(name))

    @classmethod
    def does_attach(cls, thing):

# Path: karmabot/core/client.py
# class Context(object):
# class KarmaBot(irc.IRCClient):
# class KarmaBotFactory(ReconnectingClientFactory):
#     def __init__(self, user, where, bot, private=False):
#     def nick(self):
#     def reply(self, msg, where=None, replied=True):
#     def connectionMade(self):
#     def init(self):
#     def connectionLost(self, reason):
#     def signedOn(self):
#     def join_with_key(self, channel):
#     def save(self):
#     def topicUpdated(self, user, channel, newTopic):
#     def error_msg(self, channel):
#     def msg(self, channel, message, length=160, priv=False):
#     def privmsg(self, user, channel, msg):
#     def tell_yes(self, who, nick):
#     def __init__(self, filename, nick, channels, trusted, password=None,
#                  extensions=[]):
#     def buildProtocol(self, addr):
#     def clientConnectionLost(self, connector, reason):
#     def clientConnectionFailed(self, connector, reason):
# 
# Path: karmabot/core/commands/sets.py
# class CommandSet(object):
# 
#     def __init__(self, name, regex_format="{0}", parent=None):
#         self.name = name
#         self.regex_format = regex_format
#         self.parent = parent
#         self.children = []
#         self.commands = []
# 
#     def __iter__(self):
#         return iter(self.commands)
# 
#     def add_child(self, command_set):
#         command_set.parent = self
#         self.children.append(command_set)
#         return command_set
# 
#     def add(self, format, help_str, exclusive=False, visible=True):
#         def decorator(f):
#             self.commands.append(
#                 Command(self, format, f, help_str, visible, exclusive))
#             return f
#         return decorator
# 
#     def compile(self):
#         def traverse_commands(cmdset):
#             child_commands = (child.commands for child in cmdset.children)
#             for command in chain(cmdset.commands, *child_commands):
#                 yield command
# 
#         command_infos = []
#         for command in traverse_commands(self):
#             regex = command.to_regex()
#             formatted_regex = self.regex_format.format(regex)
# 
#             command_info = {"re": re.compile(formatted_regex, re.U),
#                             "command": command,
#                             "exclusive": command.exclusive}
#             command_infos.append(command_info)
# 
#         # Sort exclusive commands before non-exclusive ones
#         command_infos.sort(key=lambda c: c["exclusive"], reverse=True)
# 
#         return CommandParser(command_infos)
# 
# Path: karmabot/core/register.py
# class FacetRegistry(dict):
#     def register(self, facet_class):
#     def __iter__(self):
#     def attach(self, subject, exclude=set()):
# 
# Path: karmabot/core/facets/base.py
# class Facet(object):
#     name = None
#     commands = None
#     listens = None
#     display_key = -1
# 
#     def __init__(self, subject):
#         self.subject = subject
#         if self.does_attach(subject):
#             subject.add_facet(self)
#             self.on_attach()
# 
#     def __str__(self):
#         return self.name
# 
#     @property
#     def data(self):
#         return self.subject.data.setdefault(self.name, {})
# 
#     def does_attach(self, subject):
#         raise NotImplementedError
# 
#     def on_attach(self):
#         pass
# 
#     def present(self, context):
#         return u""


# Path: karmabot/extensions/lmgtfy.py
from json import JSONDecoder
from urllib import urlencode
from urllib2 import urlopen
from karmabot.core.client import thing
from karmabot.core.commands.sets import CommandSet
from karmabot.core.register import facet_registry
from karmabot.core.facets import Facet
import re
import htmlentitydefs

def unescape(text):
    def fixup(m):
        text = m.group(0)
        if text[:2] == "&#":
            # character reference
            try:
                if text[:3] == "&#x":
                    return unichr(int(text[3:-1], 16))
                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(Facet):
    name = "lmgtfy"
    commands = thing.add_child(CommandSet(name))

    @classmethod
    def does_attach(cls, thing):

        return thing.name == "lmgtfy"