Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
See the helper method :func:`txtorcon.build_tor_connection`.
"""
## variables related to the state machine
self.defer = None # Deferred we returned for the current command
self.response = ''
self.code = None
self.command = None # currently processing this command
self.commands = [] # queued commands
## Here we build up the state machine. Mostly it's pretty
## 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")
idle.add_transition(Transition(idle,
self._is_single_line_response,
self._broadcast_response))
idle.add_transition(Transition(recvmulti,
self._is_multi_line,
self._start_command))
idle.add_transition(Transition(recv,
self._is_continuation_line,
self._start_command))
recv.add_transition(Transition(recv,
self._is_continuation_line,
self.cleanup = None # see set_attacher
class die(object):
def __init__(self, msg):
self.msg = msg
def __call__(self, *args):
raise RuntimeError(self.msg%tuple(args))
def nothing(*args):
pass
eat_line = State("eat_line")
waiting_r = State("waiting_r")
waiting_w = State("waiting_r")
waiting_p = State("waiting_r")
waiting_s = State("waiting_r")
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))
## variables related to the state machine
self.defer = None # Deferred we returned for the current command
self.response = ''
self.code = None
self.command = None # currently processing this command
self.commands = [] # queued commands
## Here we build up the state machine. Mostly it's pretty
## 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")
idle.add_transition(Transition(idle,
self._is_single_line_response,
self._broadcast_response))
idle.add_transition(Transition(recvmulti,
self._is_multi_line,
self._start_command))
idle.add_transition(Transition(recv,
self._is_continuation_line,
self._start_command))
recv.add_transition(Transition(recv,
self._is_continuation_line,
self._accumulate_response))
recv.add_transition(Transition(idle,
self._is_finish_line,
self.authorities = {} # keys by name
self.cleanup = None # see set_attacher
class die(object):
def __init__(self, msg):
self.msg = msg
def __call__(self, *args):
raise RuntimeError(self.msg%tuple(args))
def nothing(*args):
pass
eat_line = State("eat_line")
waiting_r = State("waiting_r")
waiting_w = State("waiting_r")
waiting_p = State("waiting_r")
waiting_s = State("waiting_r")
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
"""
## variables related to the state machine
self.defer = None # Deferred we returned for the current command
self.response = ''
self.code = None
self.command = None # currently processing this command
self.commands = [] # queued commands
## Here we build up the state machine. Mostly it's pretty
## 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")
idle.add_transition(Transition(idle,
self._is_single_line_response,
self._broadcast_response))
idle.add_transition(Transition(recvmulti,
self._is_multi_line,
self._start_command))
idle.add_transition(Transition(recv,
self._is_continuation_line,
self._start_command))
recv.add_transition(Transition(recv,
self._is_continuation_line,
self._accumulate_response))
self.entry_guards = {} # from GETINFO entry-guards, our current entry guards
self.unusable_entry_guards = [] # list of entry guards we didn't parse out
self.authorities = {} # keys by name
self.cleanup = None # see set_attacher
class die(object):
def __init__(self, msg):
self.msg = msg
def __call__(self, *args):
raise RuntimeError(self.msg%tuple(args))
def nothing(*args):
pass
eat_line = State("eat_line")
waiting_r = State("waiting_r")
waiting_w = State("waiting_r")
waiting_p = State("waiting_r")
waiting_s = State("waiting_r")
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))
class die(object):
__name__ = 'die' # FIXME? just to ease spagetti.py:82's pain
def __init__(self, msg):
self.msg = msg
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_r, ignorable_line, 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(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, ignorable_line, nothing))
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_r, ignorable_line, nothing))
self.cleanup = None # see set_attacher
class die(object):
def __init__(self, msg):
self.msg = msg
def __call__(self, *args):
raise RuntimeError(self.msg%tuple(args))
def nothing(*args):
pass
eat_line = State("eat_line")
waiting_r = State("waiting_r")
waiting_w = State("waiting_r")
waiting_p = State("waiting_r")
waiting_s = State("waiting_r")
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"')))
self.cleanup = None # see set_attacher
class die(object):
__name__ = 'die' # FIXME? just to ease spagetti.py:82's pain
def __init__(self, msg):
self.msg = msg
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_r, ignorable_line, 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(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, ignorable_line, nothing))
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))
self.cleanup = None # see set_attacher
class die(object):
__name__ = 'die' # FIXME? just to ease spagetti.py:82's pain
def __init__(self, msg):
self.msg = msg
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_r, ignorable_line, 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(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, ignorable_line, nothing))
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))