[Tutor] display filter slow

Prahlad Vaidyanathan slime@vsnl.net
Mon, 17 Dec 2001 14:21:31 +0530

Content-Type: multipart/mixed; boundary="h31gzZEtNLTqOjlF"
Content-Disposition: inline

Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable


Since this is my first post to this list, a 'howdy' to everyone here.

Now, I get many mails from people with really crazy quote-strings. So,
as I use mutt, I decided to hack up a script to filter the mail before
it is displayed in mutt's pager (using the $display_filter variable).

I've attached version 0.0001 of the script. The main problem in this
being, the filter takes a great deal of time. ie.

# With display_filter set to this script
    $ time -p mutt -f =3Dinbox -e "push <space>qq"
    real 0.60
    user 0.52
    sys 0.09

# With display_filter set to nothing
    $ time -p mutt -f =3Dinbox -e "push <space>qq"
    real 0.22
    user 0.17
    sys 0.05

As you can see, the script takes nearly thrice the amount of time as
without it. I'm sure there is a more efficient way of filtering my mail,
so any help would be appreciated.

Also, the script doesn't change the quote-string for deeper levels of
quoting, ie.

    % # * blah> quoted text here
    > # * blah> quoted text here
    > > > > quoted text here

I hope I have explained myself clearly.  Actually, a similar script
exists in perl (written by someone else), but I am re-inventing the
wheel because I want to learn python, and I want to do some other stuff
like line-wrapping, etc. which the perl script has not yet implemented.


Prahlad Vaidyanathan <slime@vsnl.net>                What, me worry ?
http://www.symonds.net/~prahladv/                    Don't Panic !

Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="display_filter.py"

#!/usr/bin/env python
import sys
import re

file = sys.stdin

# Set these accding to your needs
strip_trailing_spaces = 1   # "foo bar      \n" => "foo bar\n"
strip_empty_quotes = 1      # "> > \n" => "\n"

# REs
quote_regexp = re.compile("^(-->)|^([ \t]*[A-Za-z]+>)|^([ \t\"]*[|>}%])")
bad_sig = re.compile("^-{1,3}$")

for line in file.readlines():
    # Fix quotes
    if quote_regexp.match(line):
        line = quote_regexp.sub(">",line,10)
    # Strip trailing white-spaces, but not sig-dashes
    if strip_trailing_spaces and not re.match("^-- $",line):
        line = re.compile("\s+$").sub("\n",line)
    # String empty quoted lines
    if strip_empty_quotes:
        line = re.compile("^(>){1,}[ \t]*$").sub("",line)
    # Fix sig-dashes
    if re.match(bad_sig,line):
        line = re.sub(bad_sig,"-- ",line)
    # Display it
    print line,


Content-Type: application/pgp-signature
Content-Disposition: inline

Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org