本文整理汇总了Python中django.utils.six.BytesIO.write方法的典型用法代码示例。如果您正苦于以下问题:Python BytesIO.write方法的具体用法?Python BytesIO.write怎么用?Python BytesIO.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.six.BytesIO
的用法示例。
在下文中一共展示了BytesIO.write方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_document_version_download_user_view
# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import write [as 别名]
def test_document_version_download_user_view(self):
self.login(
username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD
)
self.assertEqual(Document.objects.count(), 1)
response = self.post(
'documents:document_version_download', args=(
self.document.latest_version.pk,
)
)
self.assertEqual(response.status_code, 302)
self.role.permissions.add(
permission_document_download.stored_permission
)
response = self.post(
'documents:document_version_download', args=(
self.document.latest_version.pk,
)
)
self.assertEqual(response.status_code, 200)
buf = BytesIO()
buf.write(response.content)
self.assertEqual(
HASH_FUNCTION(buf.getvalue()), TEST_SMALL_DOCUMENT_CHECKSUM
)
del(buf)
示例2: test_logslice_api
# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import write [as 别名]
def test_logslice_api(test_repository, webapp, activate_responses, logname,
line_range, gzipped, num_loads):
job = Job.objects.create(repository=test_repository,
guid="12345", project_specific_id=1)
fake_log_url = 'http://www.fakelog.com/log.gz'
JobLog.objects.create(job=job, name=logname,
url=fake_log_url, status=JobLog.PARSED)
lines = ['cheezburger %s' % i for i in range(10)]
# set up a file response
text = "\n".join(lines) + '\n'
content = BytesIO()
if gzipped:
with gzip.GzipFile('none', 'w', fileobj=content) as gz:
gz.write(text)
else:
content.write(text)
content.seek(0)
responses.add(responses.GET, fake_log_url,
body=content.read(),
content_type="text/plain;charset=utf-8", status=200)
# now test it
for i in range(num_loads):
resp = webapp.get(reverse('logslice-list',
kwargs={"project": test_repository.name}) +
'?start_line={}&end_line={}&job_id=1'.format(line_range[0],
line_range[1]))
assert resp.json == [{'index': i + line_range[0], 'text': l + '\n'} for (i, l) in
enumerate(lines[line_range[0]:line_range[1]])]
示例3: generate
# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import write [as 别名]
def generate(source_file, storage, prefix=None, replace=False, fill=None):
"""
Creates favicons from a source file and upload into storage.
This also create the ieconfig.xml file.
:param source_file: File to use as string (local path) or filelike object
:type source_file: str or file
:param storage: Storage where upload files
:type storage: :class:`django.core.files.storage.Storage`
:param prefix: Prefix included in new files' names
:type prefix: str
:param replace: Delete file is already existing.
:type replace: bool
:param fill: Background color for generated precomposed-* icons
:type fill: tuple of length 3, as returned by PIL.ImageColor.getrgb(color)
"""
prefix = prefix or ''
def write_file(output_file, name, replace=False):
"""Upload to storage."""
name = prefix + name
if storage.exists(name):
if replace:
storage.delete(name)
else:
return
content = File(output_file, name)
storage._save(name, content)
def save_png(img, output_name, size):
img.thumbnail(size=size, resample=Image.ANTIALIAS)
output_file = BytesIO()
img.save(output_file, format='PNG')
write_file(output_file, output_name)
# Save ICO
img = Image.open(source_file)
output_file = BytesIO()
img.save(fp=output_file, format='ICO', sizes=ICO_SIZES)
write_file(output_file, 'favicon.ico')
# Save PNG
for size in PNG_SIZES:
img = Image.open(source_file)
save_png(img, 'favicon-%s.png' % size, (size, size))
for size, output_name in WINDOWS_PNG_SIZES:
img = Image.open(source_file)
save_png(img, output_name, size)
for size in FILLED_SIZES:
img = alpha_to_color(Image.open(source_file), fill)
save_png(img, 'favicon-precomposed-%s.png' % size, (size, size))
# Create ieconfig.xml
output_name = 'ieconfig.xml'
output_file = StringIO()
template = get_template('favicon/ieconfig.xml')
output_content = template.render({'tile_color': 'FFFFFF'})
output_file.write(output_content)
write_file(output_file, 'ieconfig.xml')
示例4: test_document_version_download
# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import write [as 别名]
def test_document_version_download(self):
with open(TEST_SMALL_DOCUMENT_PATH) as file_object:
document = self.document_type.new_document(file_object=File(file_object))
response = self.client.get(reverse("rest_api:documentversion-download", args=(document.latest_version.pk,)))
buf = BytesIO()
buf.write(response.content)
self.assertEqual(HASH_FUNCTION(buf.getvalue()), TEST_SMALL_DOCUMENT_CHECKSUM)
del (buf)
示例5: test_strips_underscore_headers
# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import write [as 别名]
def test_strips_underscore_headers(self):
"""WSGIRequestHandler ignores headers containing underscores.
This follows the lead of nginx and Apache 2.4, and is to avoid
ambiguity between dashes and underscores in mapping to WSGI environ,
which can have security implications.
"""
def test_app(environ, start_response):
"""A WSGI app that just reflects its HTTP environ."""
start_response('200 OK', [])
http_environ_items = sorted(
'%s:%s' % (k, v) for k, v in environ.items()
if k.startswith('HTTP_')
)
yield (','.join(http_environ_items)).encode('utf-8')
rfile = BytesIO()
rfile.write("GET / HTTP/1.0\r\n")
rfile.write("Some-Header: good\r\n")
rfile.write("Some_Header: bad\r\n")
rfile.write("Other_Header: bad\r\n")
rfile.seek(0)
# WSGIRequestHandler closes the output file; we need to make this a
# no-op so we can still read its contents.
class UnclosableBytesIO(BytesIO):
def close(self):
pass
wfile = UnclosableBytesIO()
def makefile(mode, *a, **kw):
if mode == 'rb':
return rfile
elif mode == 'wb':
return wfile
request = Stub(makefile=makefile)
server = Stub(base_environ={}, get_app=lambda: test_app)
# We don't need to check stderr, but we don't want it in test output
old_stderr = sys.stderr
sys.stderr = StringIO()
try:
# instantiating a handler runs the request as side effect
WSGIRequestHandler(request, '192.168.0.2', server)
finally:
sys.stderr = old_stderr
wfile.seek(0)
body = list(wfile.readlines())[-1]
self.assertEqual(body, 'HTTP_SOME_HEADER:good')
示例6: get
# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import write [as 别名]
def get(self, key):
""" Regenerates a MultiValueDict instance containing the files related to all file states
stored for the given key.
"""
upload = None
files_states = self.backend.get(key)
files = MultiValueDict()
if files_states:
for name, state in files_states.items():
f = BytesIO()
f.write(state['content'])
# If the post is too large, we cannot use a
# InMemoryUploadedFile instance.
if state['size'] > settings.FILE_UPLOAD_MAX_MEMORY_SIZE:
upload = TemporaryUploadedFile(
state['name'],
state['content_type'],
state['size'],
state['charset'],
)
upload.file = f
else:
f = BytesIO()
f.write(state['content'])
upload = InMemoryUploadedFile(
file=f,
field_name=name,
name=state['name'],
content_type=state['content_type'],
size=state['size'],
charset=state['charset'],
)
files[name] = upload
# Go to the first byte in the file for future use
upload.file.seek(0)
return files