Add src.utils.parse.keyvalue, mostly for IRCv3 CAP negotiation

This commit is contained in:
jesopo 2018-11-05 11:56:28 +00:00
parent c2ebc7b5e4
commit 9c233cd4dd
2 changed files with 11 additions and 8 deletions

View file

@ -247,14 +247,7 @@ class Module(ModuleManager.BaseModule):
# the server is telling us about its capabilities!
@utils.hook("raw.cap")
def cap(self, event):
capabilities_list = event["args"][2].split(" ")
capabilities = {}
for capability in capabilities_list:
argument = None
if "=" in capability:
capability, argument = capability.split("=", 1)
capabilities[capability] = argument
capabilities = utils.parse.keyvalue(event["args"][2])
subcommand = event["args"][1].lower()
is_multiline = len(event["args"]) > 2 and event["args"][2] == "*"

View file

@ -55,3 +55,13 @@ def docstring(s: str) -> Docstring:
description += line
return Docstring(description, items, var_items)
def keyvalue(s, delimiter: str=" ") -> typing.Dict[str, str]:
items = {}
pairs = s.split(delimiter)
for pair in pairs:
key, sep, value = pair.partition("=")
if sep:
items[key] = value
else:
items[key] = None
return items