Simplify ParsedLine._tag_str and ParsedLine.format()

This commit is contained in:
jesopo 2019-02-27 22:14:53 +00:00
parent 846b881e52
commit d16046fb49

View file

@ -45,38 +45,35 @@ class ParsedLine(object):
self.tags = {} if tags == None else tags self.tags = {} if tags == None else tags
def _tag_str(self, tags: typing.Dict[str, str]) -> str: def _tag_str(self, tags: typing.Dict[str, str]) -> str:
tag_str = "" tag_pieces = []
for tag, value in tags.items(): for tag, value in tags.items():
if tag_str:
tag_str += ","
tag_str += tag
if value: if value:
tag_str += "=%s" % value tag_pieces.append("%s=%s" % (tag, value))
if tag_str: else:
tag_str = "@%s" % tag_str tag_pieces.append(tag)
return tag_str
if tag_pieces:
return "@%s" % ",".join(tag_pieces)
return ""
def format(self) -> str: def format(self) -> str:
s = "" pieces = []
if self.tags: if self.tags:
s += "%s " % self._tag_str(self.tags) pieces.append(self._tag_str(self.tags))
if self.prefix: if self.prefix:
s += "%s " % self.prefix pieces.append(self.prefix)
s += self.command.upper() pieces.append(self.command.upper())
if self.args: if self.args:
if len(self._args) > 1: for i, arg in enumerate(self._args):
s += " %s" % " ".join(self._args[:-1]) if i == len(self._args)-1 and (" " in arg or arg[0] == ":"):
pieces.append(":%s" % arg)
else:
pieces.append(arg)
s += " " return " ".join(pieces).split("\n")[0].strip("\r")
if " " in self._args[-1] or self._args[-1][0] == ":":
s += ":%s" % self._args[-1]
else:
s += self._args[-1]
return s.split("\n")[0].strip("\r")
class SentLine(IRCObject.Object): class SentLine(IRCObject.Object):
def __init__(self, send_time: datetime.datetime, hostmask: str, def __init__(self, send_time: datetime.datetime, hostmask: str,