def test_streamlit_write(self): """Test streamlitfile_util.streamlit_write.""" dirname = os.path.dirname(file_util.get_streamlit_file_path(FILENAME)) with patch("streamlit.file_util.open", mock_open()) as open, patch( "streamlit.util.os.makedirs" ) as makedirs, file_util.streamlit_write(FILENAME) as output: output.write("some data") open().write.assert_called_once_with("some data") makedirs.assert_called_once_with(dirname)
def test_streamlit_write(self): """Test streamlitfile_util.streamlit_write.""" dirname = os.path.dirname(file_util.get_streamlit_file_path(FILENAME)) # patch streamlit.*.os.makedirs instead of os.makedirs for py35 compat with patch("streamlit.file_util.open", mock_open()) as open, patch( "streamlit.util.os.makedirs" ) as makedirs, file_util.streamlit_write(FILENAME) as output: output.write("some data") open().write.assert_called_once_with("some data") makedirs.assert_called_once_with(dirname, exist_ok=True)
def test_streamlit_write_exception(self): """Test streamlitfile_util.streamlit_write.""" with patch("streamlit.file_util.open", mock_open()) as p, patch("streamlit.util.os.makedirs"): p.side_effect = OSError(errno.EINVAL, "[Errno 22] Invalid argument") with pytest.raises(util.Error) as e, file_util.streamlit_write( FILENAME) as output: output.write("some data") error_msg = ("Unable to write file: /some/cache/file\n" "Python is limited to files below 2GB on OSX. " "See https://bugs.python.org/issue24658") self.assertEqual(str(e.value), error_msg)
def _write_to_disk_cache(self, key: str, pickled_value: bytes) -> None: path = self._get_file_path(key) try: with streamlit_write(path, binary=True) as output: output.write(pickled_value) except util.Error as e: _LOGGER.debug(e) # Clean up file so we don't leave zero byte files. try: os.remove(path) except (FileNotFoundError, IOError, OSError): pass raise CacheError("Unable to write to cache") from e
def _write_to_disk_cache(key, value): path = file_util.get_streamlit_file_path("cache", "%s.pickle" % key) try: with file_util.streamlit_write(path, binary=True) as output: entry = _DiskCacheEntry(value=value) pickle.dump(entry, output, pickle.HIGHEST_PROTOCOL) except util.Error as e: _LOGGER.debug(e) # Clean up file so we don't leave zero byte files. try: os.remove(path) except (FileNotFoundError, IOError, OSError): pass raise CacheError("Unable to write to cache: %s" % e)
def _write_to_disk_cache(key, value, args_mutated): path = file_util.get_streamlit_file_path("cache", "%s.pickle" % key) try: with file_util.streamlit_write(path, binary=True) as output: entry = DiskCacheEntry(value=value, args_mutated=args_mutated) pickle.dump(entry, output, pickle.HIGHEST_PROTOCOL) # In python 2, it's pickle struct error. # In python 3, it's an open error in util. except (util.Error, struct.error) as e: LOGGER.debug(e) # Clean up file so we don't leave zero byte files. try: os.remove(path) except (FileNotFoundError, IOError, OSError): pass raise CacheError("Unable to write to cache: %s" % e)
def _get_stable_random_id(): """Get a stable random ID This is a unique identifier for a user for tracking metrics in Segment. Instead of relying on a hardware address in the container or host we'll generate a UUID and store it in the Streamlit hidden folder. """ filepath = file_util.get_streamlit_file_path(".stable_random_id") stable_id = None if os.path.exists(filepath): with file_util.streamlit_read(filepath) as input: stable_id = input.read() if not stable_id: stable_id = str(uuid.uuid4()) with file_util.streamlit_write(filepath) as output: output.write(stable_id) return stable_id