Example #1
0
 def _generate_banner(self):
     """Generates a banner that can be useful to display before running."""
     try:
         tpl_params = dict(self.BANNER_TEMPLATE.defaults)
     except AttributeError:
         tpl_params = {}
     connection_details = self._server.connection_details
     transport = connection_details.transport
     if transport.driver_version:
         transport_driver = "%s v%s" % (transport.driver_name,
                                        transport.driver_version)
     else:
         transport_driver = transport.driver_name
     tpl_params['transport_driver'] = transport_driver
     tpl_params['exchange'] = self._exchange
     tpl_params['topic'] = self._topic
     tpl_params['transport_type'] = transport.driver_type
     tpl_params['connection_uri'] = connection_details.uri
     tpl_params['executor_type'] = reflection.get_class_name(self._executor)
     threads_count = getattr(self._executor, 'max_workers', None)
     if threads_count is not None:
         tpl_params['executor_thread_count'] = threads_count
     if self._endpoints:
         pretty_endpoints = []
         for ep in self._endpoints:
             pretty_endpoints.append("  - %s" % ep)
         # This ensures there is a newline before the list...
         tpl_params['endpoints'] = "\n" + "\n".join(pretty_endpoints)
     try:
         tpl_params['hostname'] = socket.getfqdn()
     except socket.error:
         pass
     try:
         tpl_params['pid'] = os.getpid()
     except OSError:
         pass
     tpl_params['platform'] = platform.platform()
     tpl_params['thread_id'] = tu.get_ident()
     banner = self.BANNER_TEMPLATE.substitute(**tpl_params)
     # NOTE(harlowja): this is needed since the template in this file
     # will always have newlines that end with '\n' (even on different
     # platforms due to the way this source file is encoded) so we have
     # to do this little dance to make it platform neutral...
     return misc.fix_newlines(banner)
Example #2
0
 def _generate_banner(self):
     """Generates a banner that can be useful to display before running."""
     try:
         tpl_params = dict(self.BANNER_TEMPLATE.defaults)
     except AttributeError:
         tpl_params = {}
     connection_details = self._server.connection_details
     transport = connection_details.transport
     if transport.driver_version:
         transport_driver = "%s v%s" % (transport.driver_name,
                                        transport.driver_version)
     else:
         transport_driver = transport.driver_name
     tpl_params['transport_driver'] = transport_driver
     tpl_params['exchange'] = self._exchange
     tpl_params['topic'] = self._topic
     tpl_params['transport_type'] = transport.driver_type
     tpl_params['connection_uri'] = connection_details.uri
     tpl_params['executor_type'] = reflection.get_class_name(self._executor)
     threads_count = getattr(self._executor, 'max_workers', None)
     if threads_count is not None:
         tpl_params['executor_thread_count'] = threads_count
     if self._endpoints:
         pretty_endpoints = []
         for ep in self._endpoints:
             pretty_endpoints.append("  - %s" % ep)
         # This ensures there is a newline before the list...
         tpl_params['endpoints'] = "\n" + "\n".join(pretty_endpoints)
     try:
         tpl_params['hostname'] = socket.getfqdn()
     except socket.error:
         pass
     try:
         tpl_params['pid'] = os.getpid()
     except OSError:
         pass
     tpl_params['platform'] = platform.platform()
     tpl_params['thread_id'] = tu.get_ident()
     banner = self.BANNER_TEMPLATE.substitute(**tpl_params)
     # NOTE(harlowja): this is needed since the template in this file
     # will always have newlines that end with '\n' (even on different
     # platforms due to the way this source file is encoded) so we have
     # to do this little dance to make it platform neutral...
     return misc.fix_newlines(banner)
Example #3
0
def make_banner(what, chapters):
    """Makes a taskflow banner string.

    For example::

      >>> from taskflow.utils import banner
      >>> chapters = {
          'Connection details': {
              'Topic': 'hello',
          },
          'Powered by': {
              'Executor': 'parallel',
          },
      }
      >>> print(banner.make_banner('Worker', chapters))

    This will output::

      ___    __
       |    |_
       |ask |low v1.26.1
      *Worker*
      Connection details:
        Topic => hello
      Powered by:
        Executor => parallel
    """
    buf = misc.StringIO()
    buf.write_nl(BANNER_HEADER)
    if chapters:
        buf.write_nl("*%s*" % what)
        chapter_names = sorted(six.iterkeys(chapters))
    else:
        buf.write("*%s*" % what)
        chapter_names = []
    for i, chapter_name in enumerate(chapter_names):
        chapter_contents = chapters[chapter_name]
        if chapter_contents:
            buf.write_nl("%s:" % (chapter_name))
        else:
            buf.write("%s:" % (chapter_name))
        if isinstance(chapter_contents, dict):
            section_names = sorted(six.iterkeys(chapter_contents))
            for j, section_name in enumerate(section_names):
                if j + 1 < len(section_names):
                    buf.write_nl("  %s => %s"
                                 % (section_name,
                                    chapter_contents[section_name]))
                else:
                    buf.write("  %s => %s" % (section_name,
                                              chapter_contents[section_name]))
        elif isinstance(chapter_contents, (list, tuple, set)):
            if isinstance(chapter_contents, set):
                sections = sorted(chapter_contents)
            else:
                sections = chapter_contents
            for j, section in enumerate(sections):
                if j + 1 < len(sections):
                    buf.write_nl("  %s. %s" % (j + 1, section))
                else:
                    buf.write("  %s. %s" % (j + 1, section))
        else:
            raise TypeError("Unsupported chapter contents"
                            " type: one of dict, list, tuple, set expected"
                            " and not %s" % type(chapter_contents).__name__)
        if i + 1 < len(chapter_names):
            buf.write_nl("")
    # NOTE(harlowja): this is needed since the template in this file
    # will always have newlines that end with '\n' (even on different
    # platforms due to the way this source file is encoded) so we have
    # to do this little dance to make it platform neutral...
    if os.linesep != "\n":
        return misc.fix_newlines(buf.getvalue())
    return buf.getvalue()
Example #4
0
def make_banner(what, chapters):
    """Makes a taskflow banner string.

    For example::

      >>> from taskflow.utils import banner
      >>> chapters = {
          'Connection details': {
              'Topic': 'hello',
          },
          'Powered by': {
              'Executor': 'parallel',
          },
      }
      >>> print(banner.make_banner('Worker', chapters))

    This will output::

      ___    __
       |    |_
       |ask |low v1.26.1
      *Worker*
      Connection details:
        Topic => hello
      Powered by:
        Executor => parallel
    """
    buf = misc.StringIO()
    buf.write_nl(BANNER_HEADER)
    if chapters:
        buf.write_nl("*%s*" % what)
        chapter_names = sorted(six.iterkeys(chapters))
    else:
        buf.write("*%s*" % what)
        chapter_names = []
    for i, chapter_name in enumerate(chapter_names):
        chapter_contents = chapters[chapter_name]
        if chapter_contents:
            buf.write_nl("%s:" % (chapter_name))
        else:
            buf.write("%s:" % (chapter_name))
        if isinstance(chapter_contents, dict):
            section_names = sorted(six.iterkeys(chapter_contents))
            for j, section_name in enumerate(section_names):
                if j + 1 < len(section_names):
                    buf.write_nl(
                        "  %s => %s" %
                        (section_name, chapter_contents[section_name]))
                else:
                    buf.write("  %s => %s" %
                              (section_name, chapter_contents[section_name]))
        elif isinstance(chapter_contents, (list, tuple, set)):
            if isinstance(chapter_contents, set):
                sections = sorted(chapter_contents)
            else:
                sections = chapter_contents
            for j, section in enumerate(sections):
                if j + 1 < len(sections):
                    buf.write_nl("  %s. %s" % (j + 1, section))
                else:
                    buf.write("  %s. %s" % (j + 1, section))
        else:
            raise TypeError("Unsupported chapter contents"
                            " type: one of dict, list, tuple, set expected"
                            " and not %s" % type(chapter_contents).__name__)
        if i + 1 < len(chapter_names):
            buf.write_nl("")
    # NOTE(harlowja): this is needed since the template in this file
    # will always have newlines that end with '\n' (even on different
    # platforms due to the way this source file is encoded) so we have
    # to do this little dance to make it platform neutral...
    if os.linesep != "\n":
        return misc.fix_newlines(buf.getvalue())
    return buf.getvalue()