[Twisted-Python] Re: IMAP fixes

Jp Calderone exarkun at twistedmatrix.com
Wed Jul 9 06:04:48 EDT 2003

On Wed, Jul 09, 2003 at 04:47:57PM +1200, Tony Meyer wrote:
> [Tony]
> > > For example, this command:
> >     0003 FETCH 1 BODY.PEEK[HEADER.FIELDS (References X-Ref)] UID
> [Jp]
> > Ideally, I'd like to see this parsed into a structure 
> > resembling this:
> > 
> >     [Header(peek=1, name=1, value=1, ['References', 'X-Ref']), UID()]
> > 
> > Would you be interested in patching the fetch methods to 
> > generate a structure like the one I described above?
> This command now becomes:
>     ["BODY(PEEK=1, HEADER=1, FIELDS=1, fielditems=['References', 'X-Ref'])",
> 'UID()']
> Is this suitable?  I presume(d) from the structure that you suggested that
> one way of handling this would be to then exec each element (having a
> BODY(), UID function, and so on).

  Eeep :o  This isn't quite what I was going for... The exec stage is a big
no-no from my point of view.  I'd rather have Body be a class, with peek,
fields, etc as boolean attributes on its instances.  This avoids having to
build up strings in the protocol code, and avoids forcing client code to
turn it back into something easily parsed.  I guess I should give a couple

  The body class might be defined something like this (and instantiated as
above, with proper case normalization and removal of the quotes making the
whole thing a string):

    class Body:
        name = 'body'
	def __init__(self, peek=False, header=False, fields=False, **kwargs):
            self.peek, self.header, self.fields = peek, header, fields

        def __str__(self):
            base = 'BODY'
            if self.peek:
                base = 'BODY.PEEK'
            opt = ''
            if self.header:
                if self.fields:
                    opt = '[HEADER.FIELDS %s]' % ' '.join(self.fielditems)
                    opt = '[HEADER %s]' % ' '.join(self.fielditems)
            elif # ... something else
            return '%s%s' % (base, opt)

  Client code could expect to use it like this:

    def fetch(self, msgs, queryParts, uid):
        r = {}
        for q in queryParts:
            r[q] = getattr(self, 'fetch_' + q.name)(msgs, q, uid)

    def fetch_body(self, msgs, query, uid):
        if not query.peek:
            # mark specified messages as seen
        if query.header:
            if query.fields:

  There's a clear problem with the way Body.__str__ is implemented (no
quoting -- but maybe this could be fixed with a DB-API-style quoting
tactic), and obviously large parts of the implementation are missing, but I
hope this gets my idea across a little more clearly.  I am dead tired at the
moment, I just wanted to respond quickly to clear up the point of strings vs
classes.  If I still haven't succeeded in conveying all my thoughts clearly,
please let me know and I'll try again :)

> If there are problems with this, I'm happy to make changes.  I wrote it
> against the RFC, so there may be things that need to be changed if there are
> clients that do things illegally that are meant to be supported.  It also
> _only_ supports rfc2060, not any extra additions, although if they are
> simple ones or macros then it would be easy to simply append them to the
> appropriate list.

  This sounds fine.  I do like that it moves the macro processing out of the
mailbox and into the protocol code.

> Anyway, here it is, let me know what you think.

  The only other thing I wanted to mention was that scary regular
expression.  I haven't even attempted to decipher it yet, but I was curious
if you pulled it out of or based it off of one of the RFCs, or if you came
up with it yourself?  In my experience expressions that long tend to be
wrong in unexpected and surprising ways, and I'm not inclined to trust them
without careful analysis, or at least the knowledge that they come from an
authoritative source (and even then, ensuring the proper meaning survived
into the translation to python REs is a pain...)

  All that said, thanks for the patch!  I'm looking forward to the next
revision :)


        "I quite agree with you," said the Duchess; "and the moral of
that is -- Be what you would seem to be' -- or, if you'd like it put
more simply -- Never imagine yourself not to be otherwise than what it
might appear to others that what you were or might have been was not 
otherwise than what you had been would have appeared to them to be
otherwise.'"       -- Lewis Carrol, "Alice in Wonderland"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20030709/499cd46e/attachment.pgp 

More information about the Twisted-Python mailing list