anonstream/anonstream/control/parse.py

46 行
1.5 KiB
Python
Raw 通常表示 履歴

2022-06-16 10:23:11 +09:00
# SPDX-FileCopyrightText: 2022 n9k <https://git.076.ne.jp/ninya9k>
# SPDX-License-Identifier: AGPL-3.0-or-later
2022-06-16 10:05:18 +09:00
from anonstream.control.spec import ParseException, Parsed
from anonstream.control.spec.common import Str
from anonstream.control.spec.methods.allowedness import SPEC as SPEC_ALLOWEDNESS
from anonstream.control.spec.methods.chat import SPEC as SPEC_CHAT
2022-08-01 09:42:04 +09:00
from anonstream.control.spec.methods.emote import SPEC as SPEC_EMOTE
from anonstream.control.spec.methods.help import SPEC as SPEC_HELP
from anonstream.control.spec.methods.quit import SPEC as SPEC_QUIT
from anonstream.control.spec.methods.title import SPEC as SPEC_TITLE
from anonstream.control.spec.methods.user import SPEC as SPEC_USER
2022-06-14 17:29:32 +09:00
SPEC = Str({
'help': SPEC_HELP,
'quit': SPEC_QUIT,
'title': SPEC_TITLE,
'chat': SPEC_CHAT,
'user': SPEC_USER,
'allowednesss': SPEC_ALLOWEDNESS,
2022-08-01 09:42:04 +09:00
'emote': SPEC_EMOTE,
})
2022-06-14 17:29:32 +09:00
async def parse(request):
words = request.split()
if not words:
normal, response = None, ''
2022-06-14 17:29:32 +09:00
else:
spec = SPEC
index = 0
args = []
while True:
2022-06-14 17:29:32 +09:00
try:
spec, n_consumed, more_args = spec.consume(words, index)
2022-06-16 10:05:18 +09:00
except ParseException as e:
normal, response = None, e.args[0] + '\n'
break
except Parsed as e:
fn, *_ = e.args
normal, response = await fn(*args)
break
else:
index += n_consumed
args.extend(more_args)
2022-06-14 17:29:32 +09:00
return normal, response