mailcap — Mailcap 파일 처리

소스 코드: Lib/mailcap.py

버전 3.11에서 폐지되었습니다, 버전 3.13에서 제거됩니다.: The mailcap module is deprecated (see PEP 594 for details). The mimetypes module provides an alternative.


Mailcap files are used to configure how MIME-aware applications such as mail readers and web browsers react to files with different MIME types. (The name “mailcap” is derived from the phrase “mail capability”.) For example, a mailcap file might contain a line like video/mpeg; xmpeg %s. Then, if the user encounters an email message or web document with the MIME type video/mpeg, %s will be replaced by a filename (usually one belonging to a temporary file) and the xmpeg program can be automatically started to view the file.

The mailcap format is documented in RFC 1524, “A User Agent Configuration Mechanism For Multimedia Mail Format Information”, but is not an internet standard. However, mailcap files are supported on most Unix systems.

mailcap.findmatch(caps, MIMEtype, key='view', filename='/dev/null', plist=[])

2-튜플을 반환합니다; 첫 번째 요소는 실행될 명령 줄(os.system()에 전달될 수 있음)을 포함하는 문자열이고, 두 번째 요소는 지정된 MIME 유형에 대한 mailcap 항목입니다. 일치하는 MIME 유형을 찾을 수 없으면 (None, None)이 반환됩니다.

key는 원하는 이름인데, 수행할 활동의 유형을 나타냅니다; 가장 흔히 MIME 형식의 데이터 본문을 보고만 싶으므로 기본값은 ‘view’ 입니다. 주어진 MIME 유형의 새 본문을 만들거나 기존 본문 데이터를 변경하려고 할 때, 다른 가능한 값으로 ‘compose’ 이나 ‘edit’ 가 있습니다. 이 필드의 전체 목록은 RFC 1524를 참조하십시오.

filename은 명령 줄에서 %s에 치환될 파일명입니다. 기본값은 '/dev/null' 이지만, 거의 확실하게 원하는 것이 아닐 것이기 때문에, 보통 파일명을 지정하여 이를 대체합니다.

plist는 이름있는 매개 변수를 포함하는 리스트일 수 있습니다; 기본값은 단순히 빈 리스트입니다. 리스트의 각 항목은 매개 변수 이름, 등호('=') 및 매개 변수의 값이 포함된 문자열이어야 합니다. Mailcap 항목은 %{foo}와 같은 이름있는 매개 변수를 포함할 수 있는데, ‘foo’ 라는 이름의 매개 변수의 값으로 치환됩니다. 예를 들어, 명령 줄 showpartial %{id} %{number} %{total}이 mailcap 파일에 있고, plist['id=1', 'number=2', 'total=3']로 설정되었으면, 결과 명령 줄은 'showpartial 1 2 3'가 됩니다.

mailcap 파일에서, “test” 필드를 선택적으로 지정하여 mailcap 줄이 적용되는지를 판단하기 위해 일부 외부 조건(가령 기계 아키텍처나 사용 중인 윈도우 시스템)을 검사할 수 있습니다. findmatch()는 자동으로 그러한 조건을 검사하고 검사가 실패하면 항목을 건너뜁니다.

버전 3.11에서 변경: To prevent security issues with shell metacharacters (symbols that have special effects in a shell command line), findmatch will refuse to inject ASCII characters other than alphanumerics and @+=:,./-_ into the returned command line.

If a disallowed character appears in filename, findmatch will always return (None, None) as if no entry was found. If such a character appears elsewhere (a value in plist or in MIMEtype), findmatch will ignore all mailcap entries which use that value. A warning will be raised in either case.

mailcap.getcaps()

MIME 유형을 mailcap 파일 항목 리스트에 매핑하는 딕셔너리를 반환합니다. 이 딕셔너리는 findmatch() 함수에 전달되어야 합니다. 항목은 딕셔너리의 리스트로 저장되지만, 이 표현의 세부 사항을 알 필요는 없습니다.

이 정보는 시스템에서 발견된 모든 mailcap 파일에서 파생됩니다. 사용자의 mailcap 파일 $HOME/.mailcap의 설정은 시스템 mailcap 파일 /etc/mailcap, /usr/etc/mailcap/usr/local/etc/mailcap의 설정보다 우선합니다.

사용 예:

>>> import mailcap
>>> d = mailcap.getcaps()
>>> mailcap.findmatch(d, 'video/mpeg', filename='tmp1223')
('xmpeg tmp1223', {'view': 'xmpeg %s'})