Python
  Home arrow Python arrow Finishing the PyMailGUI Client: User Help Tools
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT