mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
Move the 2.6 reST doc tree in place.
This commit is contained in:
parent
f56181ff53
commit
8ec7f65613
445 changed files with 136056 additions and 0 deletions
68
Doc/includes/email-unpack.py
Normal file
68
Doc/includes/email-unpack.py
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""Unpack a MIME message into a directory of files."""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import email
|
||||
import errno
|
||||
import mimetypes
|
||||
|
||||
from optparse import OptionParser
|
||||
|
||||
|
||||
def main():
|
||||
parser = OptionParser(usage="""\
|
||||
Unpack a MIME message into a directory of files.
|
||||
|
||||
Usage: %prog [options] msgfile
|
||||
""")
|
||||
parser.add_option('-d', '--directory',
|
||||
type='string', action='store',
|
||||
help="""Unpack the MIME message into the named
|
||||
directory, which will be created if it doesn't already
|
||||
exist.""")
|
||||
opts, args = parser.parse_args()
|
||||
if not opts.directory:
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
msgfile = args[0]
|
||||
except IndexError:
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
os.mkdir(opts.directory)
|
||||
except OSError, e:
|
||||
# Ignore directory exists error
|
||||
if e.errno <> errno.EEXIST:
|
||||
raise
|
||||
|
||||
fp = open(msgfile)
|
||||
msg = email.message_from_file(fp)
|
||||
fp.close()
|
||||
|
||||
counter = 1
|
||||
for part in msg.walk():
|
||||
# multipart/* are just containers
|
||||
if part.get_content_maintype() == 'multipart':
|
||||
continue
|
||||
# Applications should really sanitize the given filename so that an
|
||||
# email message can't be used to overwrite important files
|
||||
filename = part.get_filename()
|
||||
if not filename:
|
||||
ext = mimetypes.guess_extension(part.get_type())
|
||||
if not ext:
|
||||
# Use a generic bag-of-bits extension
|
||||
ext = '.bin'
|
||||
filename = 'part-%03d%s' % (counter, ext)
|
||||
counter += 1
|
||||
fp = open(os.path.join(opts.directory, filename), 'wb')
|
||||
fp.write(part.get_payload(decode=True))
|
||||
fp.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue