parent: reduce cutpaste

Unclear whether exec() is better or worse than __import__(), but at
least the semantics are correct.
This commit is contained in:
David Wilson 2018-04-11 02:02:26 +01:00
parent 38c0ad1eea
commit e5b784ed32
1 changed files with 13 additions and 29 deletions

View File

@ -371,41 +371,25 @@ def make_call_msg(fn, *args, **kwargs):
) )
def _docker_method(): def stream_by_method_name(name):
import mitogen.docker """
return mitogen.docker.Stream Given the name of a Mitogen connection method, import its implementation
module and return its Stream subclass.
def _fork_method(): """
import mitogen.fork if name == 'local':
return mitogen.fork.Stream name = 'parent'
Stream = None
def _local_method(): exec('from mitogen.%s import Stream' % (name,))
return mitogen.parent.Stream return Stream
def _ssh_method():
import mitogen.ssh
return mitogen.ssh.Stream
def _sudo_method():
import mitogen.sudo
return mitogen.sudo.Stream
METHOD_NAMES = {
'docker': _docker_method,
'fork': _fork_method,
'local': _local_method,
'ssh': _ssh_method,
'sudo': _sudo_method,
}
@mitogen.core.takes_econtext @mitogen.core.takes_econtext
def _proxy_connect(name, method_name, kwargs, econtext): def _proxy_connect(name, method_name, kwargs, econtext):
mitogen.parent.upgrade_router(econtext) mitogen.parent.upgrade_router(econtext)
try: try:
context = econtext.router._connect( context = econtext.router._connect(
klass=METHOD_NAMES[method_name](), klass=stream_by_method_name(method_name),
name=name, name=name,
**kwargs **kwargs
) )
@ -834,7 +818,7 @@ class Router(mitogen.core.Router):
return context return context
def connect(self, method_name, name=None, **kwargs): def connect(self, method_name, name=None, **kwargs):
klass = METHOD_NAMES[method_name]() klass = stream_by_method_name(method_name)
kwargs.setdefault('debug', self.debug) kwargs.setdefault('debug', self.debug)
kwargs.setdefault('profiling', self.profiling) kwargs.setdefault('profiling', self.profiling)