1
0
Fork 0

Refactor in treatment analysis

This commit is contained in:
nemunaire 2018-02-10 09:51:51 +01:00
parent 2af56e606a
commit 342bb9acdc
3 changed files with 34 additions and 47 deletions

View File

@ -272,7 +272,6 @@ def treat_alias(msg):
# Avoid infinite recursion
if not isinstance(rpl_msg, Command) or msg.cmd != rpl_msg.cmd:
# Also return origin message, if it can be treated as well
return [msg, rpl_msg]
return rpl_msg
return msg

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import types
def call_game(call, *args, **kargs):
"""With given args, try to determine the right call to make
@ -119,10 +121,18 @@ class Abstract:
try:
if self.check(data1):
ret = call_game(self.call, data1, self.data, *args)
if isinstance(ret, types.GeneratorType):
for r in ret:
yield r
ret = None
except IMException as e:
ret = e.fill_response(data1)
finally:
if self.times == 0:
self.call_end(ret)
return ret
if isinstance(ret, list):
for r in ret:
yield ret
elif ret is not None:
yield ret

View File

@ -15,7 +15,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import types
logger = logging.getLogger("nemubot.treatment")
@ -79,19 +78,12 @@ class MessageTreater:
for h in self.hm.get_hooks("pre", type(msg).__name__):
if h.can_read(msg.to, msg.server) and h.match(msg):
res = h.run(msg)
for res in flatify(h.run(msg)):
if res is not None and res != msg:
yield from self._pre_treat(res)
if isinstance(res, list):
for i in range(len(res)):
# Avoid infinite loop
if res[i] != msg:
yield from self._pre_treat(res[i])
elif res is not None and res != msg:
yield from self._pre_treat(res)
elif res is None or res is False:
break
elif res is None or res is False:
break
else:
yield msg
@ -113,25 +105,10 @@ class MessageTreater:
msg.frm_owner = (not hasattr(msg.server, "owner") or msg.server.owner == msg.frm)
while hook is not None:
res = hook.run(msg)
if isinstance(res, list):
for r in res:
yield r
elif res is not None:
if isinstance(res, types.GeneratorType):
for r in res:
if not hasattr(r, "server") or r.server is None:
r.server = msg.server
yield r
else:
if not hasattr(res, "server") or res.server is None:
res.server = msg.server
yield res
for res in flatify(hook.run(msg)):
if not hasattr(res, "server") or res.server is None:
res.server = msg.server
yield res
hook = next(hook_gen, None)
@ -165,19 +142,20 @@ class MessageTreater:
for h in self.hm.get_hooks("post", type(msg).__name__):
if h.can_write(msg.to, msg.server) and h.match(msg):
res = h.run(msg)
for res in flatify(h.run(msg)):
if res is not None and res != msg:
yield from self._post_treat(res)
if isinstance(res, list):
for i in range(len(res)):
# Avoid infinite loop
if res[i] != msg:
yield from self._post_treat(res[i])
elif res is not None and res != msg:
yield from self._post_treat(res)
elif res is None or res is False:
break
elif res is None or res is False:
break
else:
yield msg
def flatify(g):
if hasattr(g, "__iter__"):
for i in g:
yield from flatify(i)
else:
yield g