Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / words / tap.py
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
8 import sys, socket
9
10 from twisted.application import strports
11 from twisted.application.service import MultiService
12 from twisted.python import usage
13 from twisted import plugin
14
15 from twisted.words import iwords, service
16 from twisted.cred import checkers, credentials, portal, strcred
17
18 class Options(usage.Options, strcred.AuthOptionMixin):
19     supportedInterfaces = [credentials.IUsernamePassword]
20     optParameters = [
21         ('hostname', None, socket.gethostname(),
22          'Name of this server; purely an informative')]
23
24     compData = usage.Completions(multiUse=["group"])
25
26     interfacePlugins = {}
27     plg = None
28     for plg in plugin.getPlugins(iwords.IProtocolPlugin):
29         assert plg.name not in interfacePlugins
30         interfacePlugins[plg.name] = plg
31         optParameters.append((
32             plg.name + '-port',
33             None, None,
34             'strports description of the port to bind for the  ' + plg.name + ' server'))
35     del plg
36
37     def __init__(self, *a, **kw):
38         usage.Options.__init__(self, *a, **kw)
39         self['groups'] = []
40
41     def opt_group(self, name):
42         """Specify a group which should exist
43         """
44         self['groups'].append(name.decode(sys.stdin.encoding))
45
46     def opt_passwd(self, filename):
47         """
48         Name of a passwd-style file. (This is for
49         backwards-compatibility only; you should use the --auth
50         command instead.)
51         """
52         self.addChecker(checkers.FilePasswordDB(filename))
53
54 def makeService(config):
55     credCheckers = config.get('credCheckers', [])
56     wordsRealm = service.InMemoryWordsRealm(config['hostname'])
57     wordsPortal = portal.Portal(wordsRealm, credCheckers)
58
59     msvc = MultiService()
60
61     # XXX Attribute lookup on config is kind of bad - hrm.
62     for plgName in config.interfacePlugins:
63         port = config.get(plgName + '-port')
64         if port is not None:
65             factory = config.interfacePlugins[plgName].getFactory(wordsRealm, wordsPortal)
66             svc = strports.service(port, factory)
67             svc.setServiceParent(msvc)
68
69     # This is bogus.  createGroup is async.  makeService must be
70     # allowed to return a Deferred or some crap.
71     for g in config['groups']:
72         wordsRealm.createGroup(g)
73
74     return msvc