|
23 | 23 | from traitlets.config import LoggingConfigurable
|
24 | 24 | from .localinterfaces import localhost
|
25 | 25 | from ipython_genutils.path import filefind
|
26 |
| -from ipython_genutils.py3compat import (str_to_bytes, bytes_to_str, cast_bytes_py2, |
27 |
| - string_types) |
| 26 | +from ipython_genutils.py3compat import ( |
| 27 | + bytes_to_str, cast_bytes, cast_bytes_py2, string_types, |
| 28 | +) |
28 | 29 | from traitlets import (
|
29 | 30 | Bool, Integer, Unicode, CaselessStrEnum, Instance, Type,
|
30 | 31 | )
|
@@ -411,23 +412,46 @@ def write_connection_file(self):
|
411 | 412 |
|
412 | 413 | self._connection_file_written = True
|
413 | 414 |
|
414 |
| - def load_connection_file(self): |
415 |
| - """Load connection info from JSON dict in self.connection_file.""" |
416 |
| - self.log.debug(u"Loading connection file %s", self.connection_file) |
417 |
| - with open(self.connection_file) as f: |
418 |
| - cfg = json.load(f) |
419 |
| - self.transport = cfg.get('transport', self.transport) |
420 |
| - self.ip = cfg.get('ip', self._ip_default()) |
| 415 | + def load_connection_file(self, connection_file=None): |
| 416 | + """Load connection info from JSON dict in self.connection_file. |
| 417 | + |
| 418 | + Parameters |
| 419 | + ---------- |
| 420 | + connection_file: unicode, optional |
| 421 | + Path to connection file to load. |
| 422 | + If unspecified, use self.connection_file |
| 423 | + """ |
| 424 | + if connection_file is None: |
| 425 | + connection_file = self.connection_file |
| 426 | + self.log.debug(u"Loading connection file %s", connection_file) |
| 427 | + with open(connection_file) as f: |
| 428 | + info = json.load(f) |
| 429 | + self.load_connection_info(info) |
| 430 | + |
| 431 | + def load_connection_info(self, info): |
| 432 | + """Load connection info from a dict containing connection info. |
| 433 | + |
| 434 | + Typically this data comes from a connection file |
| 435 | + and is called by load_connection_file. |
| 436 | + |
| 437 | + Parameters |
| 438 | + ---------- |
| 439 | + info: dict |
| 440 | + Dictionary containing connection_info. |
| 441 | + See the connection_file spec for details. |
| 442 | + """ |
| 443 | + self.transport = info.get('transport', self.transport) |
| 444 | + self.ip = info.get('ip', self._ip_default()) |
421 | 445 |
|
422 | 446 | for name in port_names:
|
423 |
| - if getattr(self, name) == 0 and name in cfg: |
| 447 | + if getattr(self, name) == 0 and name in info: |
424 | 448 | # not overridden by config or cl_args
|
425 |
| - setattr(self, name, cfg[name]) |
| 449 | + setattr(self, name, info[name]) |
426 | 450 |
|
427 |
| - if 'key' in cfg: |
428 |
| - self.session.key = str_to_bytes(cfg['key']) |
429 |
| - if 'signature_scheme' in cfg: |
430 |
| - self.session.signature_scheme = cfg['signature_scheme'] |
| 451 | + if 'key' in info: |
| 452 | + self.session.key = cast_bytes(info['key']) |
| 453 | + if 'signature_scheme' in info: |
| 454 | + self.session.signature_scheme = info['signature_scheme'] |
431 | 455 |
|
432 | 456 | #--------------------------------------------------------------------------
|
433 | 457 | # Creating connected sockets
|
|
0 commit comments