File: pymailgui-products/unzipped/PyMailGui-PP4E/SharedNames.py

"""
##############################################################################
objects shared by all window classes and main file: program-wide globals
##############################################################################
"""

# python 3.3+ hack (formataddr change)
import py33patch

# used in all window, icon titles
appname  = 'PyMailGUI 4.1'              # 3.1 = start standalone release

# used for list save, open, delete; also for sent messages file
saveMailSeparator = 'PyMailGUI' + ('-'*60) + 'PyMailGUI\n'

# currently viewed mail save files; also for sent-mail file
openSaveFiles = {}                      # 1 window per file,{name:win}

# shared by ViewWindows.py (opens) and PyMailGui.py (auto-prune) [4.0]
tempPartDir = 'TempParts'               # where selected parts saved, in cwd='.'

# standard library services
import sys, os, email.utils, email.message, webbrowser, mimetypes
from tkinter import *
from tkinter.simpledialog import askstring
from tkinter.filedialog   import SaveAs, Open, Directory
from tkinter.messagebox   import showinfo, showerror, askyesno

# reuse book examples
from PP4E.Gui.Tools      import windows      # window border, exit protocols
from PP4E.Gui.Tools      import threadtools  # thread callback queue checker
from PP4E.Internet.Email import mailtools    # load,send,parse,build utilities
from PP4E.Gui.TextEditor import textEditor   # component and pop up

# modules defined here
import mailconfig                            # user params: servers, fonts, etc.
import popuputil                             # help, busy, passwd pop-up windows
import wraplines                             # wrap long message lines
import messagecache                          # remember already loaded mail
import html2text                             # simplistic html->plaintext extract
import PyMailGuiHelp                         # user documentation


def printStack(exc_info):
    """
    debugging: show exception and stack traceback on stdout;
    3.0: change to print stack trace to a real log file if print_tb
    to sys.stdout fails: it does when launched from another program
    on Windows;  without this workaround, PyMailGUI aborts and exits
    altogether, as this is called from the main thread on spawned 
    thread failures;  likely a Python 3.1 bug: it doesn't occur in 
    2.5 or 2.6, and the traceback object works fine if print to file;
    oddly, the print() calls here work (but go nowhere) if spawned;

    [4.0] also move the opening print()s into the exception handler, 
    in case the error contains non-ASCII characters that fail to 
    print on the platform's console;  else, excs may hang the GUI;
    """
    import traceback
    try:
        print(exc_info[0])
        print(exc_info[1])                                      # ok if Unicode ok!
        traceback.print_tb(exc_info[2], file=sys.stdout)        # ok unless spawned!
    except:
        # try again, to a real file, and with UTF8 encoding
        try:
            log = open('_pymailerrlog.txt', 'a', encoding='utf8')  
            log.write('-'*80)
            print(exc_info[0], file=log)                     
            print(exc_info[1], file=log)                        # else gui may exit,     
            traceback.print_tb(exc_info[2], file=log)           # in 3.X, not 2.5/6
        except:
            pass   # but punt if the real file fails too


# [4.0] sanitize emojis in text for display in Tk through its version 8.6;
# out-of-range chars => Unicode replacement (also in frigcal, mergeall, pyedit);
# out-of-range filenames saved in dialogs: pass initialfile=None to disable;
from fixTkBMP import fixTkBMP, isNonBMP


# thread busy counters for threads run by this GUI
# sendingBusy shared by all send windows, used by main window quit

loadingHdrsBusy = threadtools.ThreadCounter()   # only 1
deletingBusy    = threadtools.ThreadCounter()   # only 1
loadingMsgsBusy = threadtools.ThreadCounter()   # poss many
sendingBusy     = threadtools.ThreadCounter()   # poss many



[Home page] Books Code Blog Python Author Train Find ©M.Lutz