[Mailman-Developers] [root@tolna.net: Cron <list@melanie> /usr/bin/python /var/lib/mailman/cron/mailpasswds]

Barry A. Warsaw bwarsaw@cnri.reston.va.us (Barry A. Warsaw)
Thu, 2 Sep 1999 16:03:17 -0400 (EDT)


>>>>> "SR" == Sean Reifschneider <jafo@tummy.com> writes:

    SR> It looks like your cronpass.txt template has a "%(one_list)s"
    SR> in it, which doesn't exist in the data being passed.  The only
    SR> values allowed for substitution in there are hostname,
    SR> useraddr, exreq, and owner.  Did you perhaps modify the
    SR> cronpass.txt template?

    SR> Here's a patch that will fix it in any case.  It causes any
    SR> invalid "%(key)" format strings to remain as they are instead
    SR> of throwing KeyErrors.

I like this, Sean.  I'm going to check in a slight modification
(attached).

Thanks!
-Barry

-------------------- snip snip --------------------
Index: Utils.py
===================================================================
RCS file: /projects/cvsroot/mailman/Mailman/Utils.py,v
retrieving revision 1.75
diff -c -r1.75 Utils.py
*** Utils.py	1999/09/02 19:39:08	1.75
--- Utils.py	1999/09/02 19:51:38
***************
*** 27,32 ****
--- 27,34 ----
  import os
  import string
  import re
+ from UserDict import UserDict
+ from types import StringType
  # XXX: obsolete, should use re module
  import regsub
  import random
***************
*** 620,625 ****
--- 622,645 ----
  
  
  
+ class SafeDict(UserDict):
+     """Dictionary which returns a default value for unknown keys.
+ 
+     This is used in maketext so that editing templates is a bit more robust.
+     """
+     def __init__(self, d):
+         UserDict.__init__(self, d)
+ 
+     def __getitem__(self, key):
+         try:
+             return self.data[key]
+         except KeyError:
+             if type(key) == StringType:
+                 return '%('+key+')s'
+             else:
+                 return '<Missing key: %s>' % `key`
+ 
+ 
  def maketext(templatefile, dict, raw=0):
      """Make some text from a template file.
  
***************
*** 631,639 ****
      fp = open(file)
      template = fp.read()
      fp.close()
      if raw:
!         return template % dict
!     return wrap(template % dict)
  
  
  
--- 651,660 ----
      fp = open(file)
      template = fp.read()
      fp.close()
+     text = template % SafeDict(dict)
      if raw:
!         return text
!     return wrap(text)