Split command StdOut/StdErr in to their own file (commands)
This commit is contained in:
parent
14db95601c
commit
6b042d9460
2 changed files with 66 additions and 62 deletions
|
@ -1,5 +1,6 @@
|
|||
import re
|
||||
from src import EventManager, ModuleManager, utils
|
||||
from . import outs
|
||||
|
||||
STR_MORE = "%s (more...)" % utils.consts.RESET
|
||||
STR_CONTINUED = "(...continued) "
|
||||
|
@ -12,64 +13,6 @@ OUT_CUTOFF = 400
|
|||
REGEX_CUTOFF = re.compile(r"^.{1,%d}(?:\s|$)" % OUT_CUTOFF)
|
||||
REGEX_ARG_NUMBER = re.compile(r"\$(\d+)")
|
||||
|
||||
class Out(object):
|
||||
def __init__(self, server, module_name, target, msgid, statusmsg):
|
||||
self.server = server
|
||||
self.module_name = module_name
|
||||
self._hide_prefix = False
|
||||
self.target = target
|
||||
self._text = ""
|
||||
self.written = False
|
||||
self._msgid = msgid
|
||||
self._statusmsg = statusmsg
|
||||
|
||||
def write(self, text):
|
||||
self._text += text
|
||||
self.written = True
|
||||
return self
|
||||
|
||||
def send(self, method):
|
||||
if self.has_text():
|
||||
text = self._text
|
||||
text_encoded = text.encode("utf8")
|
||||
if len(text_encoded) > OUT_CUTOFF:
|
||||
text = "%s%s" % (text_encoded[:OUT_CUTOFF].decode("utf8"
|
||||
).rstrip(), STR_MORE)
|
||||
self._text = "%s%s" % (STR_CONTINUED, text_encoded[OUT_CUTOFF:
|
||||
].decode("utf8").lstrip())
|
||||
else:
|
||||
self._text = ""
|
||||
|
||||
tags = {}
|
||||
if self._msgid:
|
||||
tags["+draft/reply"] = self._msgid
|
||||
|
||||
prefix = ""
|
||||
if not self._hide_prefix:
|
||||
prefix = utils.consts.RESET + "[%s] " % self.prefix()
|
||||
|
||||
target_str = "%s%s" % (self._statusmsg, self.target.name)
|
||||
full_text = "%s%s" % (prefix, text)
|
||||
if method == "PRIVMSG":
|
||||
self.server.send_message(target_str, full_text, tags=tags)
|
||||
elif method == "NOTICE":
|
||||
self.server.send_notice(target_str, full_text, tags=tags)
|
||||
|
||||
def set_prefix(self, prefix):
|
||||
self.module_name = prefix
|
||||
def hide_prefix(self):
|
||||
self._hide_prefix = True
|
||||
|
||||
def has_text(self):
|
||||
return bool(self._text)
|
||||
|
||||
class StdOut(Out):
|
||||
def prefix(self):
|
||||
return utils.irc.color(self.module_name, utils.consts.GREEN)
|
||||
class StdErr(Out):
|
||||
def prefix(self):
|
||||
return utils.irc.color("!"+self.module_name, utils.consts.RED)
|
||||
|
||||
def _command_method_validate(s):
|
||||
if s.upper() in COMMAND_METHODS:
|
||||
return s.upper()
|
||||
|
@ -174,9 +117,9 @@ class Module(ModuleManager.BaseModule):
|
|||
|
||||
msgid = event["tags"].get("draft/msgid", None)
|
||||
statusmsg = "".join(event.get("statusmsg", []))
|
||||
stdout = StdOut(event["server"], module_name, target, msgid,
|
||||
stdout = outs.StdOut(event["server"], module_name, target, msgid,
|
||||
statusmsg)
|
||||
stderr = StdErr(event["server"], module_name, target, msgid,
|
||||
stderr = outs.StdErr(event["server"], module_name, target, msgid,
|
||||
statusmsg)
|
||||
command_method = self._command_method(target, event["server"])
|
||||
|
||||
|
@ -390,7 +333,7 @@ class Module(ModuleManager.BaseModule):
|
|||
|
||||
@utils.hook("send.stdout")
|
||||
def send_stdout(self, event):
|
||||
stdout = StdOut(event["server"], event["module_name"],
|
||||
stdout = outs.StdOut(event["server"], event["module_name"],
|
||||
event["target"], event.get("msgid", None),
|
||||
event.get("statusmsg", ""))
|
||||
|
||||
|
@ -403,7 +346,7 @@ class Module(ModuleManager.BaseModule):
|
|||
event["target"].last_stdout = stdout
|
||||
@utils.hook("send.stderr")
|
||||
def send_stderr(self, event):
|
||||
stderr = StdErr(event["server"], event["module_name"],
|
||||
stderr = outs.StdErr(event["server"], event["module_name"],
|
||||
event["target"], event.get("msgid", None),
|
||||
event.get("statusmsg", ""))
|
||||
|
61
modules/commands/outs.py
Normal file
61
modules/commands/outs.py
Normal file
|
@ -0,0 +1,61 @@
|
|||
from src import utils
|
||||
|
||||
class Out(object):
|
||||
def __init__(self, server, module_name, target, msgid, statusmsg):
|
||||
self.server = server
|
||||
self.module_name = module_name
|
||||
self._hide_prefix = False
|
||||
self.target = target
|
||||
self._text = ""
|
||||
self.written = False
|
||||
self._msgid = msgid
|
||||
self._statusmsg = statusmsg
|
||||
|
||||
def write(self, text):
|
||||
self._text += text
|
||||
self.written = True
|
||||
return self
|
||||
|
||||
def send(self, method):
|
||||
if self.has_text():
|
||||
text = self._text
|
||||
text_encoded = text.encode("utf8")
|
||||
if len(text_encoded) > OUT_CUTOFF:
|
||||
text = "%s%s" % (text_encoded[:OUT_CUTOFF].decode("utf8"
|
||||
).rstrip(), STR_MORE)
|
||||
self._text = "%s%s" % (STR_CONTINUED, text_encoded[OUT_CUTOFF:
|
||||
].decode("utf8").lstrip())
|
||||
else:
|
||||
self._text = ""
|
||||
|
||||
|
||||
tags = {}
|
||||
if self._msgid:
|
||||
tags["+draft/reply"] = self._msgid
|
||||
|
||||
prefix = ""
|
||||
if not self._hide_prefix:
|
||||
prefix = utils.consts.RESET + "[%s] " % self.prefix()
|
||||
|
||||
target_str = "%s%s" % (self._statusmsg, self.target.name)
|
||||
full_text = "%s%s" % (prefix, text)
|
||||
if method == "PRIVMSG":
|
||||
self.server.send_message(target_str, full_text, tags=tags)
|
||||
elif method == "NOTICE":
|
||||
self.server.send_notice(target_str, full_text, tags=tags)
|
||||
|
||||
def set_prefix(self, prefix):
|
||||
self.module_name = prefix
|
||||
def hide_prefix(self):
|
||||
self._hide_prefix = True
|
||||
|
||||
def has_text(self):
|
||||
return bool(self._text)
|
||||
|
||||
class StdOut(Out):
|
||||
def prefix(self):
|
||||
return utils.irc.color(self.module_name, utils.consts.GREEN)
|
||||
class StdErr(Out):
|
||||
def prefix(self):
|
||||
return utils.irc.color("!"+self.module_name, utils.consts.RED)
|
||||
|
Loading…
Reference in a new issue