Python tempfile.SpooledTemporaryFile() Examples

The following are code examples for showing how to use tempfile.SpooledTemporaryFile(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 7 votes vote down vote up
def test_exports(self):
        # There are no surprising symbols in the tempfile module
        dict = tempfile.__dict__

        expected = {
            "NamedTemporaryFile" : 1,
            "TemporaryFile" : 1,
            "mkstemp" : 1,
            "mkdtemp" : 1,
            "mktemp" : 1,
            "TMP_MAX" : 1,
            "gettempprefix" : 1,
            "gettempdir" : 1,
            "tempdir" : 1,
            "template" : 1,
            "SpooledTemporaryFile" : 1
        }

        unexp = []
        for key in dict:
            if key[0] != '_' and key not in expected:
                unexp.append(key)
        self.assertTrue(len(unexp) == 0,
                        "unexpected keys: %s" % unexp) 
Example 2
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_properties(self):
        f = tempfile.SpooledTemporaryFile(max_size=10)
        f.write(b'x' * 10)
        self.assertFalse(f._rolled)
        self.assertEqual(f.mode, 'w+b')
        self.assertIsNone(f.name)
        with self.assertRaises(AttributeError):
            f.newlines
        with self.assertRaises(AttributeError):
            f.encoding

        f.write(b'x')
        self.assertTrue(f._rolled)
        self.assertEqual(f.mode, 'w+b')
        self.assertIsNotNone(f.name)
        with self.assertRaises(AttributeError):
            f.newlines
        with self.assertRaises(AttributeError):
            f.encoding 
Example 3
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 6 votes vote down vote up
def test_properties(self):
        f = tempfile.SpooledTemporaryFile(max_size=10)
        f.write(b'x' * 10)
        self.assertFalse(f._rolled)
        self.assertEqual(f.mode, 'w+b')
        self.assertIsNone(f.name)
        with self.assertRaises(AttributeError):
            f.newlines
        with self.assertRaises(AttributeError):
            f.encoding

        f.write(b'x')
        self.assertTrue(f._rolled)
        self.assertEqual(f.mode, 'rb+')
        self.assertIsNotNone(f.name)
        with self.assertRaises(AttributeError):
            f.newlines
        with self.assertRaises(AttributeError):
            f.encoding 
Example 4
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 6 votes vote down vote up
def test_text_newline_and_encoding(self):
        f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10,
                                          newline='', encoding='utf-8')
        f.write("\u039B\r\n")
        f.seek(0)
        self.assertEqual(f.read(), "\u039B\r\n")
        self.assertFalse(f._rolled)
        self.assertEqual(f.mode, 'w+')
        self.assertIsNone(f.name)
        self.assertIsNone(f.newlines)
        self.assertIsNone(f.encoding)

        f.write("\u039B" * 20 + "\r\n")
        f.seek(0)
        self.assertEqual(f.read(), "\u039B\r\n" + ("\u039B" * 20) + "\r\n")
        self.assertTrue(f._rolled)
        self.assertEqual(f.mode, 'w+')
        self.assertIsNotNone(f.name)
        self.assertIsNotNone(f.newlines)
        self.assertEqual(f.encoding, 'utf-8') 
Example 5
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 6 votes vote down vote up
def test_truncate_with_size_parameter(self):
        # A SpooledTemporaryFile can be truncated to zero size
        f = tempfile.SpooledTemporaryFile(max_size=10)
        f.write(b'abcdefg\n')
        f.seek(0)
        f.truncate()
        self.assertFalse(f._rolled)
        self.assertEqual(f._file.getvalue(), b'')
        # A SpooledTemporaryFile can be truncated to a specific size
        f = tempfile.SpooledTemporaryFile(max_size=10)
        f.write(b'abcdefg\n')
        f.truncate(4)
        self.assertFalse(f._rolled)
        self.assertEqual(f._file.getvalue(), b'abcd')
        # A SpooledTemporaryFile rolls over if truncated to large size
        f = tempfile.SpooledTemporaryFile(max_size=10)
        f.write(b'abcdefg\n')
        f.truncate(20)
        self.assertTrue(f._rolled)
        if has_stat:
            self.assertEqual(os.fstat(f.fileno()).st_size, 20) 
Example 6
Project: setka   Author: RomanovMikeV   File: Logger_test_list.py    MIT License 6 votes vote down vote up
def view_result(one_input, one_output):
    # print("In view result")
    img = one_input[0]
    img = (img - img.min()) / (img.max() - img.min())
    truth = one_input[1]
    label = one_output

    # print(img.size())

    fig = plt.figure()
    plt.imshow(img.permute(2, 1, 0))
    plt.close()

    signal = numpy.sin(numpy.linspace(0, 1000, 40000))

    file = tempfile.SpooledTemporaryFile()
    file.write(b'something')

    return {'figures': {'img': fig},
            'texts': {'img': 'Sample'},
            'images': {'img': (img * 255.0).int().numpy().astype('uint8')},
            'audios': {'img': signal},
            'files': {'img.sht': file}} 
Example 7
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 6 votes vote down vote up
def test_exports(self):
        # There are no surprising symbols in the tempfile module
        dict = tempfile.__dict__

        expected = {
            "NamedTemporaryFile" : 1,
            "TemporaryFile" : 1,
            "mkstemp" : 1,
            "mkdtemp" : 1,
            "mktemp" : 1,
            "TMP_MAX" : 1,
            "gettempprefix" : 1,
            "gettempdir" : 1,
            "tempdir" : 1,
            "template" : 1,
            "SpooledTemporaryFile" : 1
        }

        unexp = []
        for key in dict:
            if key[0] != '_' and key not in expected:
                unexp.append(key)
        self.assertTrue(len(unexp) == 0,
                        "unexpected keys: %s" % unexp) 
Example 8
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 6 votes vote down vote up
def test_properties(self):
        f = tempfile.SpooledTemporaryFile(max_size=10)
        f.write(b'x' * 10)
        self.assertFalse(f._rolled)
        self.assertEqual(f.mode, 'w+b')
        self.assertIsNone(f.name)
        with self.assertRaises(AttributeError):
            f.newlines
        with self.assertRaises(AttributeError):
            f.encoding

        f.write(b'x')
        self.assertTrue(f._rolled)
        self.assertEqual(f.mode, 'w+b')
        self.assertIsNotNone(f.name)
        with self.assertRaises(AttributeError):
            f.newlines
        with self.assertRaises(AttributeError):
            f.encoding 
Example 9
Project: strax   Author: AxFoundation   File: s3.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _save_chunk(self, data, chunk_info, executor=None):
        # Keyname
        key_name = f"{self.strax_unique_key}/{chunk_info['chunk_i']:06d}"

        # Save chunk via temporary file
        with tempfile.SpooledTemporaryFile() as f:
            filesize = strax.save_file(f,
                                       data=data,
                                       compressor=self.md['compressor'])
            f.seek(0)
            self.s3.upload_fileobj(f,
                                   BUCKET_NAME,
                                   key_name,
                                   Config=self.config)

        return dict(key_name=key_name, filesize=filesize), None 
Example 10
Project: SQLmap-Command-Builder   Author: kil-x   File: testing.py    GNU General Public License v3.0 6 votes vote down vote up
def initCase(switches, count):
    _failures.failedItems = []
    _failures.failedParseOn = None
    _failures.failedTraceBack = None

    paths.SQLMAP_OUTPUT_PATH = tempfile.mkdtemp(prefix="%s%d-" % (MKSTEMP_PREFIX.TESTING, count))
    paths.SQLMAP_DUMP_PATH = os.path.join(paths.SQLMAP_OUTPUT_PATH, "%s", "dump")
    paths.SQLMAP_FILES_PATH = os.path.join(paths.SQLMAP_OUTPUT_PATH, "%s", "files")

    logger.debug("using output directory '%s' for this test case" % paths.SQLMAP_OUTPUT_PATH)

    LOGGER_HANDLER.stream = sys.stdout = tempfile.SpooledTemporaryFile(max_size=0, mode="w+b", prefix="sqlmapstdout-")

    cmdLineOptions = cmdLineParser()

    if switches:
        for key, value in switches.items():
            if key in cmdLineOptions.__dict__:
                cmdLineOptions.__dict__[key] = value

    initOptions(cmdLineOptions, True)
    init() 
Example 11
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 6 votes vote down vote up
def test_properties(self):
        f = tempfile.SpooledTemporaryFile(max_size=10)
        f.write(b'x' * 10)
        self.assertFalse(f._rolled)
        self.assertEqual(f.mode, 'w+b')
        self.assertIsNone(f.name)
        with self.assertRaises(AttributeError):
            f.newlines
        with self.assertRaises(AttributeError):
            f.encoding

        f.write(b'x')
        self.assertTrue(f._rolled)
        self.assertEqual(f.mode, 'rb+')
        self.assertIsNotNone(f.name)
        with self.assertRaises(AttributeError):
            f.newlines
        with self.assertRaises(AttributeError):
            f.encoding 
Example 12
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 6 votes vote down vote up
def test_text_newline_and_encoding(self):
        f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10,
                                          newline='', encoding='utf-8')
        f.write("\u039B\r\n")
        f.seek(0)
        self.assertEqual(f.read(), "\u039B\r\n")
        self.assertFalse(f._rolled)
        self.assertEqual(f.mode, 'w+')
        self.assertIsNone(f.name)
        self.assertIsNone(f.newlines)
        self.assertIsNone(f.encoding)

        f.write("\u039B" * 20 + "\r\n")
        f.seek(0)
        self.assertEqual(f.read(), "\u039B\r\n" + ("\u039B" * 20) + "\r\n")
        self.assertTrue(f._rolled)
        self.assertEqual(f.mode, 'w+')
        self.assertIsNotNone(f.name)
        self.assertIsNotNone(f.newlines)
        self.assertEqual(f.encoding, 'utf-8') 
Example 13
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 6 votes vote down vote up
def test_truncate_with_size_parameter(self):
        # A SpooledTemporaryFile can be truncated to zero size
        f = tempfile.SpooledTemporaryFile(max_size=10)
        f.write(b'abcdefg\n')
        f.seek(0)
        f.truncate()
        self.assertFalse(f._rolled)
        self.assertEqual(f._file.getvalue(), b'')
        # A SpooledTemporaryFile can be truncated to a specific size
        f = tempfile.SpooledTemporaryFile(max_size=10)
        f.write(b'abcdefg\n')
        f.truncate(4)
        self.assertFalse(f._rolled)
        self.assertEqual(f._file.getvalue(), b'abcd')
        # A SpooledTemporaryFile rolls over if truncated to large size
        f = tempfile.SpooledTemporaryFile(max_size=10)
        f.write(b'abcdefg\n')
        f.truncate(20)
        self.assertTrue(f._rolled)
        if has_stat:
            self.assertEqual(os.fstat(f.fileno()).st_size, 20) 
Example 14
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def do_create(self, max_size=0, dir=None, pre="", suf=""):
        if dir is None:
            dir = tempfile.gettempdir()
        try:
            file = tempfile.SpooledTemporaryFile(max_size=max_size, dir=dir, prefix=pre, suffix=suf)
        except:
            self.failOnException("SpooledTemporaryFile")

        return file 
Example 15
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_basic(self):
        # SpooledTemporaryFile can create files
        f = self.do_create()
        self.assertFalse(f._rolled)
        f = self.do_create(max_size=100, pre="a", suf=".txt")
        self.assertFalse(f._rolled) 
Example 16
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_del_on_close(self):
        # A SpooledTemporaryFile is deleted when closed
        dir = tempfile.mkdtemp()
        try:
            f = tempfile.SpooledTemporaryFile(max_size=10, dir=dir)
            self.assertFalse(f._rolled)
            f.write('blat ' * 5)
            self.assertTrue(f._rolled)
            filename = f.name
            f.close()
            self.assertFalse(os.path.exists(filename),
                        "SpooledTemporaryFile %s exists after close" % filename)
        finally:
            os.rmdir(dir) 
Example 17
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_rewrite_small(self):
        # A SpooledTemporaryFile can be written to multiple within the max_size
        f = self.do_create(max_size=30)
        self.assertFalse(f._rolled)
        for i in range(5):
            f.seek(0, 0)
            f.write('x' * 20)
        self.assertFalse(f._rolled) 
Example 18
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_writelines(self):
        # Verify writelines with a SpooledTemporaryFile
        f = self.do_create()
        f.writelines((b'x', b'y', b'z'))
        f.seek(0)
        buf = f.read()
        self.assertEqual(buf, b'xyz') 
Example 19
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_writelines_sequential(self):
        # A SpooledTemporaryFile should hold exactly max_size bytes, and roll
        # over afterward
        f = self.do_create(max_size=35)
        f.writelines((b'x' * 20, b'x' * 10, b'x' * 5))
        self.assertFalse(f._rolled)
        f.write(b'x')
        self.assertTrue(f._rolled) 
Example 20
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_sparse(self):
        # A SpooledTemporaryFile that is written late in the file will extend
        # when that occurs
        f = self.do_create(max_size=30)
        self.assertFalse(f._rolled)
        f.seek(100, 0)
        self.assertFalse(f._rolled)
        f.write('x')
        self.assertTrue(f._rolled) 
Example 21
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_fileno(self):
        # A SpooledTemporaryFile should roll over to a real file on fileno()
        f = self.do_create(max_size=30)
        self.assertFalse(f._rolled)
        self.assertTrue(f.fileno() > 0)
        self.assertTrue(f._rolled) 
Example 22
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_multiple_close_before_rollover(self):
        # A SpooledTemporaryFile can be closed many times without error
        f = tempfile.SpooledTemporaryFile()
        f.write('abc\n')
        self.assertFalse(f._rolled)
        f.close()
        try:
            f.close()
            f.close()
        except:
            self.failOnException("close") 
Example 23
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_bound_methods(self):
        # It should be OK to steal a bound method from a SpooledTemporaryFile
        # and use it independently; when the file rolls over, those bound
        # methods should continue to function
        f = self.do_create(max_size=30)
        read = f.read
        write = f.write
        seek = f.seek

        write("a" * 35)
        write("b" * 35)
        seek(0, 0)
        self.assertTrue(read(70) == 'a'*35 + 'b'*35) 
Example 24
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_context_manager_before_rollover(self):
        # A SpooledTemporaryFile can be used as a context manager
        with tempfile.SpooledTemporaryFile(max_size=1) as f:
            self.assertFalse(f._rolled)
            self.assertFalse(f.closed)
        self.assertTrue(f.closed)
        def use_closed():
            with f:
                pass
        self.assertRaises(ValueError, use_closed) 
Example 25
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_context_manager_during_rollover(self):
        # A SpooledTemporaryFile can be used as a context manager
        with tempfile.SpooledTemporaryFile(max_size=1) as f:
            self.assertFalse(f._rolled)
            f.write('abc\n')
            f.flush()
            self.assertTrue(f._rolled)
            self.assertFalse(f.closed)
        self.assertTrue(f.closed)
        def use_closed():
            with f:
                pass
        self.assertRaises(ValueError, use_closed) 
Example 26
Project: pyblish-win   Author: pyblish   File: test_tempfile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_context_manager_after_rollover(self):
        # A SpooledTemporaryFile can be used as a context manager
        f = tempfile.SpooledTemporaryFile(max_size=1)
        f.write('abc\n')
        f.flush()
        self.assertTrue(f._rolled)
        with f:
            self.assertFalse(f.closed)
        self.assertTrue(f.closed)
        def use_closed():
            with f:
                pass
        self.assertRaises(ValueError, use_closed) 
Example 27
Project: ckanext-geonode   Author: geosolutions-it   File: geonode.py    GNU General Public License v2.0 5 votes vote down vote up
def _create_package(self, context, package_dict, harvest_object):

        # Resources with data to be downloaded will be added later
        # http://docs.ckan.org/en/ckan-2.2/api.html#ckan.logic.action.create.resource_create
        resources = package_dict.pop('resources', None)
        downloadable_resources = []
        normal_resources = []
        for resource in resources:
            if resource.get(RESOURCE_DOWNLOADER, None):
                downloadable_resources.append(resource)
            else:
                normal_resources.append(resource)

        if len(normal_resources):
            package_dict['resources'] = normal_resources

        package_id = p.toolkit.get_action('package_create')(context, package_dict)

        # Handle data downloads
        for resource in downloadable_resources:
            resource['package_id'] = package_id
            log.info('Handling download data for resource %s in package %s', resource['name'], package_id)
            downloader = resource.pop(RESOURCE_DOWNLOADER)

            with SpooledTemporaryFile(max_size=TEMP_FILE_THRESHOLD_SIZE) as f:
                fieldStorage = downloader.download(f)
                resource['upload'] = fieldStorage
                log.info('Create resource %s in package %s', resource['name'], package_id)
                created_resource = p.toolkit.get_action('resource_create')(context, resource)
                log.debug('Added resource %s to package %s with uuid %s', resource['name'], package_id, created_resource['id'])

        return package_id 
Example 28
Project: injuben   Author: injuben   File: in_juben.py    MIT License 5 votes vote down vote up
def preview():
    input = StringIO(request.form.get('in-juben-text'))
    has_scene_num = bool(request.form.get('in-has-scene-num'))
    first_page_number = bool(request.form.get('in-first-page-num'))
    strong_scene_heading = bool(request.form.get('in-strong-scene-heading'))
    in_lang = request.form.get('in-lang')
    tmp_file = tempfile.SpooledTemporaryFile()
    suffix = "pdf"
    try:
        if in_lang == 'zh':
            input = normalize.parse(input)
        filename = input.readline().replace("Title:", '').strip()
        input.seek(0)
        screenplay = fountain.parse(input)
        if in_lang == 'zh':
            to_pdf(screenplay, tmp_file._file, is_strong=strong_scene_heading, has_scene_num=has_scene_num, first_page_number=first_page_number)
        else:
            to_en_pdf(screenplay, tmp_file._file, is_strong=strong_scene_heading)
        tmp_file.seek(0)
        encoded_string = base64.b64encode(tmp_file.read()).decode('ascii')
        r = make_response('{"filename":"' + filename + '", "suffix":"' + suffix + '", "content":"' + encoded_string + '"}')
        r.headers['Content-Type'] = 'text/plain; charset=UTF-8'
        return r
    except:
        return locales().get('_')('Invalid Format'), 500
    finally:
        if tmp_file:
           tmp_file.close() 
Example 29
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_exports(self):
        # There are no surprising symbols in the tempfile module
        dict = tempfile.__dict__

        expected = {
            "NamedTemporaryFile" : 1,
            "TemporaryFile" : 1,
            "mkstemp" : 1,
            "mkdtemp" : 1,
            "mktemp" : 1,
            "TMP_MAX" : 1,
            "gettempprefix" : 1,
            "gettempprefixb" : 1,
            "gettempdir" : 1,
            "gettempdirb" : 1,
            "tempdir" : 1,
            "template" : 1,
            "SpooledTemporaryFile" : 1,
            "TemporaryDirectory" : 1,
        }

        unexp = []
        for key in dict:
            if key[0] != '_' and key not in expected:
                unexp.append(key)
        self.assertTrue(len(unexp) == 0,
                        "unexpected keys: %s" % unexp) 
Example 30
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def do_create(self, max_size=0, dir=None, pre="", suf=""):
        if dir is None:
            dir = tempfile.gettempdir()
        file = tempfile.SpooledTemporaryFile(max_size=max_size, dir=dir, prefix=pre, suffix=suf)

        return file 
Example 31
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_basic(self):
        # SpooledTemporaryFile can create files
        f = self.do_create()
        self.assertFalse(f._rolled)
        f = self.do_create(max_size=100, pre="a", suf=".txt")
        self.assertFalse(f._rolled) 
Example 32
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_del_on_close(self):
        # A SpooledTemporaryFile is deleted when closed
        dir = tempfile.mkdtemp()
        try:
            f = tempfile.SpooledTemporaryFile(max_size=10, dir=dir)
            self.assertFalse(f._rolled)
            f.write(b'blat ' * 5)
            self.assertTrue(f._rolled)
            filename = f.name
            f.close()
            self.assertFalse(isinstance(filename, str) and os.path.exists(filename),
                        "SpooledTemporaryFile %s exists after close" % filename)
        finally:
            os.rmdir(dir) 
Example 33
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_rewrite_small(self):
        # A SpooledTemporaryFile can be written to multiple within the max_size
        f = self.do_create(max_size=30)
        self.assertFalse(f._rolled)
        for i in range(5):
            f.seek(0, 0)
            f.write(b'x' * 20)
        self.assertFalse(f._rolled) 
Example 34
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_writelines(self):
        # Verify writelines with a SpooledTemporaryFile
        f = self.do_create()
        f.writelines((b'x', b'y', b'z'))
        f.seek(0)
        buf = f.read()
        self.assertEqual(buf, b'xyz') 
Example 35
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_writelines_sequential(self):
        # A SpooledTemporaryFile should hold exactly max_size bytes, and roll
        # over afterward
        f = self.do_create(max_size=35)
        f.writelines((b'x' * 20, b'x' * 10, b'x' * 5))
        self.assertFalse(f._rolled)
        f.write(b'x')
        self.assertTrue(f._rolled) 
Example 36
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_sparse(self):
        # A SpooledTemporaryFile that is written late in the file will extend
        # when that occurs
        f = self.do_create(max_size=30)
        self.assertFalse(f._rolled)
        f.seek(100, 0)
        self.assertFalse(f._rolled)
        f.write(b'x')
        self.assertTrue(f._rolled) 
Example 37
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_fileno(self):
        # A SpooledTemporaryFile should roll over to a real file on fileno()
        f = self.do_create(max_size=30)
        self.assertFalse(f._rolled)
        self.assertTrue(f.fileno() > 0)
        self.assertTrue(f._rolled) 
Example 38
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_multiple_close_before_rollover(self):
        # A SpooledTemporaryFile can be closed many times without error
        f = tempfile.SpooledTemporaryFile()
        f.write(b'abc\n')
        self.assertFalse(f._rolled)
        f.close()
        f.close()
        f.close() 
Example 39
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_bound_methods(self):
        # It should be OK to steal a bound method from a SpooledTemporaryFile
        # and use it independently; when the file rolls over, those bound
        # methods should continue to function
        f = self.do_create(max_size=30)
        read = f.read
        write = f.write
        seek = f.seek

        write(b"a" * 35)
        write(b"b" * 35)
        seek(0, 0)
        self.assertEqual(read(70), b'a'*35 + b'b'*35) 
Example 40
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_text_mode(self):
        # Creating a SpooledTemporaryFile with a text mode should produce
        # a file object reading and writing (Unicode) text strings.
        f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10)
        f.write("abc\n")
        f.seek(0)
        self.assertEqual(f.read(), "abc\n")
        f.write("def\n")
        f.seek(0)
        self.assertEqual(f.read(), "abc\ndef\n")
        self.assertFalse(f._rolled)
        self.assertEqual(f.mode, 'w+')
        self.assertIsNone(f.name)
        self.assertIsNone(f.newlines)
        self.assertIsNone(f.encoding)

        f.write("xyzzy\n")
        f.seek(0)
        self.assertEqual(f.read(), "abc\ndef\nxyzzy\n")
        # Check that Ctrl+Z doesn't truncate the file
        f.write("foo\x1abar\n")
        f.seek(0)
        self.assertEqual(f.read(), "abc\ndef\nxyzzy\nfoo\x1abar\n")
        self.assertTrue(f._rolled)
        self.assertEqual(f.mode, 'w+')
        self.assertIsNotNone(f.name)
        self.assertEqual(f.newlines, os.linesep)
        self.assertIsNotNone(f.encoding) 
Example 41
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_context_manager_before_rollover(self):
        # A SpooledTemporaryFile can be used as a context manager
        with tempfile.SpooledTemporaryFile(max_size=1) as f:
            self.assertFalse(f._rolled)
            self.assertFalse(f.closed)
        self.assertTrue(f.closed)
        def use_closed():
            with f:
                pass
        self.assertRaises(ValueError, use_closed) 
Example 42
Project: NiujiaoDebugger   Author: MrSrc   File: test_tempfile.py    GNU General Public License v3.0 5 votes vote down vote up
def test_context_manager_after_rollover(self):
        # A SpooledTemporaryFile can be used as a context manager
        f = tempfile.SpooledTemporaryFile(max_size=1)
        f.write(b'abc\n')
        f.flush()
        self.assertTrue(f._rolled)
        with f:
            self.assertFalse(f.closed)
        self.assertTrue(f.closed)
        def use_closed():
            with f:
                pass
        self.assertRaises(ValueError, use_closed) 
Example 43
Project: Health-Checker   Author: KriAga   File: formparser.py    MIT License 5 votes vote down vote up
def default_stream_factory(total_content_length, filename, content_type,
                           content_length=None):
    """The stream factory that is used per default."""
    max_size = 1024 * 500
    if SpooledTemporaryFile is not None:
        return SpooledTemporaryFile(max_size=max_size, mode='wb+')
    if total_content_length is None or total_content_length > max_size:
        return TemporaryFile('wb+')
    return BytesIO() 
Example 44
Project: recruit   Author: Frank-qlu   File: formparser.py    Apache License 2.0 5 votes vote down vote up
def default_stream_factory(
    total_content_length, filename, content_type, content_length=None
):
    """The stream factory that is used per default."""
    max_size = 1024 * 500
    if SpooledTemporaryFile is not None:
        return SpooledTemporaryFile(max_size=max_size, mode="wb+")
    if total_content_length is None or total_content_length > max_size:
        return TemporaryFile("wb+")
    return BytesIO() 
Example 45
Project: edx-analytics-exporter   Author: edx   File: util.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def logging_streams_on_failure(name):
    # Spill to disk if a very large amount of output is generated by the task.
    with tempfile.SpooledTemporaryFile(max_size=MEMORY_BUFFER_SIZE) as error_file:
        with tempfile.SpooledTemporaryFile(max_size=MEMORY_BUFFER_SIZE) as output_file:
            try:
                yield output_file, error_file
            except Exception:
                log.warning('Failed to execute %s', name, exc_info=True)
                log.warning('Standard Output')
                log_file_contents(log.warning, output_file)
                log.warning('Standard Error')
                log_file_contents(log.warning, error_file)
                raise 
Example 46
Project: csv2sql   Author: ymoch   File: prefetching.py    MIT License 5 votes vote down vote up
def __init__(self, file_obj, **kwargs):
        """Initialize.
        The buffer size can be specified by `buffer_size`,
        which can result in performance improvement
        in exchange for memory usage.
        """
        buffer_size = kwargs.get('buffer_size', 10 * 1024 * 1024)

        self._file = file_obj
        self._buffer = tempfile.SpooledTemporaryFile(
            max_size=buffer_size, mode='w+') 
Example 47
Project: scratchdir   Author: ahawker   File: scratchdir.py    Apache License 2.0 5 votes vote down vote up
def spooled(self, max_size: int = 0, mode: str = 'w+b', buffering: int = -1,
                encoding: typing.Optional[str] = None, newline: typing.Optional[str] = None,
                suffix: typing.Optional[str] = DEFAULT_SUFFIX, prefix: typing.Optional[str] = DEFAULT_PREFIX,
                dir: typing.Optional[str] = None) -> typing.IO:
        """
        Create a new spooled temporary file within the scratch dir.

        This returns a :class:`~tempfile.SpooledTemporaryFile` which is a specialized object that wraps a
        :class:`StringIO`/:class:`BytesIO` instance that transparently overflows into a file on the disk once it
        reaches a certain size.

        By default, a spooled file will never roll over to disk.

        :param max_size: (Optional) max size before the in-memory buffer rolls over to disk
        :type max_size: :class:`~int`
        :param mode: (Optional) mode to open the file with
        :type mode: :class:`~str`
        :param buffering: (Optional) size of the file buffer
        :type buffering: :class:`~int`
        :param encoding: (Optional) encoding to open the file with
        :type encoding: :class:`~str`
        :param newline: (Optional) newline argument to open the file with
        :type newline: :class:`~str` or :class:`~NoneType`
        :param suffix: (Optional) filename suffix
        :type suffix: :class:`~str` or :class:`~NoneType`
        :param prefix: (Optional) filename prefix
        :type prefix: :class:`~str` or :class:`~NoneType`
        :param dir: (Optional) relative path to directory within the scratch dir where the file should exist
        :type dir: :class:`~bool`
        :return: SpooledTemporaryFile instance
        :rtype: :class:`~tempfile.SpooledTemporaryFile`
        """
        return tempfile.SpooledTemporaryFile(max_size, mode, buffering, encoding,
                                             newline, suffix, prefix, self.join(dir)) 
Example 48
Project: ironpython2   Author: IronLanguages   File: test_gzip.py    Apache License 2.0 5 votes vote down vote up
def test_fileobj_without_name(self):
        # Issue #33038: GzipFile should not assume that file objects that have
        # a .name attribute use a non-None value.
        with tempfile.SpooledTemporaryFile() as f:
            with gzip.GzipFile(fileobj=f, mode='wb') as archive:
                archive.write(b'data')
                self.assertEqual(archive.name, '') 
Example 49
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def do_create(self, max_size=0, dir=None, pre="", suf=""):
        if dir is None:
            dir = tempfile.gettempdir()
        try:
            file = tempfile.SpooledTemporaryFile(max_size=max_size, dir=dir, prefix=pre, suffix=suf)
        except:
            self.failOnException("SpooledTemporaryFile")

        return file 
Example 50
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_basic(self):
        # SpooledTemporaryFile can create files
        f = self.do_create()
        self.assertFalse(f._rolled)
        f = self.do_create(max_size=100, pre="a", suf=".txt")
        self.assertFalse(f._rolled) 
Example 51
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_del_on_close(self):
        # A SpooledTemporaryFile is deleted when closed
        dir = tempfile.mkdtemp()
        try:
            f = tempfile.SpooledTemporaryFile(max_size=10, dir=dir)
            self.assertFalse(f._rolled)
            f.write('blat ' * 5)
            self.assertTrue(f._rolled)
            filename = f.name
            f.close()
            self.assertFalse(os.path.exists(filename),
                        "SpooledTemporaryFile %s exists after close" % filename)
        finally:
            os.rmdir(dir) 
Example 52
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_write_sequential(self):
        # A SpooledTemporaryFile should hold exactly max_size bytes, and roll
        # over afterward
        f = self.do_create(max_size=30)
        self.assertFalse(f._rolled)
        f.write('x' * 20)
        self.assertFalse(f._rolled)
        f.write('x' * 10)
        self.assertFalse(f._rolled)
        f.write('x')
        self.assertTrue(f._rolled) 
Example 53
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_writelines(self):
        # Verify writelines with a SpooledTemporaryFile
        f = self.do_create()
        f.writelines((b'x', b'y', b'z'))
        f.seek(0)
        buf = f.read()
        self.assertEqual(buf, b'xyz') 
Example 54
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_writelines_sequential(self):
        # A SpooledTemporaryFile should hold exactly max_size bytes, and roll
        # over afterward
        f = self.do_create(max_size=35)
        f.writelines((b'x' * 20, b'x' * 10, b'x' * 5))
        self.assertFalse(f._rolled)
        f.write(b'x')
        self.assertTrue(f._rolled) 
Example 55
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_sparse(self):
        # A SpooledTemporaryFile that is written late in the file will extend
        # when that occurs
        f = self.do_create(max_size=30)
        self.assertFalse(f._rolled)
        f.seek(100, 0)
        self.assertFalse(f._rolled)
        f.write('x')
        self.assertTrue(f._rolled) 
Example 56
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_fileno(self):
        # A SpooledTemporaryFile should roll over to a real file on fileno()
        f = self.do_create(max_size=30)
        self.assertFalse(f._rolled)
        self.assertTrue(f.fileno() > 0)
        self.assertTrue(f._rolled) 
Example 57
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_multiple_close_after_rollover(self):
        # A SpooledTemporaryFile can be closed many times without error
        f = tempfile.SpooledTemporaryFile(max_size=1)
        f.write('abc\n')
        self.assertTrue(f._rolled)
        f.close()
        try:
            f.close()
            f.close()
        except:
            self.failOnException("close") 
Example 58
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_bound_methods(self):
        # It should be OK to steal a bound method from a SpooledTemporaryFile
        # and use it independently; when the file rolls over, those bound
        # methods should continue to function
        f = self.do_create(max_size=30)
        read = f.read
        write = f.write
        seek = f.seek

        write("a" * 35)
        write("b" * 35)
        seek(0, 0)
        self.assertTrue(read(70) == 'a'*35 + 'b'*35) 
Example 59
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_context_manager_before_rollover(self):
        # A SpooledTemporaryFile can be used as a context manager
        with tempfile.SpooledTemporaryFile(max_size=1) as f:
            self.assertFalse(f._rolled)
            self.assertFalse(f.closed)
        self.assertTrue(f.closed)
        def use_closed():
            with f:
                pass
        self.assertRaises(ValueError, use_closed) 
Example 60
Project: ironpython2   Author: IronLanguages   File: test_tempfile.py    Apache License 2.0 5 votes vote down vote up
def test_context_manager_during_rollover(self):
        # A SpooledTemporaryFile can be used as a context manager
        with tempfile.SpooledTemporaryFile(max_size=1) as f:
            self.assertFalse(f._rolled)
            f.write('abc\n')
            f.flush()
            self.assertTrue(f._rolled)
            self.assertFalse(f.closed)
        self.assertTrue(f.closed)
        def use_closed():
            with f:
                pass
        self.assertRaises(ValueError, use_closed) 
Example 61
Project: XMorbid   Author: NMTech0x90   File: formparser.py    GNU General Public License v3.0 5 votes vote down vote up
def default_stream_factory(total_content_length, filename, content_type,
                           content_length=None):
    """The stream factory that is used per default."""
    max_size = 1024 * 500
    if SpooledTemporaryFile is not None:
        return SpooledTemporaryFile(max_size=max_size, mode='wb+')
    if total_content_length is None or total_content_length > max_size:
        return TemporaryFile('wb+')
    return BytesIO() 
Example 62
Project: strax   Author: AxFoundation   File: s3.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _read_chunk(self, backend_key, chunk_info, dtype, compressor):
        # Temporary hack for backward compatibility
        if 'filename' in chunk_info:
            chunk_info['key_name'] = f"{backend_key}/{chunk_info['filename']}"

        with tempfile.SpooledTemporaryFile() as f:
            self.s3.download_fileobj(Bucket=BUCKET_NAME,
                                     Key=chunk_info['key_name'],
                                     Fileobj=f)
            f.seek(0)  # Needed?
            return strax.load_file(f,
                                   dtype=dtype,
                                   compressor=compressor) 
Example 63
Project: strax   Author: AxFoundation   File: s3.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _upload_json(self, document, filename):
        with tempfile.SpooledTemporaryFile() as f:
            text = json.dumps(document, **self.json_options)
            f.write(text.encode())
            f.seek(0)
            self.s3.upload_fileobj(f,
                                   BUCKET_NAME,
                                   filename) 
Example 64
Project: Blockly-rduino-communication   Author: technologiescollege   File: formparser.py    GNU General Public License v3.0 5 votes vote down vote up
def default_stream_factory(total_content_length, filename, content_type,
                           content_length=None):
    """The stream factory that is used per default."""
    max_size = 1024 * 500
    if SpooledTemporaryFile is not None:
        return SpooledTemporaryFile(max_size=max_size, mode='wb+')
    if total_content_length is None or total_content_length > max_size:
        return TemporaryFile('wb+')
    return BytesIO() 
Example 65
Project: mscdec   Author: jam1garner   File: msc.py    MIT License 5 votes vote down vote up
def readFromBytes(self, b, headerEndianess='>'):
        with tempfile.SpooledTemporaryFile(mode='w+b') as f:
            f.write(b)
            f.seek(0)
            self.readFromFile(f, headerEndianess) 
Example 66
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_exports(self):
        # There are no surprising symbols in the tempfile module
        dict = tempfile.__dict__

        expected = {
            "NamedTemporaryFile" : 1,
            "TemporaryFile" : 1,
            "mkstemp" : 1,
            "mkdtemp" : 1,
            "mktemp" : 1,
            "TMP_MAX" : 1,
            "gettempprefix" : 1,
            "gettempprefixb" : 1,
            "gettempdir" : 1,
            "gettempdirb" : 1,
            "tempdir" : 1,
            "template" : 1,
            "SpooledTemporaryFile" : 1,
            "TemporaryDirectory" : 1,
        }

        unexp = []
        for key in dict:
            if key[0] != '_' and key not in expected:
                unexp.append(key)
        self.assertTrue(len(unexp) == 0,
                        "unexpected keys: %s" % unexp) 
Example 67
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def do_create(self, max_size=0, dir=None, pre="", suf=""):
        if dir is None:
            dir = tempfile.gettempdir()
        file = tempfile.SpooledTemporaryFile(max_size=max_size, dir=dir, prefix=pre, suffix=suf)

        return file 
Example 68
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_basic(self):
        # SpooledTemporaryFile can create files
        f = self.do_create()
        self.assertFalse(f._rolled)
        f = self.do_create(max_size=100, pre="a", suf=".txt")
        self.assertFalse(f._rolled) 
Example 69
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_del_on_close(self):
        # A SpooledTemporaryFile is deleted when closed
        dir = tempfile.mkdtemp()
        try:
            f = tempfile.SpooledTemporaryFile(max_size=10, dir=dir)
            self.assertFalse(f._rolled)
            f.write(b'blat ' * 5)
            self.assertTrue(f._rolled)
            filename = f.name
            f.close()
            self.assertFalse(isinstance(filename, str) and os.path.exists(filename),
                        "SpooledTemporaryFile %s exists after close" % filename)
        finally:
            os.rmdir(dir) 
Example 70
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_rewrite_small(self):
        # A SpooledTemporaryFile can be written to multiple within the max_size
        f = self.do_create(max_size=30)
        self.assertFalse(f._rolled)
        for i in range(5):
            f.seek(0, 0)
            f.write(b'x' * 20)
        self.assertFalse(f._rolled) 
Example 71
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_writelines(self):
        # Verify writelines with a SpooledTemporaryFile
        f = self.do_create()
        f.writelines((b'x', b'y', b'z'))
        f.seek(0)
        buf = f.read()
        self.assertEqual(buf, b'xyz') 
Example 72
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_writelines_sequential(self):
        # A SpooledTemporaryFile should hold exactly max_size bytes, and roll
        # over afterward
        f = self.do_create(max_size=35)
        f.writelines((b'x' * 20, b'x' * 10, b'x' * 5))
        self.assertFalse(f._rolled)
        f.write(b'x')
        self.assertTrue(f._rolled) 
Example 73
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_sparse(self):
        # A SpooledTemporaryFile that is written late in the file will extend
        # when that occurs
        f = self.do_create(max_size=30)
        self.assertFalse(f._rolled)
        f.seek(100, 0)
        self.assertFalse(f._rolled)
        f.write(b'x')
        self.assertTrue(f._rolled) 
Example 74
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_fileno(self):
        # A SpooledTemporaryFile should roll over to a real file on fileno()
        f = self.do_create(max_size=30)
        self.assertFalse(f._rolled)
        self.assertTrue(f.fileno() > 0)
        self.assertTrue(f._rolled) 
Example 75
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_multiple_close_before_rollover(self):
        # A SpooledTemporaryFile can be closed many times without error
        f = tempfile.SpooledTemporaryFile()
        f.write(b'abc\n')
        self.assertFalse(f._rolled)
        f.close()
        f.close()
        f.close() 
Example 76
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_bound_methods(self):
        # It should be OK to steal a bound method from a SpooledTemporaryFile
        # and use it independently; when the file rolls over, those bound
        # methods should continue to function
        f = self.do_create(max_size=30)
        read = f.read
        write = f.write
        seek = f.seek

        write(b"a" * 35)
        write(b"b" * 35)
        seek(0, 0)
        self.assertEqual(read(70), b'a'*35 + b'b'*35) 
Example 77
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_text_mode(self):
        # Creating a SpooledTemporaryFile with a text mode should produce
        # a file object reading and writing (Unicode) text strings.
        f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10)
        f.write("abc\n")
        f.seek(0)
        self.assertEqual(f.read(), "abc\n")
        f.write("def\n")
        f.seek(0)
        self.assertEqual(f.read(), "abc\ndef\n")
        self.assertFalse(f._rolled)
        self.assertEqual(f.mode, 'w+')
        self.assertIsNone(f.name)
        self.assertIsNone(f.newlines)
        self.assertIsNone(f.encoding)

        f.write("xyzzy\n")
        f.seek(0)
        self.assertEqual(f.read(), "abc\ndef\nxyzzy\n")
        # Check that Ctrl+Z doesn't truncate the file
        f.write("foo\x1abar\n")
        f.seek(0)
        self.assertEqual(f.read(), "abc\ndef\nxyzzy\nfoo\x1abar\n")
        self.assertTrue(f._rolled)
        self.assertEqual(f.mode, 'w+')
        self.assertIsNotNone(f.name)
        self.assertEqual(f.newlines, os.linesep)
        self.assertIsNotNone(f.encoding) 
Example 78
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_context_manager_before_rollover(self):
        # A SpooledTemporaryFile can be used as a context manager
        with tempfile.SpooledTemporaryFile(max_size=1) as f:
            self.assertFalse(f._rolled)
            self.assertFalse(f.closed)
        self.assertTrue(f.closed)
        def use_closed():
            with f:
                pass
        self.assertRaises(ValueError, use_closed) 
Example 79
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_tempfile.py    GNU General Public License v2.0 5 votes vote down vote up
def test_context_manager_after_rollover(self):
        # A SpooledTemporaryFile can be used as a context manager
        f = tempfile.SpooledTemporaryFile(max_size=1)
        f.write(b'abc\n')
        f.flush()
        self.assertTrue(f._rolled)
        with f:
            self.assertFalse(f.closed)
        self.assertTrue(f.closed)
        def use_closed():
            with f:
                pass
        self.assertRaises(ValueError, use_closed) 
Example 80
Project: ckanext-geonode   Author: geosolutions-it   File: geonode.py    GNU General Public License v2.0 4 votes vote down vote up
def _update_package(self, context, package_dict, harvest_object):

        # Resources will be replaced since we don't know if the data changed somehow.
        # 1) update the package, with the new values except the resource that need downloading
        # 2) update the resource(s) one by one
        # shoud we remove by hands the old resource data from the datastore? TODO

        resources = package_dict.pop('resources', None)
        downloadable_resources = []
        normal_resources = []
        for resource in resources:
            if resource.get(RESOURCE_DOWNLOADER, None):
                downloadable_resources.append(resource)
            else:
                normal_resources.append(resource)

        if len(normal_resources):
            package_dict['resources'] = normal_resources

        package_id = p.toolkit.get_action('package_update')(context, package_dict)

        # Handle data downloads

        # TODO: check what's changed in the resources: have previous res been removed?

        # add the resources
        # (we're adding the resources from scratch instead of updating them -- the downside is that we can't have
        # permalinks to resources since the ID will be recreated)

        for resource in downloadable_resources:
            resource['package_id'] = package_id
            log.info('Handling download data for resource %s in package %s' % (resource['name'], package_id))
            downloader = resource.pop(RESOURCE_DOWNLOADER)

            with SpooledTemporaryFile(max_size=TEMP_FILE_THRESHOLD_SIZE) as f:
                fieldStorage = downloader.download(f)
                resource['upload'] = fieldStorage
                log.info('Create resource %s in package %s', resource['name'], package_id)
                created_resource = p.toolkit.get_action('resource_create')(context, resource)
                log.debug('Added resource %s to package %s with uuid %s', resource['name'], package_id, created_resource['id'])

        return package_id