[spambayes-dev] Use of email factory function

Sjoerd Mullender sjoerd at acm.org
Tue Aug 3 10:15:04 CEST 2004


Tony Meyer wrote:
> [Sjoerd Mullender]
> 
>>I was wondering, instead of having the email parser create a new
>>instance and then copying over the internals, could we not 
>>make use of the factory function argument?
>>
>>We could call the parser like this:
>>
>>new_msg = email.Parser.Parser(lambda x=self:
>>x).parsestr(data[self.rfc822_key])
>>
>>The parser will call the factory function passed as its first
>>argument to get a new instance of a Message.  But we could also just
>>use a factory function that returns the already existing instance (lambda 
>>x=self: x) and then not copy over the internals.
> 
> 
> [Tony Meyer] 
> 
>>I (think I) wrote that code, and I'm certainly hazy on many 
>>parts of the email package and the parsers.  I didn't know 
>>that the above was possible (I suspected something like it, 
>>but couldn't figure it out).
>>
>>+1 to checking this in.
> 
> 
> Thinking about things more (and going over sb_imapfilter code for other
> reasons), I'm again not sure how this works (but if it does, then still
> +1!).
> 
> What we want is to make _ourselves_ be the message that is returned from
> email.Parser.Parser.  I can see the above being nicer if we were copying
> *to* new_msg, but we are copying *from* it.  I think the proper solution is
> to move the code that calls this outside of the message instance itself, but
> that involves a fair bit of work.  Hopefully, though, I'm misunderstanding
> things :)

I just discovered it doesn't work.  It doesn't work for multipart 
messages since each part is an new instance, and with my patch all those 
instances would be the same instance.
If we can do something that returns self the first time it is called and 
a (truly) new instance for all subsequent calls, it might work. 
Something like this, perhaps:

class IMAPMessage(message.SBHeaderMessage):
     def __init__(self):
         self.first_factory_call = True
         [...]

     def factory(self):
         # first time return self, all subsequent times, return a new
         # Message instance
         if self.first_factory_call:
             self.first_factory_call = False
             return self
         return email.Message.Message()

     [...]

         try:
             new_msg = 
email.Parser.Parser(self.factory).parsestr(data[self.rfc822_key])
         [...]

-- 
Sjoerd Mullender <sjoerd at acm.org>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 374 bytes
Desc: OpenPGP digital signature
Url : http://mail.python.org/pipermail/spambayes-dev/attachments/20040803/e4ea4f12/signature.pgp


More information about the spambayes-dev mailing list