[Spambayes-checkins] spambayes/spambayes CorePlugin.py, 1.1.2.1, 1.1.2.2 Options.py, 1.141.2.3, 1.141.2.4 XMLRPCPlugin.py, 1.1.2.1, 1.1.2.2

Skip Montanaro montanaro at users.sourceforge.net
Tue May 29 03:27:20 CEST 2007


Update of /cvsroot/spambayes/spambayes/spambayes
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv16608/spambayes

Modified Files:
      Tag: CORESVR
	CorePlugin.py Options.py XMLRPCPlugin.py 
Log Message:
more progress - sort of have an xmlrpc server running

Index: CorePlugin.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/spambayes/Attic/CorePlugin.py,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -d -r1.1.2.1 -r1.1.2.2
*** CorePlugin.py	28 May 2007 21:38:53 -0000	1.1.2.1
--- CorePlugin.py	29 May 2007 01:27:17 -0000	1.1.2.2
***************
*** 3,6 ****
--- 3,8 ----
  """
  
+ import sys
+ 
  __author__ = "Skip Montanaro <skip at pobox.com"
  __credits__ = "The Spambayes folk."
***************
*** 10,19 ****
          self.name = name
          self.ui = ui
!         
  class PluginUI:
!     defaults = ()
!     def __init__(self):
!         self.plugin_map = ()
!         from spambayes import Options
!         Options.defaults["Plugin"] = self.defaults
!         Options.load_options()
--- 12,20 ----
          self.name = name
          self.ui = ui
!         self.hammie = None
! 
!     def set_hammie(self, hammie):
!         self.hammie = hammie
! 
  class PluginUI:
!     pass

Index: Options.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/spambayes/Options.py,v
retrieving revision 1.141.2.3
retrieving revision 1.141.2.4
diff -C2 -d -r1.141.2.3 -r1.141.2.4
*** Options.py	24 May 2007 03:19:34 -0000	1.141.2.3
--- Options.py	29 May 2007 01:27:17 -0000	1.141.2.4
***************
*** 1295,1298 ****
--- 1295,1309 ----
       r"\w\w(?:_\w\w)?", RESTORE),
    ),
+   "Plugin": (
+     ("xmlrpc_path", _("XML-RPC path"), "/sbrpc",
+      _("""The path to respond to."""),
+      r"[\w]+", RESTORE),
+     ("xmlrpc_host", _("XML-RPC host"), "localhost",
+      _("""The host to listen on."""),
+      SERVER, RESTORE),
+     ("xmlrpc_port", _("XML-RPC port"), 8001,
+      _("""The port to listen on."""),
+      r"[\d]+", RESTORE),
+     ),
  }
  

Index: XMLRPCPlugin.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/spambayes/Attic/XMLRPCPlugin.py,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -d -r1.1.2.1 -r1.1.2.2
*** XMLRPCPlugin.py	28 May 2007 21:38:53 -0000	1.1.2.1
--- XMLRPCPlugin.py	29 May 2007 01:27:17 -0000	1.1.2.2
***************
*** 1,30 ****
  
  from CorePlugin import Plugin, PluginUI
  from spambayes.OptionsClass import *
! from spambayes.Options import _
  
  class XMLRPCUI(PluginUI):
!     defaults = (
!         ("xmlrpc_path", _("XML-RPC path"), "/sbrpc",
!          _("""The path to respond to."""),
!          r"[\w]+", RESTORE),
!         ("xmlrpc_host", _("XML-RPC host"), "localhost",
!          _("""The host to listen on."""),
!          SERVER, RESTORE),
!         ("xmlrpc_port", _("XML-RPC port"), "8001",
!          _("""The port to listen on."""),
!          r"[\d]+", RESTORE),
          )
  
!     def __init__(self):
!         PluginUI.__init__(self)
!         # Configuration options we will offer to users.
!         self.plugin_map = (
!             (_('XML-RPC Options'), None),
!             ('Plugin',            'xmlrpc_path'),
!             ('Plugin',            'xmlrpc_host'),
!             ('Plugin',            'xmlrpc_port'),
!             )
  
  def register():
!     return Plugin("XMLRPC", XMLRPCUI())
--- 1,66 ----
  
+ import threading
+ from email import Message, message_from_string
+ 
+ from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
+ 
  from CorePlugin import Plugin, PluginUI
  from spambayes.OptionsClass import *
! from spambayes.Options import _, options
  
  class XMLRPCUI(PluginUI):
!     plugin_map = (
!         (_('XML-RPC Options'), None),
!         ('Plugin',            'xmlrpc_path'),
!         ('Plugin',            'xmlrpc_host'),
!         ('Plugin',            'xmlrpc_port'),
          )
  
! class XMLRPCPlugin(Plugin):
!     def __init__(self, name, ui):
!         Plugin.__init__(self, name, ui)
!         host = options["Plugin", "xmlrpc_host"]
!         port = options["Plugin", "xmlrpc_port"]
!         path = options["Plugin", "xmlrpc_path"]
!         self.server = SimpleXMLRPCServer((host, port))
!         # Path is only enforced in Python 2.5 and later but we set it anyway.
!         self.server.RequestHandlerClass.rpc_paths = (path,)
!         self.server.register_function(self.score)
!         self.server.register_function(self.score_mime)
!         self.thread = threading.Thread(target=self.server.serve_forever)
!         self.thread.start()
! 
!     # placeholders
!     def score(self, form, attachments, extra_tokens):
!         mime_message = form_to_mime(form, attachments, tokens)
!         return self.score_mime(mime_message)
! 
!     def score_mime(self, msg):
!         try:
!             if isinstance(msg, (str, unicode)):
!                 msg = message_from_string(msg)
!             tokens = tokenizer.tokenize(msg)
!             return self.state.bayes.spamprob(tokens, evidence=True)
!         except:
!             import traceback
!             traceback.print_exc()
! 
! def form_to_mime(form, attachments, extra_tokens):
!     msg = Message.Message()
!     msg.set_type("multipart/digest")
!     main = Message.Message()
!     main.set_payload(" ".join([str(v) for v in form.values()]))
!     msg.attach(main)
!     for msg_type, content in attachments:
!         attachment = Message.Message()
!         attachment.set_type(msg_type)
!         attachment.set_payload(content)
!         msg.attach(attachment)
!     if extra_tokens:
!         extra = Message.Message()
!         extra.set_payload(" ".join(extra_tokens))
!         msg.attach(extra)
!     return msg
  
  def register():
!     return XMLRPCPlugin("XMLRPC", XMLRPCUI())



More information about the Spambayes-checkins mailing list