/ipyparallel/factory.py

https://github.com/ipython/ipyparallel
Python | 73 lines | 42 code | 11 blank | 20 comment | 3 complexity | b959d2705dfd66bfff504ff2cfabb9fe MD5 | raw file
  1. """Base config factories.
  2. Authors:
  3. * Min RK
  4. """
  5. #-----------------------------------------------------------------------------
  6. # Copyright (C) 2010-2011 The IPython Development Team
  7. #
  8. # Distributed under the terms of the BSD License. The full license is in
  9. # the file COPYING, distributed as part of this software.
  10. #-----------------------------------------------------------------------------
  11. #-----------------------------------------------------------------------------
  12. # Imports
  13. #-----------------------------------------------------------------------------
  14. from jupyter_client.localinterfaces import localhost
  15. from traitlets import Integer, Unicode
  16. from ipyparallel.util import select_random_ports
  17. from jupyter_client.session import SessionFactory
  18. #-----------------------------------------------------------------------------
  19. # Classes
  20. #-----------------------------------------------------------------------------
  21. class RegistrationFactory(SessionFactory):
  22. """The Base Configurable for objects that involve registration."""
  23. url = Unicode('', config=True,
  24. help="""The 0MQ url used for registration. This sets transport, ip, and port
  25. in one variable. For example: url='tcp://127.0.0.1:12345' or
  26. url='epgm://*:90210'"""
  27. ) # url takes precedence over ip,regport,transport
  28. transport = Unicode('tcp', config=True,
  29. help="""The 0MQ transport for communications. This will likely be
  30. the default of 'tcp', but other values include 'ipc', 'epgm', 'inproc'.""")
  31. ip = Unicode(config=True,
  32. help="""The IP address for registration. This is generally either
  33. '127.0.0.1' for loopback only or '*' for all interfaces.
  34. """)
  35. def _ip_default(self):
  36. return localhost()
  37. regport = Integer(config=True,
  38. help="""The port on which the Hub listens for registration.""")
  39. def _regport_default(self):
  40. return select_random_ports(1)[0]
  41. def __init__(self, **kwargs):
  42. super(RegistrationFactory, self).__init__(**kwargs)
  43. self._propagate_url()
  44. self._rebuild_url()
  45. self.on_trait_change(self._propagate_url, 'url')
  46. self.on_trait_change(self._rebuild_url, 'ip')
  47. self.on_trait_change(self._rebuild_url, 'transport')
  48. self.on_trait_change(self._rebuild_url, 'regport')
  49. def _rebuild_url(self):
  50. self.url = "%s://%s:%i"%(self.transport, self.ip, self.regport)
  51. def _propagate_url(self):
  52. """Ensure self.url contains full transport://interface:port"""
  53. if self.url:
  54. iface = self.url.split('://',1)
  55. if len(iface) == 2:
  56. self.transport,iface = iface
  57. iface = iface.split(':')
  58. self.ip = iface[0]
  59. if iface[1]:
  60. self.regport = int(iface[1])