Home | History | Annotate | Download | only in email
      1 # Copyright (C) 2001-2006 Python Software Foundation
      2 # Author: Barry Warsaw
      3 # Contact: email-sig (at] python.org
      4 
      5 """A package for parsing, handling, and generating email messages."""
      6 
      7 __version__ = '4.0.3'
      8 
      9 __all__ = [
     10     # Old names
     11     'base64MIME',
     12     'Charset',
     13     'Encoders',
     14     'Errors',
     15     'Generator',
     16     'Header',
     17     'Iterators',
     18     'Message',
     19     'MIMEAudio',
     20     'MIMEBase',
     21     'MIMEImage',
     22     'MIMEMessage',
     23     'MIMEMultipart',
     24     'MIMENonMultipart',
     25     'MIMEText',
     26     'Parser',
     27     'quopriMIME',
     28     'Utils',
     29     'message_from_string',
     30     'message_from_file',
     31     # new names
     32     'base64mime',
     33     'charset',
     34     'encoders',
     35     'errors',
     36     'generator',
     37     'header',
     38     'iterators',
     39     'message',
     40     'mime',
     41     'parser',
     42     'quoprimime',
     43     'utils',
     44     ]
     45 
     46 
     47 
     49 # Some convenience routines.  Don't import Parser and Message as side-effects
     50 # of importing email since those cascadingly import most of the rest of the
     51 # email package.
     52 def message_from_string(s, *args, **kws):
     53     """Parse a string into a Message object model.
     54 
     55     Optional _class and strict are passed to the Parser constructor.
     56     """
     57     from email.parser import Parser
     58     return Parser(*args, **kws).parsestr(s)
     59 
     60 
     61 def message_from_file(fp, *args, **kws):
     62     """Read a file and parse its contents into a Message object model.
     63 
     64     Optional _class and strict are passed to the Parser constructor.
     65     """
     66     from email.parser import Parser
     67     return Parser(*args, **kws).parse(fp)
     68 
     69 
     70 
     72 # Lazy loading to provide name mapping from new-style names (PEP 8 compatible
     73 # email 4.0 module names), to old-style names (email 3.0 module names).
     74 import sys
     75 
     76 class LazyImporter(object):
     77     def __init__(self, module_name):
     78         self.__name__ = 'email.' + module_name
     79 
     80     def __getattr__(self, name):
     81         __import__(self.__name__)
     82         mod = sys.modules[self.__name__]
     83         self.__dict__.update(mod.__dict__)
     84         return getattr(mod, name)
     85 
     86 
     87 _LOWERNAMES = [
     88     # email.<old name> -> email.<new name is lowercased old name>
     89     'Charset',
     90     'Encoders',
     91     'Errors',
     92     'FeedParser',
     93     'Generator',
     94     'Header',
     95     'Iterators',
     96     'Message',
     97     'Parser',
     98     'Utils',
     99     'base64MIME',
    100     'quopriMIME',
    101     ]
    102 
    103 _MIMENAMES = [
    104     # email.MIME<old name> -> email.mime.<new name is lowercased old name>
    105     'Audio',
    106     'Base',
    107     'Image',
    108     'Message',
    109     'Multipart',
    110     'NonMultipart',
    111     'Text',
    112     ]
    113 
    114 for _name in _LOWERNAMES:
    115     importer = LazyImporter(_name.lower())
    116     sys.modules['email.' + _name] = importer
    117     setattr(sys.modules['email'], _name, importer)
    118 
    119 
    120 import email.mime
    121 for _name in _MIMENAMES:
    122     importer = LazyImporter('mime.' + _name.lower())
    123     sys.modules['email.MIME' + _name] = importer
    124     setattr(sys.modules['email'], 'MIME' + _name, importer)
    125     setattr(sys.modules['email.mime'], _name, importer)
    126