当前位置: 首页>>代码示例>>Python>>正文


Python SpooledTemporaryFile.read方法代码示例

本文整理汇总了Python中tempfile.SpooledTemporaryFile.read方法的典型用法代码示例。如果您正苦于以下问题:Python SpooledTemporaryFile.read方法的具体用法?Python SpooledTemporaryFile.read怎么用?Python SpooledTemporaryFile.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tempfile.SpooledTemporaryFile的用法示例。


在下文中一共展示了SpooledTemporaryFile.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _do_execute_direct

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
 def _do_execute_direct(self, code):
     shell = builtins.__xonsh_shell__
     env = builtins.__xonsh_env__
     out = io.StringIO()
     err = io.StringIO()
     enc = env.get('XONSH_ENCODING')
     out = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
                                encoding=enc, newline='\n')
     err = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
                                encoding=enc, newline='\n')
     try:
         with redirect_stdout(out), redirect_stderr(err), \
              swap(builtins, '__xonsh_stdout_uncaptured__', out), \
              swap(builtins, '__xonsh_stderr_uncaptured__', err), \
              env.swap({'XONSH_STORE_STDOUT': False}):
             shell.default(code)
         interrupted = False
     except KeyboardInterrupt:
         interrupted = True
     output, error = '', ''
     if out.tell() > 0:
         out.seek(0)
         output = out.read()
     if err.tell() > 0:
         err.seek(0)
         error = err.read()
     out.close()
     err.close()
     return output, error, interrupted
开发者ID:Calysto,项目名称:xonsh_kernel,代码行数:31,代码来源:kernel.py

示例2: do_execute

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
    def do_execute(self, code, silent, store_history=True, user_expressions=None,
                   allow_stdin=False):
        """Execute user code."""
        if len(code.strip()) == 0:
            return {'status': 'ok', 'execution_count': self.execution_count,
                    'payload': [], 'user_expressions': {}}
        env = builtins.__xonsh_env__
        shell = builtins.__xonsh_shell__
        hist = builtins.__xonsh_history__
        enc = env.get('XONSH_ENCODING')
        out = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
                                   encoding=enc, newline='\n')
        err = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
                                   encoding=enc, newline='\n')
        try:
            with redirect_stdout(out), redirect_stderr(err), \
                 swap(builtins, '__xonsh_stdout_uncaptured__', out), \
                 swap(builtins, '__xonsh_stderr_uncaptured__', err), \
                 env.swap({'XONSH_STORE_STDOUT': False}):
                shell.default(code)
            interrupted = False
        except KeyboardInterrupt:
            interrupted = True

        if not silent:  # stdout response
            if out.tell() > 0:
                out.seek(0)
                self._respond_in_chunks('stdout', out.read())
            if err.tell() > 0:
                err.seek(0)
                self._respond_in_chunks('stderr', err.read())
            if hasattr(builtins, '_') and builtins._ is not None:
                # rely on sys.displayhook functionality
                self._respond_in_chunks('stdout', pformat(builtins._))
                builtins._ = None
            if len(hist) > 0 and out.tell() == 0 and err.tell() == 0:
                self._respond_in_chunks('stdout', hist.outs[-1])

        out.close()
        err.close()

        if interrupted:
            return {'status': 'abort', 'execution_count': self.execution_count}

        rtn = 0 if len(hist) == 0 else hist.rtns[-1]
        if 0 < rtn:
            message = {'status': 'error', 'execution_count': self.execution_count,
                       'ename': '', 'evalue': str(rtn), 'traceback': []}
        else:
            message = {'status': 'ok', 'execution_count': self.execution_count,
                       'payload': [], 'user_expressions': {}}
        return message
开发者ID:BlaXpirit,项目名称:xonsh,代码行数:54,代码来源:jupyter_kernel.py

示例3: do_execute

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
    def do_execute(self, code, silent, store_history=True, user_expressions=None, allow_stdin=False):
        """Execute user code."""
        if len(code.strip()) == 0:
            return {"status": "ok", "execution_count": self.execution_count, "payload": [], "user_expressions": {}}
        env = builtins.__xonsh_env__
        shell = builtins.__xonsh_shell__
        hist = builtins.__xonsh_history__
        enc = env.get("XONSH_ENCODING")
        out = SpooledTemporaryFile(max_size=MAX_SIZE, mode="w+t", encoding=enc, newline="\n")
        err = SpooledTemporaryFile(max_size=MAX_SIZE, mode="w+t", encoding=enc, newline="\n")
        try:
            with redirect_stdout(out), redirect_stderr(err), swap(builtins, "__xonsh_stdout_uncaptured__", out), swap(
                builtins, "__xonsh_stderr_uncaptured__", err
            ), env.swap({"XONSH_STORE_STDOUT": False}):
                shell.default(code)
            interrupted = False
        except KeyboardInterrupt:
            interrupted = True

        if not silent:  # stdout response
            if out.tell() > 0:
                out.seek(0)
                self._respond_in_chunks("stdout", out.read())
            if err.tell() > 0:
                err.seek(0)
                self._respond_in_chunks("stderr", err.read())
            if hasattr(builtins, "_") and builtins._ is not None:
                # rely on sys.displayhook functionality
                self._respond_in_chunks("stdout", pformat(builtins._))
                builtins._ = None
            if len(hist) > 0 and out.tell() == 0 and err.tell() == 0:
                self._respond_in_chunks("stdout", hist.outs[-1])

        out.close()
        err.close()

        if interrupted:
            return {"status": "abort", "execution_count": self.execution_count}

        rtn = 0 if len(hist) == 0 else hist.rtns[-1]
        if 0 < rtn:
            message = {
                "status": "error",
                "execution_count": self.execution_count,
                "ename": "",
                "evalue": str(rtn),
                "traceback": [],
            }
        else:
            message = {"status": "ok", "execution_count": self.execution_count, "payload": [], "user_expressions": {}}
        return message
开发者ID:takluyver,项目名称:xonsh,代码行数:53,代码来源:jupyter_kernel.py

示例4: run_command

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
    def run_command(self, command, stdin=None, env=None):
        """
        Launch a shell command line.

        :param command: Command line to launch
        :type command: str
        :param stdin: Standard input of command
        :type stdin: file
        :param env: Environment variable used in command
        :type env: dict
        :return: Standard output of command
        :rtype: file
        """
        cmd = shlex.split(command)
        stdout = SpooledTemporaryFile(max_size=settings.TMP_FILE_MAX_SIZE,
                                      dir=settings.TMP_DIR)
        stderr = SpooledTemporaryFile(max_size=settings.TMP_FILE_MAX_SIZE,
                                      dir=settings.TMP_DIR)
        full_env = self.env.copy()
        full_env.update(env or {})
        try:
            process = Popen(cmd, stdin=stdin, stdout=stdout, stderr=stderr,
                            env=full_env)
            process.wait()
            if process.poll():
                stderr.seek(0)
                raise exceptions.CommandConnectorError(
                    "Error running: {}\n{}".format(command, stderr.read()))
            stdout.seek(0)
            stderr.seek(0)
            return stdout, stderr
        except OSError as err:
            raise exceptions.CommandConnectorError(
                "Error running: {}\n{}".format(command, str(err)))
开发者ID:Ubiwhere,项目名称:django-dbbackup,代码行数:36,代码来源:base.py

示例5: InputStream

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
class InputStream(object):
    """
    FCGI_STDIN or FCGI_DATA stream.
    Uses temporary file to store received data once max_mem bytes
    have been received.
    """
    def __init__(self, max_mem=1024):
        self._file = SpooledTemporaryFile(max_mem)
        self._eof_received = Event()

    def feed(self, data):
        if self._eof_received.is_set():
            raise IOError('Feeding file beyond EOF mark')
        if not data:  # EOF mark
            self._file.seek(0)
            self._eof_received.set()
        else:
            self._file.write(data)

    def __iter__(self):
        self._eof_received.wait()
        return iter(self._file)

    def read(self, size=-1):
        self._eof_received.wait()
        return self._file.read(size)

    def readlines(self, sizehint=0):
        self._eof_received.wait()
        return self._file.readlines(sizehint)

    @property
    def eof_received(self):
        return self._eof_received.is_set()
开发者ID:haosdent,项目名称:gevent-fastcgi,代码行数:36,代码来源:base.py

示例6: _shell_command

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
	def _shell_command(self, cmd):
		"""Shell out a subprocess and return what it writes to stdout as a string"""
		in_mem_file = SpooledTemporaryFile(max_size=2048, mode="r+")
		check_call(cmd, shell=True, stdout=in_mem_file)
		in_mem_file.seek(0)
		stdout = in_mem_file.read()
		in_mem_file.close()
		return stdout
开发者ID:FomkaV,项目名称:wifi-arsenal,代码行数:10,代码来源:we.py

示例7: decode_bytes

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
def decode_bytes(bytes):
    temp = SpooledTemporaryFile()
    string = ""
    for byte in bytes:
        string = string + chr(byte)
    temp.write(string)
    temp.seek(0)  # work around a stupid python bug
    return Decode(0, temp.read(), Decode64Bits)
开发者ID:hndrewaall,项目名称:gbdb,代码行数:10,代码来源:disassemble.py

示例8: image

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
def image(type, spec=' ', ext='png'):

    # Parameters for `suml`.
    import suml.common
    import optparse
    options = optparse.Values(({
        'scruffy': True,
        'png': ext == 'png',
        'svg': ext == 'svg' or ext == 'pdf',
        'font': os.getenv('SCRUFFY_FONT', suml.common.defaultScruffyFont()),
        'shadow': False,
    }))

    from tempfile import SpooledTemporaryFile
    fout = SpooledTemporaryFile()

    # Execute Scruffy `suml`.
    if type == 'class':
        suml.yuml2dot.transform(spec, fout, options)
    elif type == 'sequence':
        suml.suml2pic.transform(spec, fout, options)
    else:
        return HTTPError(404, 'Unhandled diagram type.')

    # Retrieve the data generated.
    fout.seek(0)
    data = fout.read()
    fout.close()

    # Convert SVG to PDF?
    if ext == 'pdf':
        # Load SVG file.
        doc = xml.dom.expatbuilder.parseString(data)

        # Convert to a RLG drawing
        svg_renderer = svglib.svglib.SvgRenderer()
        svg_renderer.render(doc.documentElement)
        drawing = svg_renderer.finish()

        # Generate PDF.
        data = reportlab.graphics.renderPDF.drawToString(drawing)

    # Server the generated image.
    if ext == 'png':
        response.content_type = 'image/png'
    elif ext == 'svg':
        response.content_type = 'image/svg+xml'
    elif ext == 'pdf':
        response.content_type = 'application/pdf'
    else:
        return HTTPError(500, 'Unhandled extension type.')
    return data
开发者ID:wernight,项目名称:scruffy-server,代码行数:54,代码来源:server.py

示例9: StartFinish

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
class StartFinish(object):
    def __init__(self):
        pass

    def start(self, args):
        self.outFile = SpooledTemporaryFile()
        self.errFile = SpooledTemporaryFile()
        self.cmdline = list2cmdline(args)
        print 'starting: ' + self.cmdline
        self.process = Popen(args,
            stderr=self.errFile, stdout=self.outFile, universal_newlines=False)
        self.process_start = time()

    def finish(self, timeout):
        print 'finishing "' + self.cmdline + '"'
        kill_time = self.process_start + timeout
        self.process_end = time()
        while self.process.poll() is None:
            self.process_end = time()
            if self.process_end < kill_time:
                sleep(0.1)
            else:
                self.process.kill()
                raise Exception('timeout "' + self.cmdline + '"')
        # read temp streams from start
        self.outFile.seek(0)
        self.errFile.seek(0)
        self.out = self.outFile.read()
        self.err = self.errFile.read()
        self.outFile.close()
        self.errFile.close()
        print 'out:', self.out
        print 'err:', self.err
        print 'returncode:', self.process.returncode
        print 'elapsed:', self.process_end - self.process_start
        return self.process.returncode
开发者ID:GRay63,项目名称:TestAppGUI,代码行数:38,代码来源:startfinish.py

示例10: csv_fn

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
 def csv_fn(pydata):
     csvfile=SpooledTemporaryFile()
     csvw=csv.writer(csvfile)
     fieldnames=[]
     for h in pydata:
         for k in pydata[h]:
             if k not in fieldnames:
                 fieldnames.append(k)
     if showheader:
         csvw.writerow(['system_name'] + fieldnames)
     for system_name in pydata:
         csvw.writerow([system_name] + [pydata[system_name].get(k, None) for k in fieldnames])
     csvfile.seek(0)
     results=csvfile.read()
     csvfile.close()
     return results
开发者ID:dmf24,项目名称:pyscripting,代码行数:18,代码来源:get-cobbler-vars.py

示例11: run

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
def run(args, timeout = 10):
    '''
    Run a command with a timeout after which it will be forcibly
    killed.
    '''
    out_file = SpooledTemporaryFile()
    p = Popen(args, shell=True, stdout=out_file, stderr=STDOUT)
    wait_remaining_sec = timeout
    while p.poll() is None and wait_remaining_sec > 0:
        sleep(1)
        wait_remaining_sec -= 1
    if wait_remaining_sec <= 0:
        kill_proc(p.pid)
        r = -9
    else:
        r = p.returncode
    out_file.seek(0)
    out = out_file.read()
    return out, r
开发者ID:certik,项目名称:lab-dev,代码行数:21,代码来源:util.py

示例12: _parseStackTrace

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
class LLDB:
  def _parseStackTrace(self, jibberish):
    not_jibberish = re.findall(r'\(lldb\) bt(.*)\(lldb\)', jibberish, re.DOTALL)
    if len(not_jibberish) != 0:
      return not_jibberish[0].replace(' frame ', '')
    else:
      return 'Stack Trace failed:', jibberish

  def _waitForResponse(self, wait=True):
    while wait:
      self.lldb_stdout.seek(self.last_position)
      for line in self.lldb_stdout:
        if line == '(lldb) ':
          self.last_position = self.lldb_stdout.tell()
          return True
      time.sleep(0.05)
    time.sleep(0.05)
    return True

  def getStackTrace(self, pid):
    lldb_commands = [ 'attach -p ' + pid + '\n', 'bt\n', 'quit\n', 'Y\n' ]
    self.lldb_stdout = SpooledTemporaryFile()
    self.last_position = 0
    lldb_process = subprocess.Popen(['lldb', '-x'], stdin=subprocess.PIPE, stdout=self.lldb_stdout, stderr=self.lldb_stdout)
    while lldb_process.poll() == None:
      for command in lldb_commands:
        if command == lldb_commands[-1]:
          lldb_commands = []
          if self._waitForResponse(False):
            # I have seen LLDB exit out from under us
            try:
              lldb_process.stdin.write(command)
            except:
              pass
        elif self._waitForResponse():
          lldb_process.stdin.write(command)
    self.lldb_stdout.seek(0)
    stack_trace = self._parseStackTrace(self.lldb_stdout.read())
    self.lldb_stdout.close()
    return stack_trace
开发者ID:GaZ3ll3,项目名称:moose,代码行数:42,代码来源:memory_logger.py

示例13: Buffer

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
class Buffer(FileWrapper):
    """Class implementing buffering of input and output streams.
    
    This class uses a separate buffer file to hold the contents of the
    underlying file while they are being manipulated.  As data is read
    it is duplicated into the buffer, and data is written from the buffer
    back to the file on close.
    """

    def __init__(self, fileobj, mode=None, max_size_in_memory=1024 * 8):
        """Buffered file wrapper constructor."""
        self._buffer = SpooledTemporaryFile(max_size=max_size_in_memory)
        self._in_eof = False
        self._in_pos = 0
        self._was_truncated = False
        super(Buffer, self).__init__(fileobj, mode)

    def _buffer_size(self):
        try:
            return len(self._buffer.file.getvalue())
        except AttributeError:
            return os.fstat(self._buffer.fileno()).st_size

    def _buffer_chunks(self):
        chunk = self._buffer.read(16 * 1024)
        if chunk == "":
            yield chunk
        else:
            while chunk != "":
                yield chunk
                chunk = self._buffer.read(16 * 1024)

    def _write_out_buffer(self):
        if self._check_mode("r"):
            self._read_rest()
            if "a" in self.mode:
                self._buffer.seek(self._in_pos)
                self._fileobj.seek(self._in_pos)
            else:
                self._fileobj.seek(0)
                self._buffer.seek(0)
        else:
            self._buffer.seek(0)
        if self._was_truncated:
            self._fileobj.truncate(0)
            self._was_truncated = False
        for chunk in self._buffer_chunks():
            self._fileobj.write(chunk)

    def flush(self):
        # flush the buffer; we only write to the underlying file on close
        self._buffer.flush()

    def close(self):
        if self.closed:
            return
        if self._check_mode("w"):
            self._write_out_buffer()
        super(Buffer, self).close()
        self._buffer.close()

    def _read(self, sizehint=-1):
        #  First return any data available from the buffer.
        #  Since we don't flush the buffer after every write, certain OSes
        #  (guess which!) will happily read junk data from the end of it.
        #  Instead, we explicitly read only up to self._in_pos.
        if not self._in_eof:
            buffered_size = self._in_pos - self._buffer.tell()
            if sizehint >= 0:
                buffered_size = min(sizehint, buffered_size)
        else:
            buffered_size = sizehint
        data = self._buffer.read(buffered_size)
        if data != "":
            return data
        # Then look for more data in the underlying file
        if self._in_eof:
            return None
        data = self._fileobj.read(sizehint)
        self._in_pos += len(data)
        self._buffer.write(data)
        if sizehint < 0 or len(data) < sizehint:
            self._in_eof = True
            self._buffer.flush()
        return data

    def _write(self, data, flushing=False):
        self._buffer.write(data)
        if self._check_mode("r") and not self._in_eof:
            diff = self._buffer.tell() - self._in_pos
            if diff > 0:
                junk = self._fileobj.read(diff)
                self._in_pos += len(junk)
                if len(junk) < diff:
                    self._in_eof = True
                    self._buffer.flush()

    def _seek(self, offset, whence):
        # Ensure we've read enough to simply do the seek on the buffer
        if self._check_mode("r") and not self._in_eof:
#.........这里部分代码省略.........
开发者ID:TimothyFitz,项目名称:filelike,代码行数:103,代码来源:buffer.py

示例14: __init__

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
class IncrementalWriter:
    """A streaming file writer for point clouds.

    Using the IncrementalWriter with spooled temporary files, which are
    only flushed to disk if they go above the given size, allows for
    streaming points to disk even when the header is unknown in advance.
    This allows some nice tricks, including splitting a point cloud into
    multiple files in a single pass, without memory issues.
    """
    # pylint:disable=too-few-public-methods

    def __init__(self, filename: str, header: PlyHeader,
                 utm: UTM_Coord=None, buffer=2**22) -> None:
        """
        Args:
            filename: final place to save the file on disk.
            source_fname: source file for the pointcloud; used to detect
                file format for metadata etc.
            buffer (int): The number of bytes to hold in RAM before flushing
                the temporary file to disk.  Default 1MB, which holds ~8300
                points - enough for most objects but still practical to hold
                thousands in memory.  Set a smaller buffer for large forests.
        """
        self.filename = filename
        self.temp_storage = SpooledTemporaryFile(max_size=buffer, mode='w+b')
        self.count = 0
        self.utm = utm
        self.header = header
        # Always write in big-endian mode; only store type information
        self.binary = struct.Struct('>' + header.form_str[1:])

    def __call__(self, point) -> None:
        """Add a single point to this pointcloud, saving in binary format.

        Args:
            point (namedtuple): vertex attributes for the point, eg xyzrgba.
        """
        self.temp_storage.write(self.binary.pack(*point))
        self.count += 1

    def __del__(self):
        """Flush data to disk and clean up."""
        to_ply_types = {v: k for k, v in PLY_TYPES.items()}
        properties = ['property {t} {n}'.format(t=t, n=n) for t, n in zip(
            (to_ply_types[p] for p in self.header.form_str[1:]),
            self.header.names)]
        head = ['ply',
                'format binary_big_endian 1.0',
                'element vertex {}'.format(self.count),
                '\n'.join(properties),
                'end_header']
        if self.utm is not None:
            head.insert(-1, 'comment UTM x y zone north ' +
                        '{0.x} {0.y} {0.zone} {0.north}'.format(self.utm))
        if not os.path.isdir(os.path.dirname(self.filename)):
            os.makedirs(os.path.dirname(self.filename))
        with open(self.filename, 'wb') as f:
            f.write(('\n'.join(head) + '\n').encode('ascii'))
            self.temp_storage.seek(0)
            chunk = self.temp_storage.read(8192)
            while chunk:
                f.write(chunk)
                chunk = self.temp_storage.read(8192)
        self.temp_storage.close()
开发者ID:borevitzlab,项目名称:3D-tools,代码行数:66,代码来源:pointcloudfile.py

示例15: Buffer

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import read [as 别名]
class Buffer(FileWrapper):
    """Class implementing buffereing of input and output streams.
    
    This class uses a separate buffer file to hold the contents of the
    underlying file while they are being manipulated.  As data is read
    it is duplicated into the buffer, and data is written from the buffer
    back to the file on close.
    """
    
    def __init__(self,fileobj,mode=None,max_size_in_memory=1024*8):
        """Buffered file wrapper constructor."""
        self._buffer = SpooledTemporaryFile(max_size=max_size_in_memory)
        self._in_eof = False
        self._in_pos = 0
        super(Buffer,self).__init__(fileobj,mode)

    def _buffer_chunks(self):
        chunk = self._buffer.read(16*1024)
        if chunk == "":
            yield chunk
        else:
            while chunk != "":
                yield chunk
                chunk = self._buffer.read(16*1024)

    def _write_out_buffer(self):
        if self._check_mode("r"):
            self._read_rest()
            if "a" in self.mode:
                self._buffer.seek(self._in_pos)
                self._fileobj.seek(self._in_pos)
            else:
                self._fileobj.seek(0)
                self._buffer.seek(0)
        else:
            self._buffer.seek(0)
        for chunk in self._buffer_chunks():
            self._fileobj.write(chunk)
 
    def flush(self):
        # flush the buffer; we only write to the underlying file on close
        self._buffer.flush()

    def close(self):
        if self.closed:
            return
        if self._check_mode("w"):
            self._write_out_buffer()
        super(Buffer,self).close()
        self._buffer.close()

    def _read(self,sizehint=-1):
        #  First return any data available from the buffer.
        #  Since we don't flush the buffer after every write, certain OSes
        #  (guess which!) will happy read junk data from the end of it.
        #  Instead, we explicitly read only up to self._in_pos.
        if not self._in_eof:
            buffered_size = self._in_pos - self._buffer.tell()
            if sizehint >= 0:
                buffered_size = min(sizehint,buffered_size)
        else:
            buffered_size = sizehint
        data = self._buffer.read(buffered_size)
        if data != "":
            return data
        # Then look for more data in the underlying file
        if self._in_eof:
            return None
        data = self._fileobj.read(sizehint)
        self._in_pos += len(data)
        self._buffer.write(data)
        if sizehint < 0 or len(data) < sizehint:
            self._in_eof = True
            self._buffer.flush()
        return data

    def _write(self,data,flushing=False):
        self._buffer.write(data)
        if self._check_mode("r") and not self._in_eof:
            diff = self._buffer.tell() - self._in_pos
            if diff > 0:
                junk = self._fileobj.read(diff)
                self._in_pos += len(junk)
                if len(junk) < diff:
                    self._in_eof = True
                    self._buffer.flush()
    
    def _seek(self,offset,whence):
        # Ensure we've read enough to simply do the seek on the buffer
        if self._check_mode("r") and not self._in_eof:
            if whence == 0:
                if offset > self._in_pos:
                    self._read_rest()
            if whence == 1:
                if self._buffer.tell() + offset > self._in_pos:
                    self._read_rest()
            if whence == 2:
                self._read_rest()
        # Then just do it on the buffer...
        self._buffer.seek(offset,whence)
#.........这里部分代码省略.........
开发者ID:marvingreenberg,项目名称:filelike,代码行数:103,代码来源:buffer.py


注:本文中的tempfile.SpooledTemporaryFile.read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。