[Spambayes-checkins] spambayes/spambayes OptionsClass.py,1.14,1.15

Skip Montanaro montanaro at users.sourceforge.net
Wed Nov 12 17:01:41 EST 2003


Update of /cvsroot/spambayes/spambayes/spambayes
In directory sc8-pr-cvs1:/tmp/cvs-serv18827

Modified Files:
	OptionsClass.py 
Log Message:
Add set_from_cmdline().  See sb_filter.py for simple usage example.


Index: OptionsClass.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/spambayes/OptionsClass.py,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** OptionsClass.py	6 Nov 2003 22:01:23 -0000	1.14
--- OptionsClass.py	12 Nov 2003 22:01:39 -0000	1.15
***************
*** 575,578 ****
--- 575,618 ----
                                    (sect, opt, val, type(val)))
  
+     def set_from_cmdline(self, arg, stream=None):
+         """Set option from colon-separated sect:opt:val string.
+ 
+         If optional stream arg is not None, error messages will be displayed
+         on stream, otherwise KeyErrors will be propagated up the call chain.
+         """
+         sect, opt, val = arg.split(':', 2)
+         try:
+             val = self.convert(sect, opt, val)
+         except (KeyError, TypeError), msg:
+             if stream is not None:
+                 self._report_option_error(sect, opt, val, stream, msg)
+             else:
+                 raise
+         else:
+             self.set(sect, opt, val)
+ 
+     def _report_option_error(self, sect, opt, val, stream, msg):
+         import textwrap
+         if sect in self.sections():
+             vopts = self.options(True)
+             vopts = [v.split(']', 1)[1] for v in vopts
+                        if v.startswith('[%s]'%sect)]
+             if opt not in vopts:
+                 print >> stream, "Invalid option:", opt
+                 print >> stream, "Valid options for", sect, "are:"
+                 vopts = ', '.join(vopts)
+                 vopts = textwrap.wrap(vopts)
+                 for line in vopts:
+                     print >> stream, '  ', line
+             else:
+                 print >> stream, "Invalid value:", msg
+         else:
+             print >> stream, "Invalid section:", sect
+             print >> stream, "Valid sections are:"
+             vsects = ', '.join(self.sections())
+             vsects = textwrap.wrap(vsects)
+             for line in vsects:
+                 print >> stream, '  ', line
+ 
      def __setitem__(self, key, value):
          self.set(key[0], key[1], value)





More information about the Spambayes-checkins mailing list