我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用email.parser.HeaderParser()。
def parse_docstring(docstring): """ Parse out the parts of a docstring. Return (title, body, metadata). """ docstring = trim_docstring(docstring) parts = re.split(r'\n{2,}', docstring) title = parts[0] if len(parts) == 1: body = '' metadata = {} else: parser = HeaderParser() try: metadata = parser.parsestr(parts[-1]) except HeaderParseError: metadata = {} body = "\n\n".join(parts[1:]) else: metadata = dict(metadata.items()) if metadata: body = "\n\n".join(parts[1:-1]) else: body = "\n\n".join(parts[1:]) return title, body, metadata
def test_message_rfc822_only(self): # Issue 7970: message/rfc822 not in multipart parsed by # HeaderParser caused an exception when flattened. with openfile(findfile('msg_46.txt')) as fp: msgdata = fp.read() parser = HeaderParser() msg = parser.parsestr(msgdata) out = StringIO() gen = Generator(out, True, 0) gen.flatten(msg, False) self.assertEqual(out.getvalue(), msgdata)
def parse_docstring(docstring): """ Parse out the parts of a docstring. Returns (title, body, metadata). """ docstring = trim_docstring(docstring) parts = re.split(r'\n{2,}', docstring) title = parts[0] if len(parts) == 1: body = '' metadata = {} else: parser = HeaderParser() try: metadata = parser.parsestr(parts[-1]) except HeaderParseError: metadata = {} body = "\n\n".join(parts[1:]) else: metadata = dict(metadata.items()) if metadata: body = "\n\n".join(parts[1:-1]) else: body = "\n\n".join(parts[1:]) return title, body, metadata
def test_message_rfc822_only(self): # Issue 7970: message/rfc822 not in multipart parsed by # HeaderParser caused an exception when flattened. with openfile('msg_46.txt') as fp: msgdata = fp.read() parser = HeaderParser() msg = parser.parsestr(msgdata) out = StringIO() gen = Generator(out, True, 0) gen.flatten(msg, False) self.assertEqual(out.getvalue(), msgdata)
def header_charset(s): p = HeaderParser() return p.parsestr(s).get_content_charset()
def header_charset(s): p = HeaderParser() return p.parsestr(s.encode('utf-8', 'ignore')).get_content_charset()
def main(): outdir = os.path.join(os.path.dirname('__file__'), 'build') try: shutil.rmtree(outdir) except FileNotFoundError: pass os.mkdir(outdir) filenames = [] names = {} for filename in sorted(glob.glob("pep-*.rst")): outbasename = os.path.basename(filename[:-4] + '.html') filenames.append(outbasename) outfilename = os.path.join(outdir, outbasename) pepnum = get_pepnum(outfilename) print(filename, '->', outfilename) with open(filename) as inf, open(outfilename, 'w') as outf: fix_rst_pep(inf, outf, filename, pepnum) with open(filename) as inf: parser = HeaderParser() metadata = parser.parse(inf) names[pepnum] = metadata['Title'] index_filename = os.path.join(outdir, 'index.html') print(index_filename) with open(index_filename, 'w') as f: f.write('<html><head><title>Draft PEP index</title></head>') f.write('<body><h1>Draft PEP index</h1><ul>') for filename in filenames: pepnum = get_pepnum(filename) f.write('<li>{num}: <a href="{link}">{name}</a></li>'.format( link=filename, num=pepnum, name=names[pepnum])) f.write('</ul></body></html>')