File: dyna.py

package info (click to toggle)
errbot 6.2.0%2Bds-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,856 kB
  • sloc: python: 11,572; makefile: 163; sh: 97
file content (96 lines) | stat: -rw-r--r-- 2,561 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from __future__ import absolute_import

from errbot import BotPlugin, Command, arg_botcmd, botcmd, botmatch


def say_foo(plugin, msg, args):
    return "foo %s" % type(plugin)


class Dyna(BotPlugin):
    """Just a test plugin to see if dynamic plugin API works."""

    @botcmd
    def add_simple(self, _, _1):
        simple1 = Command(
            lambda plugin, msg, args: "yep %s" % type(plugin), name="say_yep"
        )
        simple2 = Command(say_foo)

        self.create_dynamic_plugin(
            "simple with special#", (simple1, simple2), doc="documented"
        )

        return "added"

    @botcmd
    def remove_simple(self, msg, args):
        self.destroy_dynamic_plugin("simple with special#")
        return "removed"

    @botcmd
    def add_arg(self, _, _1):
        cmd1_name = "echo_to_me"
        cmd1 = Command(
            lambda plugin, msg, args: "string to echo is %s" % args.positional_arg,
            cmd_type=arg_botcmd,
            cmd_args=("positional_arg",),
            cmd_kwargs={"unpack_args": False, "name": cmd1_name},
            name=cmd1_name,
        )

        self.create_dynamic_plugin("arg", (cmd1,), doc="documented")

        return "added"

    @botcmd
    def remove_arg(self, msg, args):
        self.destroy_dynamic_plugin("arg")
        return "removed"

    @botcmd
    def add_re(self, _, _1):
        re1 = Command(
            lambda plugin, msg, match: "fffound",
            name="ffound",
            cmd_type=botmatch,
            cmd_args=(r"^.*cheese.*$",),
        )
        self.create_dynamic_plugin("re", (re1,))
        return "added"

    @botcmd
    def remove_re(self, msg, args):
        self.destroy_dynamic_plugin("re")
        return "removed"

    @botcmd
    def add_saw(self, _, _1):
        re1 = Command(
            lambda plugin, msg, args: "+".join(args),
            name="splitme",
            cmd_type=botcmd,
            cmd_kwargs={"split_args_with": ","},
        )
        self.create_dynamic_plugin("saw", (re1,))
        return "added"

    @botcmd
    def remove_saw(self, msg, args):
        self.destroy_dynamic_plugin("saw")
        return "removed"

    @botcmd
    def clash(self, msg, args):
        return "original"

    @botcmd
    def add_clashing(self, _, _1):
        simple1 = Command(lambda plugin, msg, args: "dynamic", name="clash")
        self.create_dynamic_plugin("clashing", (simple1,))
        return "added"

    @botcmd
    def remove_clashing(self, _, _1):
        self.destroy_dynamic_plugin("clashing")
        return "removed"