Make info object a mapping type

Make the info object a `Mapping` to play nicer with static type
checking. Simplify the task or actor context method lookup using a dict.
log_task_context
Tyler Goodlet 2019-12-08 19:40:08 -05:00
parent 52efbfc2cd
commit cf73283586
1 changed files with 16 additions and 14 deletions

View File

@ -2,6 +2,7 @@
Per process state
"""
from typing import Optional
from collections import Mapping
import trio
@ -16,21 +17,22 @@ def current_actor() -> 'Actor': # type: ignore
return _current_actor
class ActorContextInfo:
class ActorContextInfo(Mapping):
"Dyanmic lookup for local actor and task names"
_context_keys = ('task', 'actor')
def __len__(self):
return len(self._context_keys)
def __iter__(self):
return iter(('task', 'actor'))
return iter(self._context_keys)
def __getitem__(self, key: str):
if key == 'task':
try:
return trio._core.current_task().name
return {
'task': trio.hazmat.current_task,
'actor': current_actor
}[key]().name
except RuntimeError:
# not inside `trio.run()` yet
return 'no task context'
elif key == 'actor':
try:
return current_actor().name
except RuntimeError:
# no local actor initialize yet
return 'no actor context'
# no local actor/task context initialized yet
return f'no {key} context'