MCPcopy Index your code
hub / github.com/python/cpython / main

Function main

Doc/includes/email-unpack.py:14–49  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12
13
14def main():
15 parser = ArgumentParser(description="""\
16Unpack a MIME message into a directory of files.
17""")
18 parser.add_argument('-d', '--directory', required=True,
19 help="""Unpack the MIME message into the named
20 directory, which will be created if it doesn't already
21 exist.""")
22 parser.add_argument('msgfile')
23 args = parser.parse_args()
24
25 with open(args.msgfile, 'rb') as fp:
26 msg = email.message_from_binary_file(fp, policy=default)
27
28 try:
29 os.mkdir(args.directory)
30 except FileExistsError:
31 pass
32
33 counter = 1
34 for part in msg.walk():
35 # multipart/* are just containers
36 if part.get_content_maintype() == 'multipart':
37 continue
38 # Applications should really sanitize the given filename so that an
39 # email message can't be used to overwrite important files
40 filename = part.get_filename()
41 if not filename:
42 ext = mimetypes.guess_extension(part.get_content_type())
43 if not ext:
44 # Use a generic bag-of-bits extension
45 ext = '.bin'
46 filename = f'part-{counter:03d}{ext}'
47 counter += 1
48 with open(os.path.join(args.directory, filename), 'wb') as fp:
49 fp.write(part.get_payload(decode=True))
50
51
52if __name__ == '__main__':

Callers 1

email-unpack.pyFile · 0.70

Calls 13

parse_argsMethod · 0.95
ArgumentParserClass · 0.90
get_content_maintypeMethod · 0.80
guess_extensionMethod · 0.80
get_content_typeMethod · 0.80
get_payloadMethod · 0.80
openFunction · 0.50
add_argumentMethod · 0.45
mkdirMethod · 0.45
walkMethod · 0.45
get_filenameMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…