<commit_before>"""Web Interface

This plugin provides a Web Interface to the system allowing the system
to be interacted with via a Web Browser.
"""


__version__ = "0.0.2"
__author__ = "<NAME>, prologic at shortcircuit dot net dot au"


from os import path
from cgi import escape
from traceback import format_exc
from os.path import dirname, abspath


from circuits.protocols.irc import strip
from circuits import handler, task, Component
from circuits.web import Server, Controller, JSONRPC, Static


import mako
from mako.lookup import TemplateLookup

from requests import head


import kdb
from kdb.utils import log
from kdb.events import cmd
from kdb.bot import wrapvalue
from kdb.plugin import BasePlugin


BASE = abspath(dirname(__file__))
DOCROOT = path.join(BASE, "static")

print "BASE:", BASE
print "DOCROOT:", DOCROOT

templates = TemplateLookup(
    directories=[path.join(BASE, "templates")],
    module_directory="/tmp",
    output_encoding="utf-8"
)

DEFAULTS = {
    "title": "{0:s} v{1:s} - {2:s}".format(
        kdb.__name__,
        kdb.__version__,
        kdb.__description__
    ),
    "software": "{0:s} v{1:s}".format(
        kdb.__name__,
        kdb.__version__,
    ),
}


def check_url(url):
    return head(url)


def render(name, **d):
    try:
        d.update(DEFAULTS)
        tpl = templates.get_template(name)
        return tpl.render(**d)
    except:
        return mako.exceptions.html_error_template().render()


class Commands(Component):

    channel = "commands"

    def status(self, source, target, args):
        """Report Web Status

        Syntax: STATUS
        """

        try:
            url = self.parent.server.http.base
            value = yield self.call(task(check_url, url), "workerthreads")
            response = value.value
            response.raise_for_status()
            yield "Web: Online"
        except Exception as error:
            yield "Web: Offline ({0:s})".format(error)


class WAPI(Component):

    channel = "wapi"

    @handler()
    def _on_wapi_event(self, event, *args, **kwargs):
        if event.channels != (self.channel,):
            return

        command = event.name

        if command not in self.parent.bot.command:
            yield log("Unknown Command: {0:s}", command)
        else:
            args = " ".join(args)
            event = cmd.create(command, None, None, args)

            try:
                value = yield self.call(event, "commands")
                yield "\n".join(
                    escape(strip(msg))
                    for msg in wrapvalue(command, event, value.value)
                )
            except Exception as error:
                message = "{0:s} {1:s}".format(command, " ".join(args))
                yield log("ERROR: {0:s}: ({1:s})", error, repr(message))
                log(format_exc())


class Root(Controller):

    def index(self):
        return render("index.html")


class Web(BasePlugin):
    """Web Plugin

    This plugin provides no user commands. This plugin provides
    a Web Interface to the system allowing the system to be
    interacted with via a Web Browser.
    """

    def init(self, *args, **kwargs):
        super(Web, self).init(*args, **kwargs)

        self.bind = self.config.get(
            "web", {}
        ).get(
            "bind", "0.0.0.0:8000"
        )
<commit_msg>[plugins.web]: Don't raise_for_status() when checking web status<commit_after>from os import path
from cgi import escape
from traceback import format_exc
from os.path import dirname, abspath


from circuits.protocols.irc import strip
from circuits import handler, task, Component
from circuits.web import Server, Controller, JSONRPC, Static


import mako
from mako.lookup import TemplateLookup

from requests import head


import kdb
from kdb.utils import log
from kdb.events import cmd
from kdb.bot import wrapvalue
from kdb.plugin import BasePlugin


BASE = abspath(dirname(__file__))
DOCROOT = path.join(BASE, "static")

print "BASE:", BASE
print "DOCROOT:", DOCROOT

templates = TemplateLookup(
    directories=[path.join(BASE, "templates")],
    module_directory="/tmp",
    output_encoding="utf-8"
)

DEFAULTS = {
    "title": "{0:s} v{1:s} - {2:s}".format(
        kdb.__name__,
        kdb.__version__,
        kdb.__description__
    ),
    "software": "{0:s} v{1:s}".format(
        kdb.__name__,
        kdb.__version__,
    ),
}


def check_url(url):
    return head(url)


def render(name, **d):
    try:
        d.update(DEFAULTS)
        tpl = templates.get_template(name)
        return tpl.render(**d)
    except:
        return mako.exceptions.html_error_template().render()


class Commands(Component):

    channel = "commands"

    def status(self, source, target, args):
        """Report Web Status

        Syntax: STATUS
        """

        try:
            url = self.parent.server.http.base
            yield self.call(task(check_url, url), "workerthreads")
            yield "Web: Online"
        except Exception as error:
            yield "Web: Offline ({0:s})".format(error)


class WAPI(Component):

    channel = "wapi"

    @handler()
    def _on_wapi_event(self, event, *args, **kwargs):
        if event.channels != (self.channel,):
            return

        command = event.name

        if command not in self.parent.bot.command:
            yield log("Unknown Command: {0:s}", command)
        else:
            args = " ".join(args)
            event = cmd.create(command, None, None, args)

            try:
                value = yield self.call(event, "commands")
                yield "\n".join(
                    escape(strip(msg))
                    for msg in wrapvalue(command, event, value.value)
                )
            except Exception as error:
                message = "{0:s} {1:s}".format(command, " ".join(args))
                yield log("ERROR: {0:s}: ({1:s})", error, repr(message))
                log(format_exc())


class Root(Controller):

    def index(self):
        return render("index.html")


class Web(BasePlugin):
    """Web Plugin

    This plugin provides no user commands. This plugin provides
    a Web Interface to the system allowing the system to be
    interacted with via a Web Browser.
    """

    __version__ = "0.1.0"
    __author__ = "<NAME>, prologic at shortcircuit dot net dot au"

    def init(self, *args, **kwargs):
        super(Web, self).init(*args, **kwargs)

        self.bind = self.config.get(
            "web", {}
        ).get(
            "bind", "0.0.0.0:8000"
        )

{'fraction_non_alphanumeric': 0.11932078935291418, 'fraction_numerical': 0.007036867064402631, 'mean_word_length': 2.332313965341488, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4010092', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1917, 'n_words': 558}