File: core_server.py

package info (click to toggle)
spambayes 1.1a6-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,712 kB
  • sloc: python: 48,776; ansic: 535; sh: 87; lisp: 83; makefile: 46
file content (205 lines) | stat: -rw-r--r-- 6,400 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python

"""The primary server for SpamBayes.

Currently serves the web interface only.  Plugging in listeners for various
protocols is TBD.  This is a first cut at creating a standalone server which
uses a plugin architecture to support different protocols.  The primary
motivation is that web apps like MoinMoin, Trac and Roundup can use spam
detection, but they don't necessarily provide the mechanisms necessary to
save ham and spam databases for retraining.  By providing protocol plugins
you should be able to fairly easily provide (for example) an XML-RPC
interface web apps can use.  The core server takes care of all the training
bells and whistles.

Usage:

    core_server.py [options]

        options:
            -h      : Displays this help message.
            -P module :
                      Identify plugin module to use (required)
            -d FILE : use the named DBM database file
            -p FILE : the the named Pickle database file
            -u port : User interface listens on this port number
                      (default 8880; Browse http://localhost:8880/)
            -b      : Launch a web browser showing the user interface.

            -o section:option:value :
                      set [section, option] in the options database
                      to value

        All command line arguments and switches take their default
        values from the [html_ui] section of bayescustomize.ini.
"""

# This module is part of the spambayes project, which is Copyright 2002-2007
# The Python Software Foundation and is covered by the Python Software
# Foundation license.

__author__ = "Richie Hindle <richie@entrian.com>"
__credits__ = "Tim Peters, Neale Pickett, Tim Stone, all the Spambayes folk."

_TODO = """

Protocol plugin interface:
 o Classifier for web apps (e.g. Trac, Roundup, Moin)
 o POP3?
 o NNTP?

Web training interface:

User interface improvements:

 o Once the pieces are on separate pages, make the paste box bigger.
 o Deployment: Windows executable?  atlaxwin and ctypes?  Or just
   webbrowser?
 o "Reload database" button.


New features:

 o Online manual.
 o Links to project homepage, mailing list, etc.
 o List of words with stats (it would have to be paged!) a la SpamSieve.


Info:

 o Slightly-wordy index page; intro paragraph for each page.
 o In both stats and training results, report nham and nspam.
 o "Links" section (on homepage?) to project homepage, mailing list,
   etc.


Gimmicks:

 o Graphs.  Of something.  Who cares what?
"""

import sys, getopt

from spambayes import Dibbler
from spambayes.Options import options, _
from spambayes.UserInterface import UserInterfaceServer
from spambayes.Version import get_current_version
from spambayes.CoreUI import CoreUserInterface, CoreState, \
     AlreadyRunningException


# Increase the stack size on MacOS X.  Stolen from Lib/test/regrtest.py
if sys.platform == 'darwin':
    try:
        import resource
    except ImportError:
        pass
    else:
        soft, hard = resource.getrlimit(resource.RLIMIT_STACK)
        newsoft = min(hard, max(soft, 1024*2048))
        resource.setrlimit(resource.RLIMIT_STACK, (newsoft, hard))

# Option-parsing helper functions
def _addressAndPort(s):
    """Decode a string representing a port to bind to, with optional address."""
    s = s.strip()
    if ':' in s:
        addr, port = s.split(':')
        return addr, int(port)
    else:
        return '', int(s)

def _addressPortStr((addr, port)):
    """Encode a string representing a port to bind to, with optional address."""
    if not addr:
        return str(port)
    else:
        return '%s:%d' % (addr, port)

def load_plugin(name, state):
    try:
        plugin_module = __import__(name)
    except ImportError:
        plugin_module = __import__("spambayes.%s" % name)
        plugin_module = getattr(plugin_module, name)
    plugin = plugin_module.register()
    plugin.state = state
    return plugin

def main(state):
    """Runs the core server forever or until a 'KILL' command is received or
    someone hits Ctrl+Break."""
    http_server = UserInterfaceServer(state.ui_port)
    http_server.register(CoreUserInterface(state))
    Dibbler.run(launchBrowser=state.launch_ui)

# ===================================================================
# __main__ driver.
# ===================================================================

def run():
    # Read the arguments.
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'hbd:p:l:u:o:P:')
    except getopt.error, msg:
        print >> sys.stderr, str(msg) + '\n\n' + __doc__
        sys.exit()

    state = CoreState()
    state.plugin = None

    for opt, arg in opts:
        if opt == '-h':
            print >> sys.stderr, __doc__
            sys.exit()
        elif opt == '-b':
            state.launch_ui = True
        # '-p' and '-d' are handled by the storage.database_type call
        # below, in case you are wondering why they are missing.
        elif opt == '-l':
            state.proxyPorts = [_addressAndPort(a) for a in arg.split(',')]
        elif opt == '-u':
            state.ui_port = int(arg)
        elif opt == '-o':
            options.set_from_cmdline(arg, sys.stderr)
        elif opt == '-P':
            state.plugin = load_plugin(arg, state)

    if state.plugin is None:
        print >> sys.stderr, "No plugin argument (-P) was given."
        print >> sys.stderr, __doc__
        sys.exit()

    # Let the user know what they are using...
    v = get_current_version()
    print "%s\n" % (v.get_long_version("SpamBayes Core Proxy"),)

    if 0 <= len(args) <= 2:
        # Normal usage, with optional server name and port number.
        if len(args) == 1:
            state.servers = [(args[0], 110)]
        elif len(args) == 2:
            state.servers = [(args[0], int(args[1]))]

        try:
            state.prepare()
        except AlreadyRunningException:
            print  >> sys.stderr, \
                   "ERROR: The proxy is already running on this machine."
            print  >> sys.stderr, "Please stop the existing proxy and try again"
            return

        # kick everything off
        try:
            main(state)
        finally:
            state.close()

    else:
        print >> sys.stderr, __doc__

if __name__ == '__main__':
    try:
        run()
    except KeyboardInterrupt:
        print "bye!"