[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: [PATCH] mail address handling in mailer.py

From: C. Michael Pilato <cmpilato_at_collab.net>
Date: 2006-05-30 15:23:44 CEST

Mathias Weinert wrote:
> Hi there,
>
> I am still hoping that some will commit the following patch to mailer.py
> so please let my wishes get true...
>
> This patch adds a new parameter to the configuration file which allows
> to use different characters for splitting the to_addr.
>
> This is usefull for using additional name information in the mail
> addresses. Example:
>
> to_addr_split_char = ,
> to_addr = "The great Wallace" <wallace@atanyaddress>, "little Grommit" <grommit@propablyatthesameaddress>

I find this two-option setup silly. Just make the code smart enough to know
that email records are comma-delimited, and any commas in the quoted regions
don't count towards that splitting. I whipped up the following helper
function for performing such a split, but I'm sure there's probably a
slicker/quicker way to do it:

#!/usr/bin/python
import sys

def parse_addrs(addr_line):
    """Split ADDR_LINE on commas, but don't count commas that enclosed
    in doublequote-delimited regions. Doublequote characters
    preceeded by a backslash (\) don't count as delimiters for those
    regions, either."""

    quoted = 0
    commas = []
    for i in range(len(addr_line)):
        if addr_line[i] == '"':
            if ((i and addr_line[i-1] != '\\') or (not i)):
                if quoted:
                    quoted = 0
                else:
                    quoted = 1
        elif addr_line[i] == ',':
            if not quoted:
                commas.append(i)
    commas.append(len(addr_line))
    pieces = []
    for i in range(len(commas)):
        comma = commas[i]
        if not i:
            pieces.append(addr_line[:comma].strip())
        else:
            pieces.append(addr_line[commas[i-1]+1:comma].strip())
    if i:
        pieces.append(addr_line[commas[-1]+1:].strip())
    return filter(None, pieces)

if __name__ == "__main__":
    addrs = parse_addrs(sys.argv[1])
    print '\n'.join(addrs)

-- 
C. Michael Pilato <cmpilato@collab.net>
CollabNet   <>   www.collab.net   <>   Distributed Development On Demand

Received on Tue May 30 15:25:40 2006

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.