Control socket: minor redo exceptions

このコミットが含まれているのは:
n9k 2022-06-16 01:05:18 +00:00
コミット 309b2ad54f
7個のファイルの変更26行の追加23行の削除

ファイルの表示

@ -1,5 +1,5 @@
class Exit(Exception): class ControlSocketExit(Exception):
pass pass
class Fail(Exception): class CommandFailed(Exception):
pass pass

ファイルの表示

@ -1,4 +1,4 @@
from anonstream.control.spec import NoParse, Ambiguous, Parsed from anonstream.control.spec import ParseException, Parsed
from anonstream.control.spec.common import Str from anonstream.control.spec.common import Str
from anonstream.control.spec.methods.chat import SPEC as SPEC_CHAT from anonstream.control.spec.methods.chat import SPEC as SPEC_CHAT
from anonstream.control.spec.methods.exit import SPEC as SPEC_EXIT from anonstream.control.spec.methods.exit import SPEC as SPEC_EXIT
@ -25,10 +25,7 @@ async def parse(request):
while True: while True:
try: try:
spec, n_consumed, more_args = spec.consume(words, index) spec, n_consumed, more_args = spec.consume(words, index)
except NoParse as e: except ParseException as e:
normal, response = None, e.args[0] + '\n'
break
except Ambiguous as e:
normal, response = None, e.args[0] + '\n' normal, response = None, e.args[0] + '\n'
break break
except Parsed as e: except Parsed as e:

ファイルの表示

@ -1,6 +1,6 @@
import asyncio import asyncio
from anonstream.control.exceptions import Exit, Fail from anonstream.control.exceptions import ControlSocketExit, CommandFailed
from anonstream.control.parse import parse from anonstream.control.parse import parse
def start_control_server_at(address): def start_control_server_at(address):
@ -15,9 +15,9 @@ async def serve_control_client(reader, writer):
else: else:
try: try:
normal, response = await parse(request) normal, response = await parse(request)
except Fail as e: except CommandFailed as e:
normal, response = None, e.args[0] + '\n' normal, response = None, e.args[0] + '\n'
except Exit: except ControlSocketExit:
writer.close() writer.close()
break break

ファイルの表示

@ -1,7 +1,13 @@
class NoParse(Exception): class ParseException(Exception):
pass pass
class Ambiguous(Exception): class NoParse(ParseException):
pass
class Ambiguous(ParseException):
pass
class BadArgument(ParseException):
pass pass
class Parsed(Exception): class Parsed(Exception):

ファイルの表示

@ -1,8 +1,8 @@
from anonstream.control.spec.common import Str, End from anonstream.control.spec.common import Str, End
from anonstream.control.exceptions import Exit from anonstream.control.exceptions import ControlSocketExit
async def cmd_exit(): async def cmd_exit():
raise Exit raise ControlSocketExit
async def cmd_exit_help(): async def cmd_exit_help():
normal = ['exit', 'help'] normal = ['exit', 'help']

ファイルの表示

@ -1,6 +1,6 @@
import json import json
from anonstream.control.exceptions import Fail from anonstream.control.exceptions import CommandFailed
from anonstream.control.spec import Spec, NoParse from anonstream.control.spec import Spec, NoParse
from anonstream.control.spec.common import Str, End, ArgsJsonString from anonstream.control.spec.common import Str, End, ArgsJsonString
from anonstream.control.spec.utils import get_item, json_dumps_contiguous from anonstream.control.spec.utils import get_item, json_dumps_contiguous
@ -27,7 +27,7 @@ async def cmd_title_set(title):
try: try:
await set_stream_title(title) await set_stream_title(title)
except OSError as e: except OSError as e:
raise Fail(f'could not set title: {e}') from e raise CommandFailed(f'could not set title: {e}') from e
normal = ['title', 'set', json_dumps_contiguous(title)] normal = ['title', 'set', json_dumps_contiguous(title)]
response = '' response = ''
return normal, response return normal, response

ファイルの表示

@ -2,8 +2,8 @@ import json
from quart import current_app from quart import current_app
from anonstream.control.exceptions import Fail from anonstream.control.exceptions import CommandFailed
from anonstream.control.spec import NoParse from anonstream.control.spec import BadArgument
from anonstream.control.spec.common import Str, End, ArgsInt, ArgsString, ArgsJson, ArgsJsonString from anonstream.control.spec.common import Str, End, ArgsInt, ArgsString, ArgsJson, ArgsJsonString
from anonstream.control.spec.utils import get_item, json_dumps_contiguous from anonstream.control.spec.utils import get_item, json_dumps_contiguous
from anonstream.utils.user import USER_WEBSOCKET_ATTRS from anonstream.utils.user import USER_WEBSOCKET_ATTRS
@ -17,7 +17,7 @@ class ArgsJsonTokenUser(ArgsJsonString):
try: try:
user = USERS_BY_TOKEN[token] user = USERS_BY_TOKEN[token]
except KeyError: except KeyError:
raise NoParse(f'no user with token {token!r}') raise BadArgument(f'no user with token {token!r}')
return user return user
class ArgsJsonHashUser(ArgsString): class ArgsJsonHashUser(ArgsString):
@ -26,7 +26,7 @@ class ArgsJsonHashUser(ArgsString):
if user['token_hash'] == token_hash: if user['token_hash'] == token_hash:
break break
else: else:
raise NoParse(f'no user with token_hash {token_hash!r}') raise BadArgument(f'no user with token_hash {token_hash!r}')
return user return user
def ArgsUser(spec): def ArgsUser(spec):
@ -69,11 +69,11 @@ async def cmd_user_get(user, attr):
try: try:
value = user[attr] value = user[attr]
except KeyError as e: except KeyError as e:
raise Fail('user has no such attribute') from e raise CommandFailed('user has no such attribute') from e
try: try:
value_json = json.dumps(value) value_json = json.dumps(value)
except (TypeError, ValueError) as e: except (TypeError, ValueError) as e:
raise Fail('value is not representable in json') from e raise CommandFailed('value is not representable in json') from e
normal = [ normal = [
'user', 'user',
'get', 'get',
@ -86,7 +86,7 @@ async def cmd_user_get(user, attr):
async def cmd_user_set(user, attr, value): async def cmd_user_set(user, attr, value):
if attr not in user: if attr not in user:
raise Fail(f'user has no attribute {attr!r}') raise CommandFailed(f'user has no attribute {attr!r}')
user[attr] = value user[attr] = value
if attr in USER_WEBSOCKET_ATTRS: if attr in USER_WEBSOCKET_ATTRS:
USERS_UPDATE_BUFFER.add(user['token']) USERS_UPDATE_BUFFER.add(user['token'])