本文整理汇总了Python中django.utils.six.moves.cStringIO.getvalue方法的典型用法代码示例。如果您正苦于以下问题:Python cStringIO.getvalue方法的具体用法?Python cStringIO.getvalue怎么用?Python cStringIO.getvalue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.six.moves.cStringIO
的用法示例。
在下文中一共展示了cStringIO.getvalue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: output_results
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def output_results(profiler, options, stdout):
"""Generate the profiler output in the desired format. Implemented as a
separate function so it can be run as an exit handler (because management
commands often call exit() directly, bypassing the rest of the profile
command's handle() method)."""
profiler.create_stats()
if not options['sort']:
if not which('dot'):
stdout.write('Could not find "dot" from Graphviz; please install Graphviz to enable call graph generation')
return
if not which('gprof2dot.py'):
stdout.write('Could not find gprof2dot.py, which should have been installed by yet-another-django-profiler')
return
with tempfile.NamedTemporaryFile() as stats:
stats.write(marshal.dumps(profiler.stats))
stats.flush()
cmd = ('gprof2dot.py -f pstats {} | dot -Tpdf'.format(stats.name))
process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
output = process.communicate()[0]
return_code = process.poll()
if return_code:
stdout.write('gprof2dot/dot exited with {}'.format(return_code))
return
path = options['path']
with open(path, 'wb') as pdf_file:
pdf_file.write(output)
stdout.write('Wrote call graph to {}'.format(path))
else:
sort = options['sort']
if sort == 'file':
# Work around bug on Python versions >= 2.7.4
sort = 'fil'
out = StringIO()
stats = pstats.Stats(profiler, stream=out)
with mock.patch('pstats.func_strip_path') as mock_func_strip_path:
mock_func_strip_path.side_effect = func_strip_path
stats.strip_dirs()
restrictions = []
if options['pattern']:
restrictions.append(options['pattern'])
if options['fraction']:
restrictions.append(float(options['fraction']))
elif options['max_calls']:
restrictions.append(int(options['max_calls']))
elif not options['pattern']:
restrictions.append(.2)
stats.sort_stats(sort).print_stats(*restrictions)
if options['path']:
path = options['path']
with open(path, 'w') as text_file:
text_file.write(out.getvalue())
stdout.write('Wrote profiling statistics to {}'.format(path))
else:
stdout.write(out.getvalue())
示例2: printer_label
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def printer_label(sample):
"""Generate the PDF of a sample for the label printer.
:param sample: the sample the label of which should be generated
:type sample: `samples.models.Sample`
:return:
the PDF as a byte stream
:rtype: str
"""
output = StringIO()
text = sample.name
c = canvas.Canvas(output, pagesize=(width, height))
c.setAuthor("JuliaBase samples database")
c.setTitle(text)
c.setSubject("Label of {0} for the label printer".format(text))
try:
print_line(c, 0, fontsize, text)
except ExcessException:
first, second = best_split(text)
print_line(c, height / 2, fontsize_half, first, force=True)
print_line(c, 0, fontsize_half, second, force=True)
c.drawImage(ImageReader("http://chart.googleapis.com/chart?chs=116x116&cht=qr&chl={0}&chld=H|1".format(sample.id)),
width - height, 0, height, height)
c.showPage()
c.save()
return output.getvalue()
示例3: _run_command
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def _run_command(self, **options):
"""Run the profile command with the given options on the diffsettings command and capture the output"""
output = StringIO()
error = StringIO()
args = []
if django.VERSION[0] == 1 and django.VERSION[1] < 8:
options = options.copy()
options['backend'] = 'cProfile'
options['testing'] = True
for option in ('fraction', 'max_calls', 'path', 'pattern', 'sort'):
if option not in options:
options[option] = None
call_command('profile', 'diffsettings', stderr=error, stdout=output, **options)
expected = 'INSTALLED_APPS'
else:
for option in options:
if option in ('fraction', 'pattern', 'sort'):
args.append('--{}={}'.format(option, options[option]))
elif option == 'max_calls':
args.append('--max-calls={}'.format(options[option]))
elif option == 'path':
args.append('--output={}'.format(options[option]))
args.append('showmigrations')
args.append('--plan')
options = {'backend': 'cProfile', 'testing': True, 'stderr': error, 'stdout': output}
call_command('profile', *args, **options)
expected = '0001_initial'
text = output.getvalue()
assert expected in text
return text
示例4: _migrate_extension_models
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def _migrate_extension_models(self, ext_class):
"""Perform database migrations for an extension's models.
This will call out to Django Evolution to handle the migrations.
Args:
ext_class (djblets.extensions.extension.Extension):
The class for the extension to migrate.
"""
try:
from django_evolution.management.commands.evolve import \
Command as Evolution
except ImportError:
raise InstallExtensionError(
"Unable to migrate the extension's database tables. Django "
"Evolution is not installed.")
try:
stream = StringIO()
evolution = Evolution()
evolution.style = no_style()
evolution.execute(verbosity=0, interactive=False,
execute=True, hint=False,
compile_sql=False, purge=False,
database=False,
stdout=stream, stderr=stream)
output = stream.getvalue()
if output:
logging.info('Evolved extension models for %s: %s',
ext_class.id, stream.read())
stream.close()
except CommandError as e:
# Something went wrong while running django-evolution, so
# grab the output. We can't raise right away because we
# still need to put stdout back the way it was
output = stream.getvalue()
stream.close()
logging.error('Error evolving extension models: %s: %s',
e, output, exc_info=1)
load_error = self._store_load_error(ext_class.id, output)
raise InstallExtensionError(six.text_type(e), load_error)
示例5: render_markdown_from_file
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def render_markdown_from_file(f, **markdown_kwargs):
"""Render Markdown text from a file stream to HTML."""
s = StringIO()
markdownFromFile(input=f, output=s, **markdown_kwargs)
html = s.getvalue()
s.close()
return html
示例6: parse
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def parse(self):
"""
Parses the diff, returning a list of File objects representing each
file in the diff.
"""
self.files = []
i = 0
preamble = StringIO()
while i < len(self.lines):
next_i, file_info, new_diff = self._parse_diff(i)
if file_info:
if self.files:
self.files[-1].finalize()
self._ensure_file_has_required_fields(file_info)
file_info.prepend_data(preamble.getvalue())
preamble.close()
preamble = StringIO()
self.files.append(file_info)
elif new_diff:
# We found a diff, but it was empty and has no file entry.
# Reset the preamble.
preamble.close()
preamble = StringIO()
else:
preamble.write(self.lines[i])
preamble.write(b'\n')
i = next_i
try:
if self.files:
self.files[-1].finalize()
elif preamble.getvalue().strip() != b'':
# This is probably not an actual git diff file.
raise DiffParserError('This does not appear to be a git diff',
0)
finally:
preamble.close()
return self.files
示例7: process_response
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def process_response(self, request, response):
"""
Handler for processing a response. Dumps the profiling information
to the profile log file.
"""
timedloginfo = getattr(request, '_page_timedloginfo', None)
if timedloginfo:
timedloginfo.done()
if ('profiling' in request.GET and
getattr(settings, "LOGGING_ALLOW_PROFILING", False)):
init_profile_logger()
self.profiler.create_stats()
# Capture the stats
out = StringIO()
old_stdout, sys.stdout = sys.stdout, out
self.profiler.print_stats(1)
sys.stdout = old_stdout
profile_log = logging.getLogger("profile")
profile_log.log(logging.INFO,
"Profiling results for %s (HTTP %s):",
request.path, request.method)
profile_log.log(logging.INFO, out.getvalue().strip())
profile_log.log(logging.INFO,
'%d database queries made\n',
len(connection.queries))
queries = {}
for query in connection.queries:
sql = reformat_sql(query['sql'])
stack = ''.join(query['stack'][:-1])
time = query['time']
if sql in queries:
queries[sql].append((time, stack))
else:
queries[sql] = [(time, stack)]
times = {}
for sql, entries in six.iteritems(queries):
time = sum((float(entry[0]) for entry in entries))
tracebacks = '\n\n'.join((entry[1] for entry in entries))
times[time] = \
'SQL Query profile (%d times, %.3fs average)\n%s\n\n%s\n\n' % \
(len(entries), time / len(entries), sql, tracebacks)
sorted_times = sorted(six.iterkeys(times), reverse=1)
for time in sorted_times:
profile_log.log(logging.INFO, times[time])
return response
示例8: test_generate_key
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def test_generate_key(self):
stdout = StringIO()
try:
keygen.main(stdout=stdout, argv=[])
except SystemExit as exc:
self.assertEqual(exc.code, 0)
key = stdout.getvalue()
f = Fernet(key)
# Make sure this doesn't raise an error about a bad key.
f.decrypt(f.encrypt(b'whatever'))
示例9: process_response
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def process_response(self, request, response):
if settings.YADP_ENABLED and settings.YADP_PROFILE_PARAMETER in request.REQUEST:
if self.error:
return text_response(response, self.error)
self.profiler.create_stats()
mode = request.REQUEST[settings.YADP_PROFILE_PARAMETER]
if mode == 'file':
# Work around bug on Python versions >= 2.7.4
mode = 'fil'
if not mode:
if not which('dot'):
return text_response(response, 'Could not find "dot" from Graphviz; please install Graphviz to enable call graph generation')
if not which('gprof2dot.py'):
return text_response(response, 'Could not find gprof2dot.py, which should have been installed by yet-another-django-profiler')
with tempfile.NamedTemporaryFile() as stats:
stats.write(marshal.dumps(self.profiler.stats))
stats.flush()
cmd = ('gprof2dot.py -f pstats {} | dot -Tpdf'.format(stats.name))
process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
output = process.communicate()[0]
return_code = process.poll()
if return_code:
raise Exception('gprof2dot/dot exited with {}'.format(return_code))
response.content = output
response['Content-Type'] = 'application/pdf'
return response
elif mode == 'help':
return text_response(response, ProfilerMiddleware.__doc__)
else:
out = StringIO()
stats = pstats.Stats(self.profiler, stream=out)
with mock.patch('pstats.func_strip_path') as mock_func_strip_path:
mock_func_strip_path.side_effect = func_strip_path
stats.strip_dirs()
restrictions = []
if settings.YADP_PATTERN_PARAMETER in request.REQUEST:
restrictions.append(request.REQUEST[settings.YADP_PATTERN_PARAMETER])
if settings.YADP_FRACTION_PARAMETER in request.REQUEST:
restrictions.append(float(request.REQUEST[settings.YADP_FRACTION_PARAMETER]))
elif settings.YADP_MAX_CALLS_PARAMETER in request.REQUEST:
restrictions.append(int(request.REQUEST[settings.YADP_MAX_CALLS_PARAMETER]))
elif settings.YADP_PATTERN_PARAMETER not in request.REQUEST:
restrictions.append(.2)
try:
stats.sort_stats(mode).print_stats(*restrictions)
except KeyError:
# Bad parameter for sorting stats
return text_response(response, "Bad parameter passed for sorting statistics.\n" + ProfilerMiddleware.__doc__)
return html_response(request, response, out.getvalue())
return response
示例10: _migrate_extension_models
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def _migrate_extension_models(self, ext_class):
"""Perform database migrations for an extension's models.
This will call out to Django Evolution to handle the migrations.
Args:
ext_class (djblets.extensions.extension.Extension):
The class for the extension to migrate.
"""
if django_evolution is None:
# Django Evolution isn't installed. Extensions with evolutions
# are not supported.
return
try:
stream = StringIO()
call_command('evolve',
verbosity=0,
interactive=False,
execute=True,
stdout=stream,
stderr=stream)
output = stream.getvalue()
if output:
logger.info('Evolved extension models for %s: %s',
ext_class.id, stream.read())
stream.close()
except CommandError as e:
# Something went wrong while running django-evolution, so
# grab the output. We can't raise right away because we
# still need to put stdout back the way it was
output = stream.getvalue()
stream.close()
logger.exception('Error evolving extension models: %s: %s',
e, output)
load_error = self._store_load_error(ext_class.id, output)
raise InstallExtensionError(six.text_type(e), load_error)
示例11: render_markdown_from_file
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def render_markdown_from_file(f):
"""Renders Markdown text to HTML.
The Markdown text will be sanitized to prevent injecting custom HTML.
It will also enable a few plugins for code highlighting and sane lists.
"""
s = StringIO()
markdownFromFile(input=f, output=s, **MARKDOWN_KWARGS)
html = s.getvalue()
s.close()
return html
示例12: _run_command
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def _run_command(self, **options):
"""Run the profile command with the given options on the diffsettings command and capture the output"""
output = StringIO()
options = options.copy()
options['backend'] = 'cProfile'
options['testing'] = True
for option in ('fraction', 'max_calls', 'path', 'pattern', 'sort'):
if option not in options:
options[option] = None
call_command('profile', 'diffsettings', stdout=output, **options)
text = output.getvalue()
assert 'INSTALLED_APPS' in text
return text
示例13: encode
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def encode(self, o, *args, **kwargs):
self.level = 0
self.doIndent = False
stream = StringIO()
self.xml = XMLGenerator(stream, settings.DEFAULT_CHARSET)
self.xml.startDocument()
self.startElement("rsp")
self.__encode(o, *args, **kwargs)
self.endElement("rsp")
self.xml.endDocument()
self.xml = None
return stream.getvalue()
示例14: process_response
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
def process_response(self, request, response):
if self.profile_parameter is not None:
if self.error:
return text_response(response, self.error)
self.profiler.create_stats()
mode = self.profile_parameter
if mode == 'file':
# Work around bug on Python versions >= 2.7.4
mode = 'fil'
if not mode:
if not which('dot'):
return text_response(response, _('Could not find "dot" from Graphviz; please install Graphviz to enable call graph generation'))
if not which('gprof2dot.py'):
return text_response(response, _('Could not find gprof2dot.py, which should have been installed by yet-another-django-profiler'))
return_code, output = run_gprof2dot(self.profiler)
if return_code:
raise Exception(_('gprof2dot.py exited with {return_code}').format(return_code=return_code))
set_content(response, output)
response['Content-Type'] = 'application/pdf'
return response
elif mode == 'help':
return text_response(response, ProfilerMiddleware.__doc__)
else:
out = StringIO()
stats = pstats.Stats(self.profiler, stream=out)
with mock.patch('pstats.func_strip_path') as mock_func_strip_path:
mock_func_strip_path.side_effect = func_strip_path
stats.strip_dirs()
restrictions = []
if self.pattern_parameter is not None:
restrictions.append(self.pattern_parameter)
if self.fraction_parameter is not None:
restrictions.append(float(self.fraction_parameter))
elif self.max_calls_parameter is not None:
restrictions.append(int(self.max_calls_parameter))
elif self.pattern_parameter is None:
restrictions.append(.2)
stats.sort_stats(mode).print_stats(*restrictions)
return text_response(response, out.getvalue())
return response
示例15: FileStream
# 需要导入模块: from django.utils.six.moves import cStringIO [as 别名]
# 或者: from django.utils.six.moves.cStringIO import getvalue [as 别名]
class FileStream(object):
"""File stream for streaming reponses
This buffer intended for use as an argument to StreamingHTTPResponse
and also as a file for TarFile to write into.
Files are read in by chunks and written to this buffer through TarFile.
When there is content to be read from the buffer, it is taken up by
StreamingHTTPResponse and the buffer is cleared to prevent storing large
chunks of data in memory.
"""
def __init__(self):
self.buffer = StringIO()
self.offset = 0
def write(self, s):
"""Write ``s`` to the buffer and adjust the offset."""
self.buffer.write(s)
self.offset += len(s)
def tell(self):
"""Return the current position of the buffer."""
return self.offset
def close(self):
"""Close the buffer."""
self.buffer.close()
def pop(self):
"""Return the current contents of the buffer then clear it."""
s = self.buffer.getvalue()
self.buffer.close()
self.buffer = StringIO()
return s