~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/news/tap.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
 
 
5
from twisted.news import news, database
 
6
from twisted.application import strports
 
7
from twisted.python import usage, log
 
8
 
 
9
class DBOptions(usage.Options):
 
10
    optParameters = [
 
11
        ['module',   None, 'pyPgSQL.PgSQL', "DB-API 2.0 module to use"],
 
12
        ['dbhost',   None, 'localhost',     "Host where database manager is listening"],
 
13
        ['dbuser',   None, 'news',          "Username with which to connect to database"],
 
14
        ['database', None, 'news',          "Database name to use"],
 
15
        ['schema',   None, 'schema.sql',    "File to which to write SQL schema initialisation"],
 
16
 
 
17
        # XXX - Hrm.
 
18
        ["groups",     "g", "groups.list",   "File containing group list"],
 
19
        ["servers",    "s", "servers.list",  "File containing server list"]
 
20
    ]
 
21
    
 
22
    def postOptions(self):
 
23
        # XXX - Hmmm.
 
24
        self['groups'] = [g.strip() for g in open(self['groups']).readlines() if not g.startswith('#')]
 
25
        self['servers'] = [s.strip() for s in open(self['servers']).readlines() if not s.startswith('#')]
 
26
 
 
27
        try:
 
28
            __import__(self['module'])
 
29
        except ImportError:
 
30
            log.msg("Warning: Cannot import %s" % (self['module'],))
 
31
        
 
32
        open(self['schema'], 'w').write(
 
33
            database.NewsStorageAugmentation.schema + '\n' +
 
34
            database.makeGroupSQL(self['groups']) + '\n' +
 
35
            database.makeOverviewSQL()
 
36
        )
 
37
        
 
38
        info = {
 
39
            'host': self['dbhost'], 'user': self['dbuser'],
 
40
            'database': self['database'], 'dbapiName': self['module']
 
41
        }
 
42
        self.db = database.NewsStorageAugmentation(info)
 
43
 
 
44
 
 
45
class PickleOptions(usage.Options):
 
46
    optParameters = [
 
47
        ['file', None, 'news.pickle', "File to which to save pickle"],
 
48
 
 
49
        # XXX - Hrm.
 
50
        ["groups",     "g", "groups.list",   "File containing group list"],
 
51
        ["servers",    "s", "servers.list",  "File containing server list"],
 
52
        ["moderators", "m", "moderators.list",
 
53
         "File containing moderators list"],
 
54
    ]
 
55
    
 
56
    subCommands = None
 
57
 
 
58
    def postOptions(self):
 
59
        # XXX - Hmmm.
 
60
        filename = self['file']
 
61
        self['groups'] = [g.strip() for g in open(self['groups']).readlines()
 
62
                          if not g.startswith('#')]
 
63
        self['servers'] = [s.strip() for s in open(self['servers']).readlines()
 
64
                           if not s.startswith('#')]
 
65
        self['moderators'] = [s.split()
 
66
                              for s in open(self['moderators']).readlines()
 
67
                              if not s.startswith('#')]
 
68
        self.db = database.PickleStorage(filename, self['groups'],
 
69
                                         self['moderators'])
 
70
 
 
71
 
 
72
class Options(usage.Options):
 
73
    synopsis = "Usage: mktap news [options]"
 
74
    
 
75
    groups = None
 
76
    servers = None
 
77
    subscriptions = None
 
78
 
 
79
    optParameters = [
 
80
        ["port",       "p", "119",           "Listen port"],
 
81
        ["interface",  "i", "",              "Interface to which to bind"],
 
82
        ["datadir",    "d", "news.db",       "Root data storage path"],
 
83
        ["mailhost",   "m", "localhost",     "Host of SMTP server to use"]
 
84
    ]
 
85
    zsh_actions = {"datadir" : "_dirs", "mailhost" : "_hosts"}
 
86
 
 
87
    def __init__(self):
 
88
        usage.Options.__init__(self)
 
89
        self.groups = []
 
90
        self.servers = []
 
91
        self.subscriptions = []
 
92
 
 
93
 
 
94
    def opt_group(self, group):
 
95
        """The name of a newsgroup to carry."""
 
96
        self.groups.append([group, None])
 
97
 
 
98
 
 
99
    def opt_moderator(self, moderator):
 
100
        """The email of the moderator for the most recently passed group."""
 
101
        self.groups[-1][1] = moderator
 
102
 
 
103
 
 
104
    def opt_subscription(self, group):
 
105
        """A newsgroup to list as a recommended subscription."""
 
106
        self.subscriptions.append(group)
 
107
 
 
108
 
 
109
    def opt_server(self, server):
 
110
        """The address of a Usenet server to pass messages to and receive messages from."""
 
111
        self.servers.append(server)
 
112
 
 
113
 
 
114
def makeService(config):
 
115
    if not len(config.groups):
 
116
        raise usage.UsageError("No newsgroups specified")
 
117
    
 
118
    db = database.NewsShelf(config['mailhost'], config['datadir'])
 
119
    for (g, m) in config.groups:
 
120
        if m:
 
121
            db.addGroup(g, 'm')
 
122
            db.addModerator(g, m)
 
123
        else:
 
124
            db.addGroup(g, 'y')
 
125
    for s in config.subscriptions:
 
126
        print s
 
127
        db.addSubscription(s)
 
128
    s = config['port']
 
129
    if config['interface']:
 
130
        # Add a warning here
 
131
        s += ':interface='+config['interface']
 
132
    return strports.service(s, news.UsenetServerFactory(db, config.servers))