_parser.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- test-case-name: twisted.protocols.haproxy.test.test_parser -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Parser for 'haproxy:' string endpoint.
  6. """
  7. from zope.interface import implementer
  8. from twisted.plugin import IPlugin
  9. from twisted.internet.endpoints import (
  10. quoteStringArgument, serverFromString, IStreamServerEndpointStringParser
  11. )
  12. from twisted.python.compat import iteritems
  13. from . import proxyEndpoint
  14. def unparseEndpoint(args, kwargs):
  15. """
  16. Un-parse the already-parsed args and kwargs back into endpoint syntax.
  17. @param args: C{:}-separated arguments
  18. @type args: L{tuple} of native L{str}
  19. @param kwargs: C{:} and then C{=}-separated keyword arguments
  20. @type arguments: L{tuple} of native L{str}
  21. @return: a string equivalent to the original format which this was parsed
  22. as.
  23. @rtype: native L{str}
  24. """
  25. description = ':'.join(
  26. [quoteStringArgument(str(arg)) for arg in args] +
  27. sorted(['%s=%s' % (quoteStringArgument(str(key)),
  28. quoteStringArgument(str(value)))
  29. for key, value in iteritems(kwargs)
  30. ]))
  31. return description
  32. @implementer(IPlugin, IStreamServerEndpointStringParser)
  33. class HAProxyServerParser(object):
  34. """
  35. Stream server endpoint string parser for the HAProxyServerEndpoint type.
  36. @ivar prefix: See L{IStreamServerEndpointStringParser.prefix}.
  37. """
  38. prefix = "haproxy"
  39. def parseStreamServer(self, reactor, *args, **kwargs):
  40. """
  41. Parse a stream server endpoint from a reactor and string-only arguments
  42. and keyword arguments.
  43. @param reactor: The reactor.
  44. @param args: The parsed string arguments.
  45. @param kwargs: The parsed keyword arguments.
  46. @return: a stream server endpoint
  47. @rtype: L{IStreamServerEndpoint}
  48. """
  49. subdescription = unparseEndpoint(args, kwargs)
  50. wrappedEndpoint = serverFromString(reactor, subdescription)
  51. return proxyEndpoint(wrappedEndpoint)