github / src / scripting / trigger.py @ 4d2b7a46
1 |
# Copyright (c) 2016, LE GOFF Vincent |
---|---|
2 |
# All rights reserved.
|
3 |
|
4 |
# Redistribution and use in source and binary forms, with or without
|
5 |
# modification, are permitted provided that the following conditions are met:
|
6 |
|
7 |
# * Redistributions of source code must retain the above copyright notice, this
|
8 |
# list of conditions and the following disclaimer.
|
9 |
|
10 |
# * Redistributions in binary form must reproduce the above copyright notice,
|
11 |
# this list of conditions and the following disclaimer in the documentation
|
12 |
# and/or other materials provided with the distribution.
|
13 |
|
14 |
# * Neither the name of ytranslate nor the names of its
|
15 |
# contributors may be used to endorse or promote products derived from
|
16 |
# this software without specific prior written permission.
|
17 |
|
18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
19 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
20 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21 |
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
22 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
23 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
24 |
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
25 |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
26 |
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
27 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28 |
|
29 |
"""Class containing the Trigger class."""
|
30 |
|
31 |
import re |
32 |
|
33 |
class Trigger: |
34 |
|
35 |
"""A trigger object.
|
36 |
|
37 |
In a MUD client terminology, a trigger can "watch" the information
|
38 |
received by the client, and do something in return, like sending
|
39 |
an information to the server, playing a sound, calling a script
|
40 |
and so on.
|
41 |
|
42 |
"""
|
43 |
|
44 |
def __init__(self, sharp, reaction, action): |
45 |
self.sharp_engine = sharp
|
46 |
self.reaction = reaction
|
47 |
self.re_reaction = self.find_regex(reaction) |
48 |
self.action = action
|
49 |
|
50 |
def __repr__(self): |
51 |
return "<Trigger for {}>".format(repr(self.reaction)) |
52 |
|
53 |
def find_regex(self, reaction): |
54 |
"""Find and compile the reaction given as argument.
|
55 |
|
56 |
If the reaction begins with '^', the reaction is already a
|
57 |
regular expression that just needs to be compiled. Otherwise,
|
58 |
some automatic actions will be performed on it.
|
59 |
|
60 |
"""
|
61 |
if reaction.startswith("^"): |
62 |
return re.compile(reaction)
|
63 |
|
64 |
reaction = re.escape(reaction) |
65 |
|
66 |
# The '*' sign will be replaced by a group
|
67 |
reaction = reaction.replace("\\*", "(.*?)") |
68 |
reaction = "^" + reaction + "$" |
69 |
print "test", reaction |
70 |
|
71 |
return re.compile(reaction, re.IGNORECASE)
|
72 |
|
73 |
def feed(self, line): |
74 |
"""Should the trigger be triggered by the text?"""
|
75 |
if self.re_reaction.search(line): |
76 |
self.execute()
|
77 |
|
78 |
def execute(self): |
79 |
"""Execute the trigger."""
|
80 |
print "execute the trigger", self |
81 |
self.sharp_engine.execute(self.action) |
- « Previous
- 1
- 2
- 3
- 4
- Next »