magic-wormhole/src/wormhole/_code.py

89 lines
2.8 KiB
Python
Raw Normal View History

2017-02-24 02:11:07 +00:00
from __future__ import print_function, absolute_import, unicode_literals
from zope.interface import implementer
2017-02-23 00:56:39 +00:00
from attr import attrs, attrib
from attr.validators import provides
from automat import MethodicalMachine
from . import _interfaces
2017-02-23 00:56:39 +00:00
@attrs
@implementer(_interfaces.ICode)
class Code(object):
2017-02-23 00:56:39 +00:00
_timing = attrib(validator=provides(_interfaces.ITiming))
m = MethodicalMachine()
@m.setTrace()
def set_trace(): pass # pragma: no cover
2017-02-23 00:56:39 +00:00
2017-03-17 23:50:37 +00:00
def wire(self, boss, allocator, nameplate, key, input):
self._B = _interfaces.IBoss(boss)
2017-03-15 07:43:25 +00:00
self._A = _interfaces.IAllocator(allocator)
self._N = _interfaces.INameplate(nameplate)
self._K = _interfaces.IKey(key)
self._I = _interfaces.IInput(input)
@m.state(initial=True)
2017-03-17 23:50:37 +00:00
def S0_idle(self): pass # pragma: no cover
@m.state()
2017-03-17 23:50:37 +00:00
def S1_inputting_nameplate(self): pass # pragma: no cover
@m.state()
2017-03-17 23:50:37 +00:00
def S2_inputting_words(self): pass # pragma: no cover
@m.state()
2017-03-17 23:50:37 +00:00
def S3_allocating(self): pass # pragma: no cover
2017-03-11 22:18:58 +00:00
@m.state()
2017-03-17 23:50:37 +00:00
def S4_known(self): pass # pragma: no cover
# from App
@m.input()
2017-03-17 23:50:37 +00:00
def allocate_code(self, length, wordlist): pass
@m.input()
def input_code(self): pass
@m.input()
2017-02-24 02:23:55 +00:00
def set_code(self, code): pass
2017-03-17 23:50:37 +00:00
# from Allocator
@m.input()
2017-03-17 23:50:37 +00:00
def allocated(self, nameplate, code): pass
2017-03-11 22:18:58 +00:00
# from Input
@m.input()
def got_nameplate(self, nameplate): pass
@m.input()
def finished_input(self, code): pass
@m.output()
2017-03-17 23:50:37 +00:00
def do_set_code(self, code):
nameplate = code.split("-", 2)[0]
self._N.set_nameplate(nameplate)
self._K.got_code(code)
self._B.got_code(code)
@m.output()
def do_start_input(self):
return self._I.start()
2017-03-11 22:18:58 +00:00
@m.output()
2017-03-17 23:50:37 +00:00
def do_middle_input(self, nameplate):
2017-03-11 22:18:58 +00:00
self._N.set_nameplate(nameplate)
@m.output()
2017-03-17 23:50:37 +00:00
def do_finish_input(self, code):
self._K.got_code(code)
self._B.got_code(code)
2017-03-11 22:18:58 +00:00
@m.output()
2017-03-17 23:50:37 +00:00
def do_start_allocate(self, length, wordlist):
self._A.allocate(length, wordlist)
@m.output()
2017-03-17 23:50:37 +00:00
def do_finish_allocate(self, nameplate, code):
assert code.startswith(nameplate+"-"), (nameplate, code)
2017-03-17 23:50:37 +00:00
self._N.set_nameplate(nameplate)
self._K.got_code(code)
self._B.got_code(code)
S0_idle.upon(set_code, enter=S4_known, outputs=[do_set_code])
S0_idle.upon(input_code, enter=S1_inputting_nameplate,
outputs=[do_start_input])
S1_inputting_nameplate.upon(got_nameplate, enter=S2_inputting_words,
outputs=[do_middle_input])
S2_inputting_words.upon(finished_input, enter=S4_known,
outputs=[do_finish_input])
S0_idle.upon(allocate_code, enter=S3_allocating, outputs=[do_start_allocate])
S3_allocating.upon(allocated, enter=S4_known, outputs=[do_finish_allocate])