本文整理汇总了Python中six.StringIO.readline方法的典型用法代码示例。如果您正苦于以下问题:Python StringIO.readline方法的具体用法?Python StringIO.readline怎么用?Python StringIO.readline使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.StringIO
的用法示例。
在下文中一共展示了StringIO.readline方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_simple_cmd_with_input_fileobject_and_redirected_output_fileobject
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import readline [as 别名]
def test_simple_cmd_with_input_fileobject_and_redirected_output_fileobject(self):
out_buff = StringIO()
in_buff = StringIO()
in_buff.write(self.PHRASE)
assert not os.path.exists(self.IN_FILE)
in_buff.seek(0)
assert in_buff.readline().strip("\n") == self.PHRASE
ret = ProcessHelper.run_subprocess(self.CAT_COMMAND, input=in_buff, output=out_buff)
in_buff.close()
assert ret == 0
assert not os.path.exists(self.OUT_FILE)
out_buff.seek(0)
assert out_buff.readline().strip("\n") == self.PHRASE
out_buff.close()
示例2: test_simple_cmd_with_redirected_output_fileobject
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import readline [as 别名]
def test_simple_cmd_with_redirected_output_fileobject(self):
buff = StringIO()
ret = ProcessHelper.run_subprocess(self.ECHO_COMMAND, output=buff)
assert ret == 0
assert not os.path.exists(self.OUT_FILE)
assert buff.readline().strip("\n") == self.PHRASE
buff.close()
示例3: __init__
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import readline [as 别名]
def __init__(self, server):
self.host, self.port = server.split(':')
try:
stat = self.send_cmd('stat\n')
envi = self.send_cmd('envi\n')
sio = StringIO(stat)
line = sio.readline()
if 'not currently serving requests' in line:
raise Exception("This ZooKeeper instance is not currently serving requests")
except socket.error:
self.mode = "Unreachable"
self.sessions = []
self.version = "Unknown"
return
except Exception as e:
traceback.print_exc()
self.mode = "Internal error"
self.sessions = []
self.version = "Unknown"
return
m = re.search('.*: (\d+\.\d+\.\d+)-.*', line)
self.version = m.group(1)
sio.readline()
self.sessions = []
for line in sio:
if not line.strip():
break
self.sessions.append(Session(line.strip()))
for line in sio:
attr, value = line.split(':')
attr = attr.strip().replace(" ", "_").replace("/", "_").lower()
self.__dict__[attr] = value.strip()
self.min_latency, self.avg_latency, self.max_latency = self.latency_min_avg_max.split("/")
self.envi = []
sio = StringIO(envi)
for line in sio:
if not line.strip(): break
attr, equ, value = line.partition("=")
if not equ: continue
self.envi.append((attr, value))
示例4: csv_reader
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import readline [as 别名]
def csv_reader(self, url, header=False, encoding=None, skip_rows=0, data=None, **kwargs):
if not data:
result = urlparse(url)
if result.scheme == 'ftp':
data = StringIO()
ftp = FTP(result.hostname)
ftp.login(result.username, result.password)
ftp.retrbinary('RETR {}'.format(result.path), lambda block: data.write(block.decode('utf-8')))
ftp.quit()
data.seek(0)
else:
response = self.get(url, **kwargs)
if encoding:
response.encoding = encoding
data = StringIO(response.text.strip())
if skip_rows:
for _ in range(skip_rows):
data.readline()
if header:
return csv.DictReader(data)
else:
return csv.reader(data)
示例5: decode
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import readline [as 别名]
def decode(text, *args, **kwargs):
"""Used by pypy and pylint to deal with a spec file"""
return_tuple = kwargs.get("return_tuple", True)
if six.PY3:
if hasattr(text, 'tobytes'):
text = text.tobytes().decode('utf8')
else:
text = text.decode('utf8')
buffered = StringIO(text)
# Determine if we need to have imports for this string
# It may be a fragment of the file
has_spec = regexes['encoding_matcher'].search(buffered.readline())
no_imports = not has_spec
buffered.seek(0)
# Translate the text
if six.PY2:
utf8 = encodings.search_function('utf8') # Assume utf8 encoding
reader = utf8.streamreader(buffered)
else:
reader = buffered
data = self.dealwith(reader.readline, no_imports=no_imports)
# If nothing was changed, then we want to use the original file/line
# Also have to replace indentation of original line with indentation of new line
# To take into account nested describes
if text and not regexes['only_whitespace'].match(text):
if regexes['whitespace'].sub('', text) == regexes['whitespace'].sub('', data):
bad_indentation = regexes['leading_whitespace'].search(text).groups()[0]
good_indentation = regexes['leading_whitespace'].search(data).groups()[0]
data = '%s%s' % (good_indentation, text[len(bad_indentation):])
# If text is empty and data isn't, then we should return text
if len(text) == 0 and len(data) == 1:
if return_tuple:
return "", 0
else:
return ""
# Return translated version and it's length
if return_tuple:
return data, len(data)
else:
return data
示例6: shell
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import readline [as 别名]
def shell(
name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
"""insert a shell command's raw output in a pre block, like::
| .. shell::
| :run_on_method: some.module.main
|
| mycmd --arg 1
Also:
| .. shell::
| :setup: some.module.setup
| :teardown: some.module.teardown
|
| mycmd --arg 1
"""
printable_cmd_parts = content
cmd = ' '.join([c.replace("\\", "") for c in content])
if options.get('setup'):
setup = get_object_from_path(options['setup'])
setup()
if options.get('run_on_method'):
main = get_object_from_path(options['run_on_method'])
def decode(s):
if isinstance(s, unicode):
s = str(s.decode())
return s
def unquot(s):
if s[0] in ('"', "'"):
s = s[1:-1]
return s
cmdlist = []
# get args with whitespace normalized:
for part in re.split(r'\s*', cmd.strip()):
part = decode(part)
part = unquot(part)
e = part.find('=')
if e != -1:
# i.e. --where="title='Dune'"
part = "%s=%s" % (part[:e], unquot(part[e+1:]))
cmdlist.append(part)
stdout = StringIO()
stderr = StringIO()
sys.stdout = stdout
sys.stderr = stderr
_program = sys.argv[0]
sys.argv[0] = cmdlist[0]
try:
try:
main(cmdlist[1:])
except SystemExit as e:
returncode = e.code
else:
returncode = 0
finally:
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
stdout.seek(0)
stderr.seek(0)
sys.argv[0] = _program
else:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, close_fds=True, shell=True)
returncode = p.wait()
stdout, stderr = p.stdout, p.stderr
if returncode != 0:
raise RuntimeError("%s\n%s (exit: %s)" % (
stderr.read(), cmd, returncode))
if options.get('teardown'):
setup = get_object_from_path(options['teardown'])
setup()
# just create a pre block and fill it with command output...
pad = " "
output = ["\n::\n\n"]
output.append(pad + "$ " + ("%s\n" % pad).join(printable_cmd_parts) + "\n")
while 1:
line = stdout.readline()
if not line:
break
output.append(pad + line)
output.append("\n")
output = "".join(output)
include_lines = statemachine.string2lines(output)
state_machine.insert_input(include_lines, None)
return []
示例7: NormalHTTPFile
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import readline [as 别名]
class NormalHTTPFile(HTTPFile):
def __init__(self,
path,
devid,
backup_dests=None,
mg=None,
fid=None,
cls=None,
key=None,
create_close_arg=None,
**kwds):
super(NormalHTTPFile, self).__init__(mg, fid, key, cls, create_close_arg)
if backup_dests is None:
backup_dests = []
self._fp = StringIO()
self._paths = [(devid, path)] + list(backup_dests)
self._is_closed = 0
def paths(self):
return self._paths
def read(self, n= -1):
return self._fp.read(n)
def readline(self, *args, **kwds):
return self._fp.readline(*args, **kwds)
def readlines(self, *args, **kwds):
return self._fp.readlines(*args, **kwds)
def write(self, content):
self._fp.write(content)
def close(self):
if not self._is_closed:
self._is_closed = True
# content = self._fp.getvalue()
# self._fp.close()
for tried_devid, tried_path in self._paths:
try:
# self._request(tried_path, "PUT", content)
self._fp.seek(0)
put.putfile(self._fp, tried_path)
devid = tried_devid
path = tried_path
break
except HTTPError as e:
continue
else:
devid = None
path = None
self._fp.seek(0, 2)
size = self._fp.tell()
self._fp.close()
if devid:
params = {
'fid' : self.fid,
'domain': self.mg.domain,
'key' : self.key,
'path' : path,
'devid' : devid,
'size' : size
}
if self.create_close_arg:
params.update(self.create_close_arg)
try:
self.mg.backend.do_request('create_close', params)
except MogileFSError as e:
if e.err != 'empty_file':
raise
def seek(self, pos, mode=0):
return self._fp.seek(pos, mode)
def tell(self):
return self._fp.tell()