Python tempfile.SpooledTemporaryFile() Examples

The following are 30 code examples of tempfile.SpooledTemporaryFile(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tempfile , or try the search function .
Example #1
Source File: test_file.py    From python-sdk with GNU Lesser General Public License v3.0 7 votes vote down vote up
def test_basic():  # type: () -> None
    def fn(s):
        f = File("Blah", s, mime_type="text/plain")
        assert f.name == "Blah"
        assert f._metadata["size"] == 14
        assert f.size == 14
        assert f._metadata["_checksum"] == "55e562bfee2bde4f9e71b8885eb5e303"

    b = b"blah blah blah"
    fn(io.BytesIO(b))
    fn(memoryview(b))

    import tempfile

    f = tempfile.SpooledTemporaryFile()
    f.write(b)
    fn(f)

    if six.PY2:
        import StringIO
        import cStringIO

        fn(StringIO.StringIO(b))
        fn(cStringIO.StringIO(b))
        fn(buffer(b))  # noqa: F821 
Example #2
Source File: espeak.py    From xuebao with MIT License 7 votes vote down vote up
def say(self, phrase):
        if isinstance(phrase, unicode):
            phrase = phrase.encode('utf8')
        with tempfile.SpooledTemporaryFile() as out_f:
            cmd = ['espeak', '-v', self.voice,
                             '-p', self.pitch_adjustment,
                             '-s', self.words_per_minute,
                             '--stdout',
                             phrase]
            cmd = [str(x) for x in cmd]
            self._logger.debug('Executing %s', ' '.join([pipes.quote(arg)
                                                         for arg in cmd]))
            subprocess.call(cmd, stdout=out_f)
            out_f.seek(0)
            data = out_f.read()
            return data 
Example #3
Source File: __init__.py    From omnitool with MIT License 6 votes vote down vote up
def plug_save(Plug):
    if hasattr(Plug, "loadingbar"):
        # we have a loadingbar to attend to
        loadcallback = Plug.loadingbar
    else:
        loadcallback = None
    f = tempfile.SpooledTemporaryFile(10000000)  # 10 megabyte ram file
    set_header(f, Plug.header)
    try:
        Plug.tiles[0]
    except:
        Plug.tiles.seek(0)
        f.write(Plug.tiles.read())
    else:
        set_tiles(f, Plug.tiles, Plug.header, True, loadcallback)
    set_chests(f, Plug.chests)
    set_signs(f, Plug.signs)
    [set_npc(f, npc) for npc in Plug.npcs]
    set_npc(f, None)
    set_npc_names(f, Plug.names)
    set_trail(f, (1, Plug.header["name"], Plug.header["ID"]))
    with get_next_world(Plug.header["name"]).open("wb") as g:
        f.seek(0)
        g.write(f.read()) 
Example #4
Source File: plugin.py    From xuebao with MIT License 6 votes vote down vote up
def mp3_to_wave(self, filename):
        mf = mad.MadFile(filename)
        with tempfile.SpooledTemporaryFile() as f:
            wav = wave.open(f, mode='wb')
            wav.setframerate(mf.samplerate())
            wav.setnchannels(1 if mf.mode() == mad.MODE_SINGLE_CHANNEL else 2)
            # 4L is the sample width of 32 bit audio
            wav.setsampwidth(4)
            frame = mf.read()
            while frame is not None:
                wav.writeframes(frame)
                frame = mf.read()
            wav.close()
            f.seek(0)
            data = f.read()
        return data 
Example #5
Source File: plugin.py    From testinfra with Apache License 2.0 6 votes vote down vote up
def pytest_configure(config):
    if config.option.verbose > 1:
        root = logging.getLogger()
        if not root.handlers:
            root.addHandler(logging.NullHandler())
        logging.getLogger("testinfra").setLevel(logging.DEBUG)
    if config.option.nagios:
        # disable & re-enable terminalreporter to write in a tempfile
        reporter = config.pluginmanager.getplugin('terminalreporter')
        if reporter:
            out = SpooledTemporaryFile(encoding=sys.stdout.encoding)
            config.pluginmanager.unregister(reporter)
            reporter = reporter.__class__(config, out)
            config.pluginmanager.register(reporter, 'terminalreporter')
            config.pluginmanager.register(NagiosReporter(out),
                                          'nagiosreporter') 
Example #6
Source File: s3.py    From strax with 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 #7
Source File: test_tempfile.py    From BinderFilter with MIT License 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 #8
Source File: test_tempfile.py    From ironpython2 with 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
Source File: test_tempfile.py    From BinderFilter with MIT License 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 #10
Source File: test_tempfile.py    From oss-ftp with MIT License 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 #11
Source File: download.py    From send-cli with GNU General Public License v3.0 6 votes vote down vote up
def api_download(service, fileId, authorisation):
    '''Given a Send url, download and return the encrypted data and metadata'''
    data = tempfile.SpooledTemporaryFile(max_size=SPOOL_SIZE, mode='w+b')

    headers = {'Authorization' : 'send-v1 ' + unpadded_urlsafe_b64encode(authorisation)}
    url = service + 'api/download/' + fileId

    r = requests.get(url, headers=headers, stream=True)
    r.raise_for_status()
    content_length = int(r.headers['Content-length'])

    pbar = progbar(content_length)
    for chunk in r.iter_content(chunk_size=CHUNK_SIZE):
        data.write(chunk)
        pbar.update(len(chunk))
    pbar.close()

    data.seek(0)
    return data 
Example #12
Source File: upload.py    From send-cli with GNU General Public License v3.0 6 votes vote down vote up
def encrypt_file(file, keys=secretKeys()):
    '''Encrypt file data with the same method as the Send browser/js client'''
    key = keys.encryptKey
    iv = keys.encryptIV
    encData = tempfile.SpooledTemporaryFile(max_size=SPOOL_SIZE, mode='w+b')
    cipher = Cryptodome.Cipher.AES.new(key, Cryptodome.Cipher.AES.MODE_GCM, iv)

    pbar = progbar(fileSize(file))

    for chunk in iter(lambda: file.read(CHUNK_SIZE), b''):
        encData.write(cipher.encrypt(chunk))
        pbar.update(len(chunk))

    pbar.close()
    encData.write(cipher.digest())
    file.close()

    encData.seek(0)
    return encData 
Example #13
Source File: test_tempfile.py    From oss-ftp with MIT License 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 #14
Source File: test_tempfile.py    From ironpython2 with Apache License 2.0 6 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 #15
Source File: provider.py    From streamalert with Apache License 2.0 6 votes vote down vote up
def offer_fileobj(self, descriptor):
        """Opens a file-like temporary file spool and returns it.

        If you use the return value in a `with` statement block then the file descriptor
        auto-close.

        NOTE: (!) This returns an ephemeral spool that is not attached to the caching mechanism
            in save_credentials() and load_credentials()

        Args:
            descriptor (str): Descriptor of the current Output

        Returns:
            file object
        """
        return tempfile.SpooledTemporaryFile(0, 'a+b') 
Example #16
Source File: test_tempfile.py    From ironpython2 with 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 #17
Source File: test_tempfile.py    From oss-ftp with MIT License 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 #18
Source File: test_payload_s3.py    From streamalert with Apache License 2.0 5 votes vote down vote up
def test_gz_reader(self):
        """S3Payload - GZ Reader"""
        record = {'key': 'value'}
        json_line = (json.dumps(record, separators=(',', ':')) + '\n').encode()
        with tempfile.SpooledTemporaryFile(max_size=10*1024) as reader:
            writer = gzip.GzipFile(filename='test', fileobj=reader)
            writer.writelines([
                json_line,
                json_line
            ])
            writer.close()
            reader.seek(0)
            gz_reader = S3Payload._gz_reader(reader)
            assert_equal(isinstance(gz_reader, gzip.GzipFile), True)
            assert_equal(gz_reader.read(), json_line + json_line) 
Example #19
Source File: provider.py    From streamalert with Apache License 2.0 5 votes vote down vote up
def save_credentials(self, descriptor, credentials):
        """Saves the credentials into a temporary spool.

        Args:
            descriptor (str): Descriptor of the current Output
            credentials (Credentials): Credentials object that is intended to be saved

        Return:
            bool: True on success, False otherwise
        """
        # Always store unencrypted because it's in memory. Saves calls to KMS and it's safe
        # because other unrelated processes cannot read this memory (probably..)
        if not credentials.is_encrypted():
            LOGGER.error('Error: Writing unencrypted credentials to disk is disallowed.')
            return False

        raw_creds = credentials.data()
        if not isinstance(raw_creds, bytes):
            raw_creds = raw_creds.encode()

        spool = tempfile.SpooledTemporaryFile()
        spool.write(raw_creds)

        key = self.get_spool_cache_key(descriptor)
        type(self).SERVICE_SPOOLS[key] = spool

        return True 
Example #20
Source File: test_payload_s3.py    From streamalert with Apache License 2.0 5 votes vote down vote up
def test_gz_reader_non_gz(self):
        """S3Payload - GZ Reader, Non-gzip"""
        record = {'key': 'value'}
        json_line = (json.dumps(record, separators=(',', ':')) + '\n').encode()
        with tempfile.SpooledTemporaryFile(max_size=10*1024) as reader:
            reader.writelines([
                json_line,
                json_line
            ])
            reader.seek(0)
            non_gz_reader = S3Payload._gz_reader(reader)
            assert_equal(reader == non_gz_reader, True) 
Example #21
Source File: classifier.py    From talon with Apache License 2.0 5 votes vote down vote up
def load_compat(saved_classifier_filename):
    import os
    import pickle
    import tempfile

    # we need to switch to the data path to properly load the related _xx.npy files
    cwd = os.getcwd()
    os.chdir(os.path.dirname(saved_classifier_filename))

    # convert encoding using pick.load and write to temp file which we'll tell joblib to use
    pickle_file = open(saved_classifier_filename, 'rb')
    classifier = pickle.load(pickle_file, encoding='latin1')

    try:
        # save our conversion if permissions allow
        joblib.dump(classifier, saved_classifier_filename)
    except Exception:
        # can't write to classifier, use a temp file
        tmp = tempfile.SpooledTemporaryFile()
        joblib.dump(classifier, tmp)
        saved_classifier_filename = tmp

    # important, use joblib.load before switching back to original cwd
    jb_classifier = joblib.load(saved_classifier_filename)
    os.chdir(cwd)

    return jb_classifier 
Example #22
Source File: test_downloaders.py    From attention-lvcsr with MIT License 5 votes vote down vote up
def test_download_content_no_length(self):
        with tempfile.SpooledTemporaryFile() as f:
            download(mock_url, f)
            f.seek(0)
            assert_equal(f.read(), mock_content) 
Example #23
Source File: test_tempfile.py    From oss-ftp with MIT License 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 #24
Source File: node_output.py    From aztk with MIT License 5 votes vote down vote up
def __init__(self, id: str, output: Union[SpooledTemporaryFile, str] = None, error: Exception = None):
        self.id = id
        self.output = output
        self.error = error 
Example #25
Source File: test_tempfile.py    From oss-ftp with MIT License 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 #26
Source File: test_tempfile.py    From oss-ftp with MIT License 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 #27
Source File: test_tempfile.py    From oss-ftp with MIT License 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 #28
Source File: test_tempfile.py    From oss-ftp with MIT License 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 #29
Source File: test_tempfile.py    From oss-ftp with MIT License 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 #30
Source File: test_tempfile.py    From oss-ftp with MIT License 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)