tap.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- test-case-name: twisted.words.test.test_tap -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Shiny new words service maker
  6. """
  7. import sys, socket
  8. from twisted.application import strports
  9. from twisted.application.service import MultiService
  10. from twisted.python import usage
  11. from twisted import plugin
  12. from twisted.words import iwords, service
  13. from twisted.cred import checkers, credentials, portal, strcred
  14. class Options(usage.Options, strcred.AuthOptionMixin):
  15. supportedInterfaces = [credentials.IUsernamePassword]
  16. optParameters = [
  17. ('hostname', None, socket.gethostname(),
  18. 'Name of this server; purely an informative')]
  19. compData = usage.Completions(multiUse=["group"])
  20. interfacePlugins = {}
  21. plg = None
  22. for plg in plugin.getPlugins(iwords.IProtocolPlugin):
  23. assert plg.name not in interfacePlugins
  24. interfacePlugins[plg.name] = plg
  25. optParameters.append((
  26. plg.name + '-port',
  27. None, None,
  28. 'strports description of the port to bind for the ' + plg.name + ' server'))
  29. del plg
  30. def __init__(self, *a, **kw):
  31. usage.Options.__init__(self, *a, **kw)
  32. self['groups'] = []
  33. def opt_group(self, name):
  34. """Specify a group which should exist
  35. """
  36. self['groups'].append(name.decode(sys.stdin.encoding))
  37. def opt_passwd(self, filename):
  38. """
  39. Name of a passwd-style file. (This is for
  40. backwards-compatibility only; you should use the --auth
  41. command instead.)
  42. """
  43. self.addChecker(checkers.FilePasswordDB(filename))
  44. def makeService(config):
  45. credCheckers = config.get('credCheckers', [])
  46. wordsRealm = service.InMemoryWordsRealm(config['hostname'])
  47. wordsPortal = portal.Portal(wordsRealm, credCheckers)
  48. msvc = MultiService()
  49. # XXX Attribute lookup on config is kind of bad - hrm.
  50. for plgName in config.interfacePlugins:
  51. port = config.get(plgName + '-port')
  52. if port is not None:
  53. factory = config.interfacePlugins[plgName].getFactory(wordsRealm, wordsPortal)
  54. svc = strports.service(port, factory)
  55. svc.setServiceParent(msvc)
  56. # This is bogus. createGroup is async. makeService must be
  57. # allowed to return a Deferred or some crap.
  58. for g in config['groups']:
  59. wordsRealm.createGroup(g)
  60. return msvc