当前位置: 首页>>代码示例>>Python>>正文


Python BytesIO.read方法代码示例

本文整理汇总了Python中django.utils.six.BytesIO.read方法的典型用法代码示例。如果您正苦于以下问题:Python BytesIO.read方法的具体用法?Python BytesIO.read怎么用?Python BytesIO.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.utils.six.BytesIO的用法示例。


在下文中一共展示了BytesIO.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_comment_file_upload_tmp_file

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
    def test_comment_file_upload_tmp_file(self):
        """
        Check (tmp) upload files are checked
        """
        utils.login(self)
        file = BytesIO(
            b'%PDF-1.0\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj 2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1'
            b'>>endobj 3 0 obj<</Type/Page/MediaBox[0 0 3 3]>>endobj\nxref\n0 4\n0000000000 65535 f\n000000'
            b'0010 00000 n\n0000000053 00000 n\n0000000102 00000 n\ntrailer<</Size 4/Root 1 0 R>>\nstartxre'
            b'f\n149\n%EOF\n')
        files = {
            'file': SimpleUploadedFile(
                'file_large.pdf', file.read(), content_type='application/pdf'),}
        response = self.client.post(
            reverse('spirit:comment:file-upload-ajax'),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest',
            data=files)

        res = json.loads(response.content.decode('utf-8'))
        file_url = os.path.join(
            settings.MEDIA_URL, 'spirit', 'files', str(self.user.pk), "fadcb2389bb2b69b46bc54185de0ae91.pdf"
        ).replace("\\", "/")
        self.assertEqual(res['url'], file_url)
        file_path = os.path.join(
            settings.MEDIA_ROOT, 'spirit', 'files', str(self.user.pk), "fadcb2389bb2b69b46bc54185de0ae91.pdf"
        )

        with open(file_path, 'rb') as fh:
            file.seek(0)
            self.assertEqual(fh.read(), file.read())

        shutil.rmtree(settings.MEDIA_ROOT)  # cleanup
开发者ID:nitely,项目名称:Spirit,代码行数:34,代码来源:tests.py

示例2: test_comment_file_upload_unique

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
    def test_comment_file_upload_unique(self):
        user_files_parts = ('spirit', 'files', str(self.user.pk))
        user_files_base = os.path.join(*user_files_parts)
        user_media = os.path.join(settings.MEDIA_ROOT, user_files_base)
        self.assertFalse(os.path.isdir(user_media))

        utils.login(self)
        pdf = BytesIO(
            b'%PDF-1.0\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj 2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1'
            b'>>endobj 3 0 obj<</Type/Page/MediaBox[0 0 3 3]>>endobj\nxref\n0 4\n0000000000 65535 f\n000000'
            b'0010 00000 n\n0000000053 00000 n\n0000000102 00000 n\ntrailer<</Size 4/Root 1 0 R>>\nstartxre'
            b'f\n149\n%EOF\n')
        file_name = 'foo.pdf'
        file = SimpleUploadedFile(
            file_name, pdf.read(), content_type='application/pdf')
        response = self.client.post(
            reverse('spirit:comment:file-upload-ajax'),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest',
            data={'file': file})
        res = json.loads(response.content.decode('utf-8'))

        self.assertTrue(os.path.isdir(user_media))

        url_parts = res['url'].split('/')
        self.assertEqual(
            url_parts[:-2],
            (settings.MEDIA_URL + '/'.join(user_files_parts)).split('/'))
        self.assertEqual(len(url_parts[-2]), 32)  # uuid
        self.assertEqual(url_parts[-1], file_name)

        self.assertEqual(len(os.listdir(user_media)), 1)
        self.assertTrue(os.path.join(
            user_media, os.listdir(user_media)[0], file_name))
        shutil.rmtree(settings.MEDIA_ROOT)  # cleanup
开发者ID:nitely,项目名称:Spirit,代码行数:36,代码来源:tests.py

示例3: SFTPStorageFile

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
class SFTPStorageFile(File):
    def __init__(self, name, storage, mode):
        self._name = name
        self._storage = storage
        self._mode = mode
        self._is_dirty = False
        self.file = BytesIO()
        self._is_read = False

    @property
    def size(self):
        if not hasattr(self, '_size'):
            self._size = self._storage.size(self._name)
        return self._size

    def read(self, num_bytes=None):
        if not self._is_read:
            self.file = self._storage._read(self._name)
            self._is_read = True

        return self.file.read(num_bytes)

    def write(self, content):
        if 'w' not in self._mode:
            raise AttributeError("File was opened for read-only access.")
        self.file = BytesIO(content)
        self._is_dirty = True
        self._is_read = True

    def close(self):
        if self._is_dirty:
            self._storage._save(self._name, self)
        self.file.close()
开发者ID:chenjyw,项目名称:django-storages,代码行数:35,代码来源:sftpstorage.py

示例4: test_logslice_api

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [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]])]
开发者ID:AnthonyMeaux,项目名称:treeherder,代码行数:33,代码来源:test_logslice_api.py

示例5: test_comment_image_upload_unique_no_duplication

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
    def test_comment_image_upload_unique_no_duplication(self):
        utils.login(self)
        img = BytesIO(
            b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00'
            b'\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;')
        image_name = 'foo_image.gif'
        file = SimpleUploadedFile(
            image_name, img.read(), content_type='image/gif')

        response = self.client.post(
            reverse('spirit:comment:image-upload-ajax'),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest',
            data={'image': file})
        res = json.loads(response.content.decode('utf-8'))
        first_url = res['url']

        utils.cache_clear()
        file.seek(0)
        response = self.client.post(
            reverse('spirit:comment:image-upload-ajax'),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest',
            data={'image': file})
        res = json.loads(response.content.decode('utf-8'))
        second_url = res['url']

        self.assertNotEqual(first_url, second_url)
开发者ID:nitely,项目名称:Spirit,代码行数:28,代码来源:tests.py

示例6: test_comment_image_upload

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
    def test_comment_image_upload(self):
        """
        Image upload
        """
        content = (
            b"GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00"
            b"\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;"
        )
        img = BytesIO(content)
        files = {"image": SimpleUploadedFile("image.gif", img.read(), content_type="image/gif")}

        form = CommentImageForm(user=self.user, data={}, files=files)
        self.assertTrue(form.is_valid())
        image = form.save()
        self.assertEqual(image.name, "bf21c3043d749d5598366c26e7e4ab44.gif")
        image_url = os.path.join(settings.MEDIA_URL, "spirit", "images", str(self.user.pk), image.name).replace(
            "\\", "/"
        )
        self.assertEqual(image.url, image_url)
        image_path = os.path.join(settings.MEDIA_ROOT, "spirit", "images", str(self.user.pk), image.name)
        self.assertTrue(os.path.isfile(image_path))

        with open(image_path, "rb") as fh:
            self.assertEqual(fh.read(), content)

        os.remove(image_path)
开发者ID:alesdotio,项目名称:Spirit,代码行数:28,代码来源:tests.py

示例7: test_comment_image_upload_unique

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
    def test_comment_image_upload_unique(self):
        user_images_parts = ('spirit', 'images', str(self.user.pk))
        user_images_base = os.path.join(*user_images_parts)
        user_media = os.path.join(settings.MEDIA_ROOT, user_images_base)
        self.assertFalse(os.path.isdir(user_media))

        utils.login(self)
        img = BytesIO(
            b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00'
            b'\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;')
        image_name = 'foo_image.gif'
        file = SimpleUploadedFile(
            image_name, img.read(), content_type='image/gif')
        response = self.client.post(
            reverse('spirit:comment:image-upload-ajax'),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest',
            data={'image': file})
        res = json.loads(response.content.decode('utf-8'))

        self.assertTrue(os.path.isdir(user_media))

        url_parts = res['url'].split('/')
        self.assertEqual(
            url_parts[:-2],
            (settings.MEDIA_URL + '/'.join(user_images_parts)).split('/'))
        self.assertEqual(len(url_parts[-2]), 32)  # uuid
        self.assertEqual(url_parts[-1], image_name)

        self.assertEqual(len(os.listdir(user_media)), 1)
        self.assertTrue(os.path.join(
            user_media, os.listdir(user_media)[0], image_name))
        shutil.rmtree(settings.MEDIA_ROOT)  # cleanup
开发者ID:nitely,项目名称:Spirit,代码行数:34,代码来源:tests.py

示例8: test_comment_image_upload

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
 def test_comment_image_upload(self):
     """
     comment image upload
     """
     utils.login(self)
     img = BytesIO(
         b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00'
         b'\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;')
     files = {'image': SimpleUploadedFile(
         'image.gif', img.read(), content_type='image/gif'), }
     response = self.client.post(
         reverse('spirit:comment:image-upload-ajax'),
         HTTP_X_REQUESTED_WITH='XMLHttpRequest',
         data=files)
     res = json.loads(response.content.decode('utf-8'))
     image_url = os.path.join(
         settings.MEDIA_URL, 'spirit', 'images', str(self.user.pk),
         "bf21c3043d749d5598366c26e7e4ab44.gif"
     ).replace("\\", "/")
     self.assertEqual(res['url'], image_url)
     image_path = os.path.join(
         settings.MEDIA_ROOT, 'spirit', 'images', str(self.user.pk),
         "bf21c3043d749d5598366c26e7e4ab44.gif"
     )
     self.assertTrue(os.path.isfile(image_path))
     shutil.rmtree(settings.MEDIA_ROOT)  # cleanup
开发者ID:nitely,项目名称:Spirit,代码行数:28,代码来源:tests.py

示例9: test_comment_file_upload_unique_no_duplication

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
    def test_comment_file_upload_unique_no_duplication(self):
        utils.login(self)
        pdf = BytesIO(
            b'%PDF-1.0\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj 2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1'
            b'>>endobj 3 0 obj<</Type/Page/MediaBox[0 0 3 3]>>endobj\nxref\n0 4\n0000000000 65535 f\n000000'
            b'0010 00000 n\n0000000053 00000 n\n0000000102 00000 n\ntrailer<</Size 4/Root 1 0 R>>\nstartxre'
            b'f\n149\n%EOF\n')
        file_name = 'foo.pdf'
        file = SimpleUploadedFile(
            file_name, pdf.read(), content_type='application/pdf')

        response = self.client.post(
            reverse('spirit:comment:file-upload-ajax'),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest',
            data={'file': file})
        res = json.loads(response.content.decode('utf-8'))
        first_url = res['url']

        utils.cache_clear()
        file.seek(0)
        response = self.client.post(
            reverse('spirit:comment:file-upload-ajax'),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest',
            data={'file': file})
        res = json.loads(response.content.decode('utf-8'))
        second_url = res['url']

        self.assertNotEqual(first_url, second_url)
开发者ID:nitely,项目名称:Spirit,代码行数:30,代码来源:tests.py

示例10: test_comment_image_upload_invalid

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
 def test_comment_image_upload_invalid(self):
     """
     Image upload with bad content but good extension should raise an error
     """
     img = BytesIO(b'bad\x00;')
     files = {'image': SimpleUploadedFile('image.gif', img.read(), content_type='image/gif'), }
     form = CommentImageForm(data={}, files=files)
     self.assertFalse(form.is_valid())
开发者ID:nitely,项目名称:Spirit,代码行数:10,代码来源:tests.py

示例11: test_comment_image_upload_invalid

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
 def test_comment_image_upload_invalid(self):
     """
     Image upload, bad image
     """
     img = BytesIO(b"bad\x00;")
     files = {"image": SimpleUploadedFile("image.gif", img.read(), content_type="image/gif")}
     form = CommentImageForm(data={}, files=files)
     self.assertFalse(form.is_valid())
开发者ID:alesdotio,项目名称:Spirit,代码行数:10,代码来源:tests.py

示例12: test_comment_image_upload_invalid

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
 def test_comment_image_upload_invalid(self):
     """
     Image upload, bad image
     """
     img = BytesIO(b'bad\x00;')
     files = {'image': SimpleUploadedFile('image.gif', img.read(), content_type='image/gif'), }
     form = CommentImageForm(data={}, files=files)
     self.assertFalse(form.is_valid())
开发者ID:beedesk,项目名称:django-spirit,代码行数:10,代码来源:tests.py

示例13: test_comment_image_upload_not_allowed_ext

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
 def test_comment_image_upload_not_allowed_ext(self):
     """
     Image upload with good mime but not allowed extension should raise an error
     """
     img = BytesIO(b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00'
                   b'\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;')
     files = {'image': SimpleUploadedFile('image.png', img.read(), content_type='image/png'), }
     form = CommentImageForm(user=self.user, data={}, files=files)
     self.assertFalse(form.is_valid())
开发者ID:nitely,项目名称:Spirit,代码行数:11,代码来源:tests.py

示例14: test_comment_image_upload_not_allowed_format

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
 def test_comment_image_upload_not_allowed_format(self):
     """
     Image upload, invalid format
     """
     img = BytesIO(b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00'
                   b'\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;')
     # fake png extension
     files = {'image': SimpleUploadedFile('image.png', img.read(), content_type='image/png'), }
     form = CommentImageForm(data={}, files=files)
     self.assertFalse(form.is_valid())
开发者ID:beedesk,项目名称:django-spirit,代码行数:12,代码来源:tests.py

示例15: test_comment_image_upload_no_extension

# 需要导入模块: from django.utils.six import BytesIO [as 别名]
# 或者: from django.utils.six.BytesIO import read [as 别名]
 def test_comment_image_upload_no_extension(self):
     """
     Image upload no extension
     """
     img = BytesIO(b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00'
                   b'\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;')
     files = {'image': SimpleUploadedFile('image', img.read(), content_type='image/gif'), }
     form = CommentImageForm(user=self.user, data={}, files=files)
     self.assertTrue(form.is_valid())
     image = form.save()
     self.assertEqual(image.name, "bf21c3043d749d5598366c26e7e4ab44.gif")
     os.remove(os.path.join(settings.MEDIA_ROOT, 'spirit', 'images', str(self.user.pk), image.name))
开发者ID:beedesk,项目名称:django-spirit,代码行数:14,代码来源:tests.py


注:本文中的django.utils.six.BytesIO.read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。