How to use the spaghetti.Transition function in spaghetti

To help you get started, we’ve selected a few spaghetti examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

meejah / txtorcon / txtorcon / torcontrolprotocol.py View on Github
``````self._start_command))
self._is_continuation_line,
self._start_command))

self._is_continuation_line,
self._accumulate_response))
self._is_finish_line,

self._is_end_line,
lambda x: None))
self._is_not_end_line,
self._accumulate_multi_response))

self.fsm = FSM([recvnotify, idle, recvmulti, recv])
self.state_idle = idle
## hand-set initial state default start state is first in the
## list; the above looks nice in dotty though
self.fsm.state = idle
if DEBUG:
self.debuglog = open('txtorcon-debug.log', 'w')
with open('fsm.dot', 'w') as fsmfile:
fsmfile.write(self.fsm.dotty())``````
meejah / txtorcon / txtorcon / torstate.py View on Github
``````waiting_r.add_transition(Transition(eat_line, lambda x: x.strip() == '.' or x.strip() == 'OK', nothing))
waiting_r.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin))
## FIXME use better method/func than die!!
waiting_r.add_transition(Transition(eat_line, lambda x: x[:2] != 'r ', die('Expected "r " while parsing routers not "%s"')))

waiting_s.add_transition(Transition(waiting_w, lambda x: x[:2] == 's ', self._router_flags))
waiting_s.add_transition(Transition(eat_line, lambda x: x[:2] != 's ', die('Expected "s " while parsing routers not "%s"')))
waiting_s.add_transition(Transition(eat_line, lambda x: x.strip() == '.', nothing))

waiting_w.add_transition(Transition(waiting_p, lambda x: x[:2] == 'w ', self._router_bandwidth))
waiting_w.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin)) # "w" lines are optional
waiting_w.add_transition(Transition(eat_line, lambda x: x[:2] != 'w ', die('Expected "w " while parsing routers not "%s"')))
waiting_w.add_transition(Transition(eat_line, lambda x: x.strip() == '.', nothing))

waiting_p.add_transition(Transition(waiting_r, lambda x: x[:2] == 'p ', self._router_policy))
waiting_p.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin)) # "p" lines are optional
waiting_p.add_transition(Transition(eat_line, lambda x: x[:2] != 'p ', die('Expected "p " while parsing routers not "%s"')))
waiting_p.add_transition(Transition(eat_line, lambda x: x.strip() == '.', nothing))

self._network_status_parser = FSM([eat_line, waiting_r, waiting_s, waiting_w, waiting_p])

self.post_bootstrap = defer.Deferred()
if bootstrap:
if self.protocol.post_bootstrap:
else:
self._bootstrap()``````
meejah / txtorcon / txtorcon / torstate.py View on Github
``````waiting_s = State("waiting_s")

def ignorable_line(x):
return x.strip() == '.' or x.strip() == 'OK' or x[:3] == 'ns/' or x.strip() == ''

waiting_r.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin))
## FIXME use better method/func than die!!
waiting_r.add_transition(Transition(waiting_r, lambda x: x[:2] != 'r ', die('Expected "r " while parsing routers not "%s"')))

waiting_s.add_transition(Transition(waiting_w, lambda x: x[:2] == 's ', self._router_flags))
waiting_s.add_transition(Transition(waiting_r, lambda x: x[:2] != 's ', die('Expected "s " while parsing routers not "%s"')))
waiting_s.add_transition(Transition(waiting_r, lambda x: x.strip() == '.', nothing))

waiting_w.add_transition(Transition(waiting_p, lambda x: x[:2] == 'w ', self._router_bandwidth))
waiting_w.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin)) # "w" lines are optional
waiting_w.add_transition(Transition(waiting_r, lambda x: x[:2] != 'w ', die('Expected "w " while parsing routers not "%s"')))
waiting_w.add_transition(Transition(waiting_r, lambda x: x.strip() == '.', nothing))

waiting_p.add_transition(Transition(waiting_r, lambda x: x[:2] == 'p ', self._router_policy))
waiting_p.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin)) # "p" lines are optional
waiting_p.add_transition(Transition(waiting_r, lambda x: x[:2] != 'p ', die('Expected "p " while parsing routers not "%s"')))
waiting_p.add_transition(Transition(waiting_r, lambda x: x.strip() == '.', nothing))

self._network_status_parser = FSM([waiting_r, waiting_s, waiting_w, waiting_p])
if write_state_diagram:
with open('routerfsm.dot', 'w') as fsmfile:
fsmfile.write(self._network_status_parser.dotty())``````
meejah / txtorcon / txtorcon / torstate.py View on Github
``````def nothing(*args):
pass

waiting_r = State("waiting_r")
waiting_w = State("waiting_w")
waiting_p = State("waiting_p")
waiting_s = State("waiting_s")

def ignorable_line(x):
return x.strip() == '.' or x.strip() == 'OK' or x[:3] == 'ns/' or x.strip() == ''

waiting_r.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin))
## FIXME use better method/func than die!!
waiting_r.add_transition(Transition(waiting_r, lambda x: x[:2] != 'r ', die('Expected "r " while parsing routers not "%s"')))

waiting_s.add_transition(Transition(waiting_w, lambda x: x[:2] == 's ', self._router_flags))
waiting_s.add_transition(Transition(waiting_r, lambda x: x[:2] != 's ', die('Expected "s " while parsing routers not "%s"')))
waiting_s.add_transition(Transition(waiting_r, lambda x: x.strip() == '.', nothing))

waiting_w.add_transition(Transition(waiting_p, lambda x: x[:2] == 'w ', self._router_bandwidth))
waiting_w.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin)) # "w" lines are optional
waiting_w.add_transition(Transition(waiting_r, lambda x: x[:2] != 'w ', die('Expected "w " while parsing routers not "%s"')))
waiting_w.add_transition(Transition(waiting_r, lambda x: x.strip() == '.', nothing))

waiting_p.add_transition(Transition(waiting_r, lambda x: x[:2] == 'p ', self._router_policy))
waiting_p.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin)) # "p" lines are optional
waiting_p.add_transition(Transition(waiting_r, lambda x: x[:2] != 'p ', die('Expected "p " while parsing routers not "%s"')))``````
meejah / txtorcon / txtorcon / torstate.py View on Github
``````waiting_w = State("waiting_w")
waiting_p = State("waiting_p")
waiting_s = State("waiting_s")

def ignorable_line(x):
return x.strip() == '.' or x.strip() == 'OK' or x[:3] == 'ns/' or x.strip() == ''

waiting_r.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin))
## FIXME use better method/func than die!!
waiting_r.add_transition(Transition(waiting_r, lambda x: x[:2] != 'r ', die('Expected "r " while parsing routers not "%s"')))

waiting_s.add_transition(Transition(waiting_w, lambda x: x[:2] == 's ', self._router_flags))
waiting_s.add_transition(Transition(waiting_r, lambda x: x[:2] != 's ', die('Expected "s " while parsing routers not "%s"')))
waiting_s.add_transition(Transition(waiting_r, lambda x: x.strip() == '.', nothing))

waiting_w.add_transition(Transition(waiting_p, lambda x: x[:2] == 'w ', self._router_bandwidth))
waiting_w.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin)) # "w" lines are optional
waiting_w.add_transition(Transition(waiting_r, lambda x: x[:2] != 'w ', die('Expected "w " while parsing routers not "%s"')))
waiting_w.add_transition(Transition(waiting_r, lambda x: x.strip() == '.', nothing))

waiting_p.add_transition(Transition(waiting_r, lambda x: x[:2] == 'p ', self._router_policy))
waiting_p.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin)) # "p" lines are optional
waiting_p.add_transition(Transition(waiting_r, lambda x: x[:2] != 'p ', die('Expected "p " while parsing routers not "%s"')))
waiting_p.add_transition(Transition(waiting_r, lambda x: x.strip() == '.', nothing))

self._network_status_parser = FSM([waiting_r, waiting_s, waiting_w, waiting_p])
if write_state_diagram:
with open('routerfsm.dot', 'w') as fsmfile:``````
meejah / txtorcon / txtorcon / torcontrolprotocol.py View on Github
``````## simply, confounded by the fact that 600's (notify) can come
## at any time AND can be multi-line itself. Luckily, these
## can't be nested, nor can the responses be interleaved.

idle = State("IDLE")
recv = State("RECV")
recvmulti = State("RECV_PLUS")
recvnotify = State("NOTIFY_MULTILINE")

self._is_single_line_response,
self._is_multi_line,
self._start_command))
self._is_continuation_line,
self._start_command))

self._is_continuation_line,
self._accumulate_response))
self._is_finish_line,

self._is_end_line,
lambda x: None))
self._is_not_end_line,
self._accumulate_multi_response))``````
meejah / txtorcon / txtorcon / torstate.py View on Github
``````waiting_r.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin))
## FIXME use better method/func than die!!
waiting_r.add_transition(Transition(waiting_r, lambda x: x[:2] != 'r ', die('Expected "r " while parsing routers not "%s"')))

waiting_s.add_transition(Transition(waiting_w, lambda x: x[:2] == 's ', self._router_flags))
waiting_s.add_transition(Transition(waiting_r, lambda x: x[:2] != 's ', die('Expected "s " while parsing routers not "%s"')))
waiting_s.add_transition(Transition(waiting_r, lambda x: x.strip() == '.', nothing))

waiting_w.add_transition(Transition(waiting_p, lambda x: x[:2] == 'w ', self._router_bandwidth))
waiting_w.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin)) # "w" lines are optional
waiting_w.add_transition(Transition(waiting_r, lambda x: x[:2] != 'w ', die('Expected "w " while parsing routers not "%s"')))
waiting_w.add_transition(Transition(waiting_r, lambda x: x.strip() == '.', nothing))

waiting_p.add_transition(Transition(waiting_r, lambda x: x[:2] == 'p ', self._router_policy))
waiting_p.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin)) # "p" lines are optional
waiting_p.add_transition(Transition(waiting_r, lambda x: x[:2] != 'p ', die('Expected "p " while parsing routers not "%s"')))
waiting_p.add_transition(Transition(waiting_r, lambda x: x.strip() == '.', nothing))

self._network_status_parser = FSM([waiting_r, waiting_s, waiting_w, waiting_p])
if write_state_diagram:
with open('routerfsm.dot', 'w') as fsmfile:
fsmfile.write(self._network_status_parser.dotty())

self.post_bootstrap = defer.Deferred()
if bootstrap:
if self.protocol.post_bootstrap:
else:
self._bootstrap()``````
meejah / txtorcon / txtorcon / torcontrolprotocol.py View on Github
``````recvnotify = State("NOTIFY_MULTILINE")

self._is_single_line_response,
self._is_multi_line,
self._start_command))
self._is_continuation_line,
self._start_command))

self._is_continuation_line,
self._accumulate_response))
self._is_finish_line,

self._is_end_line,
lambda x: None))
self._is_not_end_line,
self._accumulate_multi_response))

self.fsm = FSM([recvnotify, idle, recvmulti, recv])
self.state_idle = idle
## hand-set initial state default start state is first in the
## list; the above looks nice in dotty though
self.fsm.state = idle
if DEBUG:``````
meejah / txtorcon / txtorcon / torstate.py View on Github
``````eat_line.add_transition(Transition(waiting_r, lambda x: True, nothing))

waiting_r.add_transition(Transition(eat_line, lambda x: x.strip() == '.' or x.strip() == 'OK', nothing))
waiting_r.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin))
## FIXME use better method/func than die!!
waiting_r.add_transition(Transition(eat_line, lambda x: x[:2] != 'r ', die('Expected "r " while parsing routers not "%s"')))

waiting_s.add_transition(Transition(waiting_w, lambda x: x[:2] == 's ', self._router_flags))
waiting_s.add_transition(Transition(eat_line, lambda x: x[:2] != 's ', die('Expected "s " while parsing routers not "%s"')))
waiting_s.add_transition(Transition(eat_line, lambda x: x.strip() == '.', nothing))

waiting_w.add_transition(Transition(waiting_p, lambda x: x[:2] == 'w ', self._router_bandwidth))
waiting_w.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin)) # "w" lines are optional
waiting_w.add_transition(Transition(eat_line, lambda x: x[:2] != 'w ', die('Expected "w " while parsing routers not "%s"')))
waiting_w.add_transition(Transition(eat_line, lambda x: x.strip() == '.', nothing))

waiting_p.add_transition(Transition(waiting_r, lambda x: x[:2] == 'p ', self._router_policy))
waiting_p.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin)) # "p" lines are optional
waiting_p.add_transition(Transition(eat_line, lambda x: x[:2] != 'p ', die('Expected "p " while parsing routers not "%s"')))
waiting_p.add_transition(Transition(eat_line, lambda x: x.strip() == '.', nothing))

self._network_status_parser = FSM([eat_line, waiting_r, waiting_s, waiting_w, waiting_p])

self.post_bootstrap = defer.Deferred()
if bootstrap:
if self.protocol.post_bootstrap:
else:
self._bootstrap()``````
meejah / txtorcon / txtorcon / torstate.py View on Github
``````def __call__(self, *args):
raise RuntimeError(self.msg % tuple(args))

def nothing(*args):
pass

waiting_r = State("waiting_r")
waiting_w = State("waiting_w")
waiting_p = State("waiting_p")
waiting_s = State("waiting_s")

def ignorable_line(x):
return x.strip() == '.' or x.strip() == 'OK' or x[:3] == 'ns/' or x.strip() == ''

waiting_r.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin))
## FIXME use better method/func than die!!
waiting_r.add_transition(Transition(waiting_r, lambda x: x[:2] != 'r ', die('Expected "r " while parsing routers not "%s"')))

waiting_s.add_transition(Transition(waiting_w, lambda x: x[:2] == 's ', self._router_flags))
waiting_s.add_transition(Transition(waiting_r, lambda x: x[:2] != 's ', die('Expected "s " while parsing routers not "%s"')))
waiting_s.add_transition(Transition(waiting_r, lambda x: x.strip() == '.', nothing))

waiting_w.add_transition(Transition(waiting_p, lambda x: x[:2] == 'w ', self._router_bandwidth))
waiting_w.add_transition(Transition(waiting_s, lambda x: x[:2] == 'r ', self._router_begin)) # "w" lines are optional
waiting_w.add_transition(Transition(waiting_r, lambda x: x[:2] != 'w ', die('Expected "w " while parsing routers not "%s"')))
waiting_w.add_transition(Transition(waiting_r, lambda x: x.strip() == '.', nothing))

waiting_p.add_transition(Transition(waiting_r, lambda x: x[:2] == 'p ', self._router_policy))

spaghetti

Analysis of Network-constrained Spatial Data

BSD-3-Clause