82 lines · 3.7 KB · TeX
RawDownload
\section{\module{mailcap} ---         Mailcap file handling.}\declaremodule{standard}{mailcap} \modulesynopsis{Mailcap file handling.}  Mailcap files are used to configure how MIME-aware applications suchas mail readers and Web browsers react to files with different MIMEtypes. (The name ``mailcap'' is derived from the phrase ``mailcapability''.)  For example, a mailcap file might contain a line like\samp{video/mpeg; xmpeg \%s}.  Then, if the user encounters an emailmessage or Web document with the MIME type \mimetype{video/mpeg},\samp{\%s} will be replaced by a filename (usually one belonging to atemporary file) and the \program{xmpeg} program can be automaticallystarted to view the file. The mailcap format is documented in \rfc{1524}, ``A User AgentConfiguration Mechanism For Multimedia Mail Format Information,'' butis not an Internet standard.  However, mailcap files are supported onmost \UNIX{} systems. \begin{funcdesc}{findmatch}{caps, MIMEtype%                            \optional{, key\optional{,                            filename\optional{, plist}}}}Return a 2-tuple; the first element is a string containing the commandline to be executed(which can be passed to \function{os.system()}), and the second element isthe mailcap entry for a given MIME type.  If no matching MIMEtype can be found, \code{(None, None)} is returned. \var{key} is the name of the field desired, which represents the typeof activity to be performed; the default value is 'view', since in the most common case you simply want to view the body of the MIME-typeddata.  Other possible values might be 'compose' and 'edit', if youwanted to create a new body of the given MIME type or alter theexisting body data.  See \rfc{1524} for a complete list of thesefields. \var{filename} is the filename to be substituted for \samp{\%s} in thecommand line; the default value is\code{'/dev/null'} which is almost certainly not what you want, sousually you'll override it by specifying a filename. \var{plist} can be a list containing named parameters; the defaultvalue is simply an empty list.  Each entry in the list must be astring containing the parameter name, an equals sign (\character{=}),and the parameter's value.  Mailcap entries can contain named parameters like \code{\%\{foo\}}, which will be replaced by thevalue of the parameter named 'foo'.  For example, if the command line\samp{showpartial \%\{id\}\ \%\{number\}\ \%\{total\}}was in a mailcap file, and \var{plist} was set to \code{['id=1','number=2', 'total=3']}, the resulting command line would be \code{'showpartial 1 2 3'}.   In a mailcap file, the ``test'' field can optionally be specified totest some external condition (such as the machine architecture, or thewindow system in use) to determine whether or not the mailcap lineapplies.  \function{findmatch()} will automatically check suchconditions and skip the entry if the check fails.\end{funcdesc} \begin{funcdesc}{getcaps}{}Returns a dictionary mapping MIME types to a list of mailcap fileentries. This dictionary must be passed to the \function{findmatch()}function.  An entry is stored as a list of dictionaries, but itshouldn't be necessary to know the details of this representation. The information is derived from all of the mailcap files found on thesystem. Settings in the user's mailcap file \file{\$HOME/.mailcap}will override settings in the system mailcap files\file{/etc/mailcap}, \file{/usr/etc/mailcap}, and\file{/usr/local/etc/mailcap}.\end{funcdesc} An example usage:\begin{verbatim}>>> import mailcap>>> d=mailcap.getcaps()>>> mailcap.findmatch(d, 'video/mpeg', filename='/tmp/tmp1223')('xmpeg /tmp/tmp1223', {'view': 'xmpeg %s'})\end{verbatim}