晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。 林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。 见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝) 既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。 南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。
|
Server : Apache System : Linux srv.rainic.com 4.18.0-553.47.1.el8_10.x86_64 #1 SMP Wed Apr 2 05:45:37 EDT 2025 x86_64 User : rainic ( 1014) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /usr/share/doc/python2-docs/html/_sources/library/ |
Upload File : |
:mod:`multifile` --- Support for files containing distinct parts
================================================================
.. module:: multifile
:synopsis: Support for reading files which contain distinct parts, such as some MIME data.
:deprecated:
.. sectionauthor:: Eric S. Raymond <esr@snark.thyrsus.com>
.. deprecated:: 2.5
The :mod:`email` package should be used in preference to the :mod:`multifile`
module. This module is present only to maintain backward compatibility.
The :class:`MultiFile` object enables you to treat sections of a text file as
file-like input objects, with ``''`` being returned by :meth:`readline` when a
given delimiter pattern is encountered. The defaults of this class are designed
to make it useful for parsing MIME multipart messages, but by subclassing it and
overriding methods it can be easily adapted for more general use.
.. class:: MultiFile(fp[, seekable])
Create a multi-file. You must instantiate this class with an input object
argument for the :class:`MultiFile` instance to get lines from, such as a file
object returned by :func:`open`.
:class:`MultiFile` only ever looks at the input object's :meth:`readline`,
:meth:`seek` and :meth:`tell` methods, and the latter two are only needed if you
want random access to the individual MIME parts. To use :class:`MultiFile` on a
non-seekable stream object, set the optional *seekable* argument to false; this
will prevent using the input object's :meth:`seek` and :meth:`tell` methods.
It will be useful to know that in :class:`MultiFile`'s view of the world, text
is composed of three kinds of lines: data, section-dividers, and end-markers.
MultiFile is designed to support parsing of messages that may have multiple
nested message parts, each with its own pattern for section-divider and
end-marker lines.
.. seealso::
Module :mod:`email`
Comprehensive email handling package; supersedes the :mod:`multifile` module.
.. _multifile-objects:
MultiFile Objects
-----------------
A :class:`MultiFile` instance has the following methods:
.. method:: MultiFile.readline(str)
Read a line. If the line is data (not a section-divider or end-marker or real
EOF) return it. If the line matches the most-recently-stacked boundary, return
``''`` and set ``self.last`` to 1 or 0 according as the match is or is not an
end-marker. If the line matches any other stacked boundary, raise an error. On
encountering end-of-file on the underlying stream object, the method raises
:exc:`Error` unless all boundaries have been popped.
.. method:: MultiFile.readlines(str)
Return all lines remaining in this part as a list of strings.
.. method:: MultiFile.read()
Read all lines, up to the next section. Return them as a single (multiline)
string. Note that this doesn't take a size argument!
.. method:: MultiFile.seek(pos[, whence])
Seek. Seek indices are relative to the start of the current section. The *pos*
and *whence* arguments are interpreted as for a file seek.
.. method:: MultiFile.tell()
Return the file position relative to the start of the current section.
.. method:: MultiFile.next()
Skip lines to the next section (that is, read lines until a section-divider or
end-marker has been consumed). Return true if there is such a section, false if
an end-marker is seen. Re-enable the most-recently-pushed boundary.
.. method:: MultiFile.is_data(str)
Return true if *str* is data and false if it might be a section boundary. As
written, it tests for a prefix other than ``'-``\ ``-'`` at start of line (which
all MIME boundaries have) but it is declared so it can be overridden in derived
classes.
Note that this test is used intended as a fast guard for the real boundary
tests; if it always returns false it will merely slow processing, not cause it
to fail.
.. method:: MultiFile.push(str)
Push a boundary string. When a decorated version of this boundary is found as
an input line, it will be interpreted as a section-divider or end-marker
(depending on the decoration, see :rfc:`2045`). All subsequent reads will
return the empty string to indicate end-of-file, until a call to :meth:`pop`
removes the boundary a or :meth:`.next` call reenables it.
It is possible to push more than one boundary. Encountering the
most-recently-pushed boundary will return EOF; encountering any other
boundary will raise an error.
.. method:: MultiFile.pop()
Pop a section boundary. This boundary will no longer be interpreted as EOF.
.. method:: MultiFile.section_divider(str)
Turn a boundary into a section-divider line. By default, this method
prepends ``'--'`` (which MIME section boundaries have) but it is declared so
it can be overridden in derived classes. This method need not append LF or
CR-LF, as comparison with the result ignores trailing whitespace.
.. method:: MultiFile.end_marker(str)
Turn a boundary string into an end-marker line. By default, this method
prepends ``'--'`` and appends ``'--'`` (like a MIME-multipart end-of-message
marker) but it is declared so it can be overridden in derived classes. This
method need not append LF or CR-LF, as comparison with the result ignores
trailing whitespace.
Finally, :class:`MultiFile` instances have two public instance variables:
.. attribute:: MultiFile.level
Nesting depth of the current part.
.. attribute:: MultiFile.last
True if the last end-of-file was for an end-of-message marker.
.. _multifile-example:
:class:`MultiFile` Example
--------------------------
.. sectionauthor:: Skip Montanaro <skip@pobox.com>
::
import mimetools
import multifile
import StringIO
def extract_mime_part_matching(stream, mimetype):
"""Return the first element in a multipart MIME message on stream
matching mimetype."""
msg = mimetools.Message(stream)
msgtype = msg.gettype()
params = msg.getplist()
data = StringIO.StringIO()
if msgtype[:10] == "multipart/":
file = multifile.MultiFile(stream)
file.push(msg.getparam("boundary"))
while file.next():
submsg = mimetools.Message(file)
try:
data = StringIO.StringIO()
mimetools.decode(file, data, submsg.getencoding())
except ValueError:
continue
if submsg.gettype() == mimetype:
break
file.pop()
return data.getvalue()