~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/howto/listings/TwistedQuotes/quotetap.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from twisted.application import internet # services that run TCP/SSL/etc.
 
2
from TwistedQuotes import quoteproto    # Protocol and Factory
 
3
from TwistedQuotes import quoters       # "give me a quote" code
 
4
 
 
5
from twisted.python import usage        # twisted command-line processing
 
6
 
 
7
 
 
8
class Options(usage.Options):
 
9
    optParameters = [["port", "p", 8007,
 
10
                      "Port number to listen on for QOTD protocol."],
 
11
                     ["static", "s", "An apple a day keeps the doctor away.",
 
12
                      "A static quote to display."],
 
13
                     ["file", "f", None,
 
14
                      "A fortune-format text file to read quotes from."]]
 
15
 
 
16
 
 
17
def makeService(config):
 
18
    """Return a service that will be attached to the application."""
 
19
    if config["file"]:                  # If I was given a "file" option...
 
20
        # Read quotes from a file, selecting a random one each time,
 
21
        quoter = quoters.FortuneQuoter([config['file']]) 
 
22
    else:                               # otherwise,
 
23
        # read a single quote from the command line (or use the default).
 
24
        quoter = quoters.StaticQuoter(config['static']) 
 
25
    port = int(config["port"])          # TCP port to listen on
 
26
    factory = quoteproto.QOTDFactory(quoter) # here we create a QOTDFactory
 
27
    # Finally, set up our factory, with its custom quoter, to create QOTD
 
28
    # protocol instances when events arrive on the specified port.
 
29
    return internet.TCPServer(port, factory)