本文整理汇总了Python中six.moves.StringIO.read方法的典型用法代码示例。如果您正苦于以下问题:Python StringIO.read方法的具体用法?Python StringIO.read怎么用?Python StringIO.read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.StringIO
的用法示例。
在下文中一共展示了StringIO.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_inject_yum_mirrors
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
def test_inject_yum_mirrors(
orig_repos_cfg, mirrors_dict, expected_repos_cfg,
expected_repos_proxied_cfg
):
my_out_fil = StringIO()
inject_yum_mirrors(mirrors_dict, StringIO(orig_repos_cfg), my_out_fil)
my_out_fil.seek(0)
assert expected_repos_cfg == my_out_fil.read()
# Test when proxies are allowed
my_out_fil = StringIO()
inject_yum_mirrors(
mirrors_dict, StringIO(orig_repos_cfg), my_out_fil, True
)
my_out_fil.seek(0)
assert expected_repos_proxied_cfg == my_out_fil.read()
示例2: test_contradictory_date_entries_warn
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
def test_contradictory_date_entries_warn(self):
"""4.8.5.3 Emit warning on contradictory date entries."""
stream = StringIO(
wrap_document_text(construct_document_from(**{
"Author": {
"ForeName": "John",
"LastName": "Smith"
},
"DateCompleted": {
"Year": "2011",
"Month": "01",
"Day": "01"
},
"DateRevised": {
"Year": "2010",
"Month": "01",
"Day": "01"
},
}))
)
stderr = StringIO()
self.patch(sys, "stderr", stderr)
result = parsexml.parse_element_tree(
parsexml.file_to_element_tree(stream)
)
stderr.seek(0)
stderr_out = stderr.read()
self.assertThat(result["pubDate"], Is(None))
self.assertThat(result["reviseDate"], Is(None))
self.assertThat(stderr_out,
Contains("is greater than"))
示例3: __str__
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
def __str__(self):
stream = StringIO()
pprint.pprint(self.extractors, stream)
stream.seek(0)
template_data = stream.read()
if template_data:
return "%s[\n%s\n]" % (self.__class__.__name__, template_data)
return "%s[none]" % (self.__class__.__name__)
示例4: test_show
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
def test_show(self):
"""Establish that the show method will properly route to an
alternate file.
"""
sio = StringIO()
ex = TowerCLIError("Fe fi fo fum; I smell the blood of an Englishman.")
ex.show(file=sio)
sio.seek(0)
self.assertIn("Fe fi fo fum;", sio.read())
示例5: EncodedStringIO
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
class EncodedStringIO(object):
def __init__(self):
self._data = StringIO()
self.encoding = "ascii"
def read(self):
return self._data.read()
def write(self, data):
return self._data.write(data)
def flush(self):
self._data.flush()
示例6: push
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
def push(name, api, domain):
repo = git.Repo(os.getcwd())
branch = "temp-{}".format(str(uuid.uuid4())[:8])
set_deploy_branch(name, branch, api, domain)
remote = git_url(name, api, domain)
if is_dirty():
print("Nuking changes.")
git.reset(repo, "hard")
with TempBranch(branch, repo, delete=True):
for fname, file_info in openshift_files.items():
with open(fname, 'w') as f:
f.write(file_info.get("contents", ""))
repo.stage(fname)
repo.do_commit("Commit openshift files")
push_out = StringIO()
push_err = StringIO()
print("Pushing to openshift (may take a few minutes)")
git.push(repo, remote, "refs/heads/{}".format(branch),
outstream=push_out, errstream=push_err)
push_out.seek(0)
out = push_out.read()
if not re.match(r'^Push to .* successful.', out):
print("There was a failure while pushing")
print("---BEGIN STDERR---")
push_err.seek(0)
print(push_err.read())
print("---BEGIN STDOUT---")
print(out)
print("There was a failure while pushing")
git.rm(repo, openshift_files.keys())
map(os.remove, openshift_files.keys())
return get_app(name, api, domain)['app_url']
示例7: exceptions_csv
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
def exceptions_csv():
data = StringIO()
writer = csv.writer(data)
writer.writerow(["Count", "Message", "Traceback", "Nodes"])
for exc in six.itervalues(runners.locust_runner.exceptions):
nodes = ", ".join(exc["nodes"])
writer.writerow([exc["count"], exc["msg"], exc["traceback"], nodes])
data.seek(0)
response = make_response(data.read())
file_name = "exceptions_{0}.csv".format(time())
disposition = "attachment;filename={0}".format(file_name)
response.headers["Content-type"] = "text/csv"
response.headers["Content-disposition"] = disposition
return response
示例8: TestStreamStructure
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
class TestStreamStructure(TestInMemoryStructure):
def json(self, head):
return jl(
jd(
(q(self.LIST_TOKEN), jl(*(q(w) for w in self.LIST))),
(q(self.DICT_TOKEN), jd(*((q(k), q(v)) for k,v in self.DICT.items()))),
(q(self.HEAD_TOKEN), jd(*((q(k), q(v)) for k,v in self.HEAD.items()))),
),
q(head),
)
def get_read_store(self):
self.stream = StringIO(self.json(self.HEAD_TOKEN))
return structure.JSONStreamReadStructure(self.stream)
def get_write_store(self):
self.stream = StringIO()
return structure.JSONStreamWriteStructure(self.stream)
def verify_dict(self, received, expect):
tools.assert_equal(dict(expect), received)
def verify_list(self, received, expect):
tools.assert_equal(list(expect), received)
def verify_write(self, store, head):
# Nothing written to stream until close().
tools.assert_equal(0, self.stream.tell())
tools.assert_equal('', self.stream.read())
store.close()
self.stream.seek(0)
tools.assert_equal(self.json(head), self.stream.read())
def test_no_close(self):
store = self.get_read_store()
tools.assert_raises(AttributeError, getattr, store, 'close')
示例9: to
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
def to(cls, format, **options):
"""Serialize YANG container to the given output `format`.
"""
# pyang output plugins need an output stream
stream = StringIO()
plugin = PYANG_PLUGINS[format]
# register plugin options according to pyang script
optparser = OptionParser()
plugin.add_opts(optparser)
# pyang output plugins also need a pyang.Context
ctx = pyang.Context(DummyRepository())
# which offers plugin-specific options (just take defaults)
ctx.opts = optparser.parse_args([])[0]
# ready to serialize!
plugin.emit(ctx, [cls.to_statement(**options)], stream)
# and return the resulting data
stream.seek(0)
return stream.read()
示例10: _askYesNo
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
def _askYesNo(question=None):
message = StringIO()
while True:
askString = "\r%s? (yes|no): " % (question)
logging.debug("asking user: %s" % askString)
message.write(askString)
message.seek(0)
raw = raw_input(message.read())
if not len(raw):
continue
answer = raw[0].lower()
logging.debug("user answered read: %s" % (answer))
if answer not in 'yn':
continue
return answer == 'y'
示例11: StringIO
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
describe "mainline":
it "catches DelfickError errors and prints them nicely":
fle = StringIO()
class MyApp(App):
def execute(slf, args_obj, args_dict, extra_args, handler):
raise DelfickError("Well this should work", blah=1, _errors=[DelfickError("SubError", meh=2), DelfickError("SubError2", stuff=3)])
try:
MyApp().mainline([], print_errors_to=fle)
assert False, "This should have failed"
except SystemExit as error:
self.assertEqual(error.code, 1)
fle.flush()
fle.seek(0)
self.assertEqual(fle.read(), dedent("""
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Something went wrong! -- DelfickError
\t"Well this should work"\tblah=1
errors:
=======
\t"SubError"\tmeh=2
-------
\t"SubError2"\tstuff=3
-------
"""))
it "Converts KeyboardInterrupt into a UserQuit":
fle = StringIO()
class MyApp(App):
示例12: request
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
def request(self, method, path, contents, headers, decode_json=False,
stream=False, query=None, cdn=False):
"""
See :py:func:`swiftly.client.client.Client.request`
"""
if query:
path += '?' + '&'.join(
('%s=%s' % (quote(k), quote(v)) if v else quote(k))
for k, v in sorted(six.iteritems(query)))
reset_func = self._default_reset_func
if isinstance(contents, six.string_types):
contents = StringIO(contents)
tell = getattr(contents, 'tell', None)
seek = getattr(contents, 'seek', None)
if tell and seek:
try:
orig_pos = tell()
reset_func = lambda: seek(orig_pos)
except Exception:
tell = seek = None
elif not contents:
reset_func = lambda: None
status = 0
reason = 'Unknown'
attempt = 0
while attempt < self.attempts:
attempt += 1
if time() >= self.conn_discard:
self.storage_conn = None
self.cdn_conn = None
if cdn:
conn = self.cdn_conn
conn_path = self.cdn_path
else:
conn = self.storage_conn
conn_path = self.storage_path
if not conn:
parsed, conn = self._connect(cdn=cdn)
if conn:
if cdn:
self.cdn_conn = conn
self.cdn_path = conn_path = parsed.path
else:
self.storage_conn = conn
self.storage_path = conn_path = parsed.path
else:
raise self.HTTPException(
'%s %s failed: No connection' % (method, path))
self.conn_discard = time() + 4
titled_headers = dict((k.title(), v) for k, v in six.iteritems({
'User-Agent': self.user_agent,
'X-Auth-Token': self.auth_token}))
if headers:
titled_headers.update(
(k.title(), v) for k, v in six.iteritems(headers))
try:
if not hasattr(contents, 'read'):
if method not in self.no_content_methods and contents and \
'Content-Length' not in titled_headers and \
'Transfer-Encoding' not in titled_headers:
titled_headers['Content-Length'] = str(
len(contents or ''))
verbose_headers = ' '.join(
'%s: %s' % (k, v)
for k, v in sorted(six.iteritems(titled_headers)))
self.verbose(
'> %s %s %s', method, conn_path + path,
verbose_headers)
conn.request(
method, conn_path + path, contents, titled_headers)
else:
conn.putrequest(method, conn_path + path)
content_length = None
for h, v in sorted(six.iteritems(titled_headers)):
if h == 'Content-Length':
content_length = int(v)
conn.putheader(h, v)
if method not in self.no_content_methods and \
content_length is None:
titled_headers['Transfer-Encoding'] = 'chunked'
conn.putheader('Transfer-Encoding', 'chunked')
conn.endheaders()
verbose_headers = ' '.join(
'%s: %s' % (k, v)
for k, v in sorted(six.iteritems(titled_headers)))
self.verbose(
'> %s %s %s', method, conn_path + path,
verbose_headers)
if method not in self.no_content_methods and \
content_length is None:
chunk = contents.read(self.chunk_size)
while chunk:
conn.send('%x\r\n%s\r\n' % (len(chunk), chunk))
chunk = contents.read(self.chunk_size)
conn.send('0\r\n\r\n')
else:
left = content_length or 0
while left > 0:
size = self.chunk_size
if size > left:
#.........这里部分代码省略.........
示例13: _getInputFromUser
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
def _getInputFromUser(param):
"""
this private func reads the data from the user
for the given param
"""
loop = True
userInput = None
try:
if param.USE_DEFAULT:
logging.debug("setting default value (%s) for key (%s)" % (mask(param.DEFAULT_VALUE), param.CONF_NAME))
controller.CONF[param.CONF_NAME] = param.DEFAULT_VALUE
else:
while loop:
# If the value was not supplied by the command line flags
if param.CONF_NAME not in commandLineValues:
message = StringIO()
message.write(param.PROMPT)
val_list = param.VALIDATORS or []
if(validators.validate_regexp not in val_list
and param.OPTION_LIST):
message.write(" [%s]" % "|".join(param.OPTION_LIST))
if param.DEFAULT_VALUE:
message.write(" [%s] " % (str(param.DEFAULT_VALUE)))
message.write(": ")
message.seek(0)
# mask password or hidden fields
if (param.MASK_INPUT):
userInput = getpass.getpass("%s :" % (param.PROMPT))
else:
userInput = raw_input(message.read())
else:
userInput = commandLineValues[param.CONF_NAME]
# If DEFAULT_VALUE is set and user did not input anything
if userInput == "" and len(str(param.DEFAULT_VALUE)) > 0:
userInput = param.DEFAULT_VALUE
# Param processing
userInput = process_param_value(param, userInput)
# If param requires validation
try:
validate_param_value(param, userInput)
controller.CONF[param.CONF_NAME] = userInput
loop = False
except ParamValidationError:
if param.LOOSE_VALIDATION:
# If validation failed but LOOSE_VALIDATION is true, ask user
answer = _askYesNo("User input failed validation, "
"do you still wish to use it")
loop = not answer
if answer:
controller.CONF[param.CONF_NAME] = userInput
continue
else:
if param.CONF_NAME in commandLineValues:
del commandLineValues[param.CONF_NAME]
else:
# Delete value from commandLineValues so that we will prompt the user for input
if param.CONF_NAME in commandLineValues:
del commandLineValues[param.CONF_NAME]
loop = True
except KeyboardInterrupt:
# add the new line so messages wont be displayed in the same line as the question
print("")
raise
except:
logging.error(traceback.format_exc())
raise Exception(output_messages.ERR_EXP_READ_INPUT_PARAM % (param.CONF_NAME))
示例14: _object
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
def _object(self, method, container_name, object_name, contents, headers,
stream, query, cdn):
if cdn:
raise Exception('CDN not yet supported with LocalClient')
fs_container = _encode_name(container_name)
fs_object = _encode_name(object_name)
status = 503
reason = 'Internal Server Error'
hdrs = {}
body = ''
if method in ('GET', 'HEAD'):
local_path = path_join(self.local_path, fs_container, fs_object)
if not exists(local_path):
status = 404
reason = 'Not Found'
else:
content_length = getsize(local_path)
hdrs['content-length'] = str(content_length)
status = 200 if content_length else 204
if method == 'HEAD':
body = ''
else:
body = open(local_path, 'rb')
if not stream:
body = body.read()
elif method == 'PUT':
fs_object_path = path_join(
self.local_path, fs_container, fs_object)
temp_path = path_join(
self.local_path, fs_container, '_-temp' + uuid4().hex)
content_length = headers.get('content-length')
if content_length is not None:
content_length = int(content_length)
fp = open(temp_path, 'wb')
left = content_length
written = 0
while left is None or left > 0:
if left is not None:
chunk = contents.read(max(left, self.chunk_size))
left -= len(chunk)
else:
chunk = contents.read(self.chunk_size)
if not chunk:
break
fp.write(chunk)
written += len(chunk)
fp.flush()
fp.close()
if content_length is not None and written != content_length:
unlink(temp_path)
status = 503
reason = 'Internal Server Error'
body = 'Wrote %d bytes when Content-Length was %d' % (
written, content_length)
else:
db = self._get_db()
with lock_dir(self.local_path):
if isfile(fs_object_path):
rename(temp_path, fs_object_path)
db.execute('''
UPDATE object_entry
SET put_timestamp = ?, byte_count = ?
WHERE container_name = ? AND object_name = ?
''', (time(), written, container_name, object_name))
else:
rename(temp_path, fs_object_path)
db.execute('''
INSERT INTO object_entry (
container_name, object_name, put_timestamp,
byte_count)
VALUES (?, ?, ?, ?)
''', (container_name, object_name, time(), written))
db.commit()
status = 201
reason = 'Created'
body = ''
hdrs['content-length'] = str(len(body))
elif method == 'DELETE':
fs_object_path = path_join(
self.local_path, fs_container, fs_object)
if not isfile(fs_object_path):
status = 404
reason = 'Not Found'
else:
db = self._get_db()
with lock_dir(self.local_path):
if not isfile(fs_object_path):
status = 404
reason = 'Not Found'
else:
unlink(fs_object_path)
db.execute('''
DELETE FROM object_entry
WHERE container_name = ? AND object_name = ?
''', (container_name, object_name))
db.commit()
status = 204
reason = 'No Content'
body = ''
hdrs['content-length'] = str(len(body))
#.........这里部分代码省略.........
示例15: open
# 需要导入模块: from six.moves import StringIO [as 别名]
# 或者: from six.moves.StringIO import read [as 别名]
def open(self,f=None,mode='r',encrypt=False,sign=False):
"""write: encrypt =bool or list of recipients, sign = sender or bool(default_key)
read: encrypt = bool encrypted data expected, sign= expected key or True=defaultkey
"""
from six.moves import StringIO
from six import next, PY3, BytesIO
self.mode,self.encrypt,self.sign = mode,encrypt,sign
if self.required is True: self.required = self.columns
if encrypt or sign: assert self.gpg, 'gpg not intialized'
self.origfile = self.file = f
assert mode in ('r','w'), 'invalid mode'
if mode=='r':
if sign:
if sign is True:
fingerprint = self.gpg.default_key
if type(fingerprint) == tuple: fingerprint = fingerprint[0]
else:
if type(sign) == tuple: sign = sign[0]
fingerprint = self.gpg.find_key(sign)
assert fingerprint, "sender key not found"
if self.fileformat=='csv':
import re
if encrypt:
if PY3 and isinstance(f,StringIO):
result = self.gpg.decrypt_str(f.getvalue())
else:
result = self.gpg.decrypt_file(f)
assert result.ok, "decryption failed"
if sign: assert result.valid and result.fingerprint==fingerprint, 'invalid signature'
f = StringIO(str(result))
elif sign:
if PY3 and isinstance(f,StringIO):
result = self.gpg.verify_str(f.getvalue())
f = StringIO(self.gpg.without_signature(f.getvalue()))
else:
result = self.gpg.verify_file(f)
f.seek(0)
f = StringIO(self.gpg.without_signature(f.read()))
assert result.valid and result.fingerprint==fingerprint, 'invalid signature'
self.file = f
dialect = self.dialect
if not dialect:
pos = f.tell()
dialect = csv.Sniffer().sniff(f.read(1024))
f.seek(pos) # rewind
if not PY3:
import unicodecsv
reader = unicodecsv.reader
else:
reader = csv.reader
reader = reader(f,dialect=dialect)
preamble = next(reader)
assert len(preamble), 'invalid file format'
assert preamble[0]==self.dataformat, "file format not supported"
preamble = re.match(r'^(\d+).(\d+)',preamble[1])
assert int(preamble.group(2))<=self.version[0], "format version not supported"
fields = next(reader)
self.csv = reader
else: # self.fileformat in ('json','jsondict','json-file','jsondict-file'):
import json
if self.fileformat in ('json-file','jsondict-file'):
self.file = f = json.load(f)
data, encrypted, signed, result = json_decrypt(f,self.gpg)
assert data, 'invalid input'
if encrypt: assert encrypted==bool(encrypt), 'encryption expected'
if sign:
assert signed==bool(sign), 'signature expected'
assert result.valid and result.fingerprint==fingerprint, 'invalid signature'
assert 'format' in data and data['format']==self.dataformat, "file format not supported"
assert 'version' in data and data['version'][0]<=self.version[0], "file version not supported"
assert 'fields' in data , "fields missing"
fields = data['fields']
self.rows = data['data']
columns, unknown = [], []
for field in fields:
if field in self.columns: columns.append(field)
elif self.ignore: unknown.append(field)
else: assert False, "unknown field '%s'" % field
if self.required:
for field in self.required:
assert field in columns, "missing required field '%s'" % field
self.fields = fields
self.read_columns = (columns,unknown)
elif mode=='w':
assert self.fileformat in ('json','jsondict') or self.file, 'file missing'
if self.fileformat=='csv':
if encrypt or sign: self.file = StringIO()
else: self.file = f
if not PY3:
import unicodecsv
writer = unicodecsv.writer
else:
writer = csv.writer
self.csv = writer(self.file,lineterminator='\n',dialect=self.dialect)
self.csv.writerow((self.dataformat,'%i.%i' % tuple(self.version)))
self.csv.writerow(self.columns)
else: # self.fileformat in ('json','jsondict'):
self.rows = []