mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-06 09:20:57 +01:00
some docs
This commit is contained in:
parent
83ecfd0578
commit
1a34c10a39
1 changed files with 37 additions and 0 deletions
|
@ -76,6 +76,9 @@ def RE_VT100_ESCAPE():
|
||||||
|
|
||||||
|
|
||||||
def populate_char_queue(reader, fd, queue):
|
def populate_char_queue(reader, fd, queue):
|
||||||
|
"""Reads single characters from a file descriptor into a queue.
|
||||||
|
If this ends or fails, it flags the calling reader object as closed.
|
||||||
|
"""
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
c = os.read(fd, 1)
|
c = os.read(fd, 1)
|
||||||
|
@ -90,8 +93,20 @@ def populate_char_queue(reader, fd, queue):
|
||||||
|
|
||||||
|
|
||||||
class NonBlockingFDReader:
|
class NonBlockingFDReader:
|
||||||
|
"""A class for reading characters from a file descriptor on a background
|
||||||
|
thread. This has the advantages that the calling thread can close the
|
||||||
|
file and that the reading does not block the calling thread.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, fd, timeout=None):
|
def __init__(self, fd, timeout=None):
|
||||||
|
"""
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
fd : int
|
||||||
|
A file descriptor
|
||||||
|
timeout : float or None, optional
|
||||||
|
The queue reading timeout.
|
||||||
|
"""
|
||||||
self.fd = fd
|
self.fd = fd
|
||||||
self.queue = queue.Queue()
|
self.queue = queue.Queue()
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
@ -103,6 +118,7 @@ class NonBlockingFDReader:
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
|
|
||||||
def read_char(self, timeout=None):
|
def read_char(self, timeout=None):
|
||||||
|
"""Reads a single character from the queue."""
|
||||||
timeout = timeout or self.timeout
|
timeout = timeout or self.timeout
|
||||||
try:
|
try:
|
||||||
return self.queue.get(block=timeout is not None,
|
return self.queue.get(block=timeout is not None,
|
||||||
|
@ -111,6 +127,7 @@ class NonBlockingFDReader:
|
||||||
return b''
|
return b''
|
||||||
|
|
||||||
def read(self, size=-1):
|
def read(self, size=-1):
|
||||||
|
"""Reads bytes from the file."""
|
||||||
i = 0
|
i = 0
|
||||||
buf = b''
|
buf = b''
|
||||||
while i != size:
|
while i != size:
|
||||||
|
@ -123,6 +140,7 @@ class NonBlockingFDReader:
|
||||||
return buf
|
return buf
|
||||||
|
|
||||||
def readline(self, size=-1):
|
def readline(self, size=-1):
|
||||||
|
"""Reads a line, or a partial line from the file descriptor."""
|
||||||
i = 0
|
i = 0
|
||||||
nl = b'\n'
|
nl = b'\n'
|
||||||
buf = b''
|
buf = b''
|
||||||
|
@ -138,6 +156,7 @@ class NonBlockingFDReader:
|
||||||
return buf
|
return buf
|
||||||
|
|
||||||
def readlines(self, hint=-1):
|
def readlines(self, hint=-1):
|
||||||
|
"""Reads lines from the file descriptor."""
|
||||||
lines = []
|
lines = []
|
||||||
while len(lines) != hint:
|
while len(lines) != hint:
|
||||||
line = self.readline(szie=-1, timeout=timeout)
|
line = self.readline(szie=-1, timeout=timeout)
|
||||||
|
@ -147,10 +166,16 @@ class NonBlockingFDReader:
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
def fileno(self):
|
def fileno(self):
|
||||||
|
"""Returns the file descriptor number."""
|
||||||
return self.fd
|
return self.fd
|
||||||
|
|
||||||
|
|
||||||
def populate_buffer(reader, fd, buffer, chunksize):
|
def populate_buffer(reader, fd, buffer, chunksize):
|
||||||
|
"""Reads bytes from the file descriptor and copies them into a buffer.
|
||||||
|
The reads happend in parallel, using pread(), and is thus only
|
||||||
|
availabe on posix. If the read fails for any reason, the reader is
|
||||||
|
flagged as closed.
|
||||||
|
"""
|
||||||
offset = 0
|
offset = 0
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
@ -167,8 +192,20 @@ def populate_buffer(reader, fd, buffer, chunksize):
|
||||||
|
|
||||||
|
|
||||||
class BufferedFDParallelReader:
|
class BufferedFDParallelReader:
|
||||||
|
"""Buffered, parallel background thread reader."""
|
||||||
|
|
||||||
def __init__(self, fd, buffer=None, chunksize=1024):
|
def __init__(self, fd, buffer=None, chunksize=1024):
|
||||||
|
"""
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
fd : int
|
||||||
|
File descriptor from which to read.
|
||||||
|
buffer : binary file-like or None, optional
|
||||||
|
A buffer to write bytes into. If None, a new BytesIO object
|
||||||
|
is created.
|
||||||
|
chunksize : int, optional
|
||||||
|
The max size of the parallel reads, default 1 kb.
|
||||||
|
"""
|
||||||
self.fd = fd
|
self.fd = fd
|
||||||
self.buffer = io.BytesIO() if buffer is None else buffer
|
self.buffer = io.BytesIO() if buffer is None else buffer
|
||||||
self.chunksize = chunksize
|
self.chunksize = chunksize
|
||||||
|
|
Loading…
Add table
Reference in a new issue