Python
  Home arrow Python arrow Finishing the PyMailGUI Client: User H...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Finishing the PyMailGUI Client: User Help Tools
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-08-09

    Table of Contents:
  • Finishing the PyMailGUI Client: User Help Tools
  • popuputil: General-Purpose GUI Pop Ups
  • wraplines: Line Split Tools
  • mailcon
  • PyMailGuiHelp: User Help Text
  • Ideas for Improvement

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Finishing the PyMailGUI Client: User Help Tools
    (Page 1 of 6 )

    In this conclusion to a six-part series, you will learn about the user help tools that come with the PyMailGUI client, and more. This article is excerpted from chapter 15 of the book Programming Python, Third Edition, written by Mark Lutz (O'Reilly, 2006; ISBN: 0596009259) Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    messagecache: Message Cache Manager

    The class in Example 15-5 implements a cache for already loaded messages. Its logic is split off into this file in order to avoid complicating list window implementations. The server list window creates and embeds an instance of this class to interface with the mail server, and to keep track of already loaded mail headers and full text.

    Example 15-5. PP3E\Internet\Email\PyMailGui\messagecache.py

    ################################################################ # manage message and header loads and context, but not GUI
    # a MailFetcher, with a list of already loaded headers and messages
    # the caller must handle any required threading or GUI interfaces ###############################################################

    from PP3E.Internet.Email import mailtools from popuputil import askPasswordWindow

    class MessageInfo:
        """
        an item in the mail cache list
        """
        def __init__(self, hdrtext, size):
            self.hdrtext  = hdrtext           # fulltext is cached msg
            self.fullsize = size              # hdrtext is just the hdrs
            self.fulltext = None              # fulltext=hdrtext if no TOP

    class MessageCache(mailtools.MailFetcher): 
        """
        keep track of already loaded headers and messages
        inherits server transfer methods from MailFetcher
        useful in other apps: no GUI or thread assumptions
        """
        def __init__(self):
           
    mailtools.MailFetcher.__init__(self)
            self.msglist = []

        def loadHeaders(self, forceReloads, progress=None):
            """
            three cases to handle here: the initial full load,
            load newly arrived, and forced reload after delete;
            don't refetch viewed msgs if hdrs list same or extended;
            retains cached msgs after a delete unless delete fails;
           
    2.1: does quick check to see if msgnums still in sync
           
    """
            if forceReloads:
                loadfrom = 1
                self.msglist = []          # msg nums have changed
            else:
                loadfrom =
    len(self.msglist)+1                    # continue from last load

            # only if loading newly arrived
            if loadfrom != 1:
                self.checkSynchError(self.allHdrs())                       # raises except if bad

            # get all or newly arrived msgs
           
    reply = self.downloadAllHeaders(progress, loadfrom)
            headersList, msgSizes, loadedFull = reply

            for (hdrs, size) in zip(headersList, msgSizes):
                newmsg = MessageInfo(hdrs, size)
                if loadedFull:             # zip result may be empty
                   
    newmsg.fulltext =
    hdrs                                    # got full msg if no 'top'
                self.msglist.append(newmsg)

        def getMessage
    (self, msgnum):                        # get raw msg text
           
    if not self.msglist[msgnum-1].fulltext:                           # add to cache if fetched
                fulltext = self.downloadMessage(msgnum)                               # harmless if threaded
                self.msglist[msgnum-1].fulltext = fulltext
           
    return self.msglist[msgnum-1].fulltext

        def getMessages(self, msgnums, progress=None):
            """
            prefetch full raw text of multiple messages, in thread;
           
    2.1: does quick check to see if msgnums still in sync;
            we can't get here unless the index list already loaded;
            """
            self.checkSynchError
    (self.allHdrs())            # raises except if bad
           
    nummsgs = len
    msgnums)                    # adds messages to cache
            for (ix, msgnum)
    in enumerate(msgnums):      # some poss already there
            if progress:
    progress(ix+1, nummsgs)     # only connects if needed
            self.getMessage
    (msgnum)                    # but may connect > once

        def getSize
    (self, msgnum):             # encapsulate cache struct
            return self.msglist
    [msgnum-1].fullsize         # it changed once already!

        def isLoaded(self, msgnum):
            return self.msglist[msgnum-1].fulltext

        def allHdrs(self):
            return [msg.hdrtext for msg in self.msglist]

        def deleteMessages(self, msgnums, progress=None):
            """
            if delete of all msgnums works, remove deleted entries
            from mail cache, but don't reload either the headers list
            or already viewed mails text: cache list will reflect the
            changed msg nums on server; if delete fails for any reason,
            caller should forceably reload all hdrs next, because _some_
            server msg nums may have changed, in unpredictable ways;
            2.1: this now checks msg hdrs to detect out of synch msg
            numbers, if TOP supported by mail server; runs in thread
            """
            try:
               
    self.deleteMessagesSafely(msgnums, self.allHdrs(), progress)
            except mailtools.TopNotSupported: 
            mailtools.MailFetcher.deleteMessages(self, msgnums, progress)

            # no errors: update index list 
            indexed = enumerate(self.msglist) 
            self.msglist = [msg for (ix, msg) in indexed if ix+1 not in msgnums]

    class GuiMessageCache(MessageCache):
        """
        add any GUI-specific calls here so cache usable in non-GUI apps
        """

        def setPopPassword(self, appname):
            """
            get password from GUI here, in main thread
            forceably called from GUI to avoid pop ups in threads
            """
            if not self.popPassword:
               
    prompt = 'Password for %s on %s?' % (self.popUser, self.popServer)
                self.popPassword = askPasswordWindow(appname, prompt)

        def askPopPassword(self):
            """
            but don't use GUI pop up here: I am run in a thread!
           
    when tried pop up in thread, caused GUI to hang;
            may be called by MailFetcher superclass, but only
            if passwd is still empty string due to dialog close
            """
            return self.popPassword

    More Python Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming Python, Third Edition,"...
     

    Buy this book now. This article is excerpted from chapter 15 of the book Programming Python, Third Edition, written by Mark Lutz (O'Reilly, 2006; ISBN: 0596009259). Check it out today at your favorite bookstore. Buy this book now.

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway