summaryrefslogtreecommitdiff
path: root/src/wheel/pkginfo.py
blob: bed016ce59d8db41a569d7c75cf278b8b8aef319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""Tools for reading and writing PKG-INFO / METADATA without caring
about the encoding."""

from email.generator import BytesGenerator
from email.parser import Parser


def read_pkg_info_bytes(bytestr):
    headers = bytestr.decode(encoding="ascii", errors="surrogateescape")
    message = Parser().parsestr(headers)
    return message


def read_pkg_info(path):
    with open(path, encoding="ascii", errors="surrogateescape") as headers:
        message = Parser().parse(headers)

    return message


def write_pkg_info(path, message):
    with open(path, "wb") as out:
        BytesGenerator(out, mangle_from_=False, maxheaderlen=0).flatten(message)