def test_streamlit_read_zero_bytes(self): """Test streamlitfile_util.streamlit_read.""" self.os_stat.return_value.st_size = 0 with pytest.raises(util.Error) as e: with file_util.streamlit_read(FILENAME) as input: data = input.read() self.assertEqual(str(e.value), 'Read zero byte file: "/some/cache/file"')
def _read_from_disk_cache(self, key: str) -> bytes: path = self._get_file_path(key) try: with streamlit_read(path, binary=True) as input: value = input.read() _LOGGER.debug("Disk cache HIT: %s", key) return bytes(value) except FileNotFoundError: raise CacheKeyNotFoundError("Key not found in disk cache") except BaseException as e: _LOGGER.error(e) raise CacheError("Unable to read from cache") from e
def _read_from_disk_cache(key): path = file_util.get_streamlit_file_path("cache", "%s.pickle" % key) try: with file_util.streamlit_read(path, binary=True) as input: value, args_mutated = pickle.load(input) LOGGER.debug("Disk cache HIT: %s", type(value)) except util.Error as e: LOGGER.error(e) raise CacheError("Unable to read from cache: %s" % e) except (OSError, FileNotFoundError): # Python 2 # Python 3 raise CacheKeyNotFoundError("Key not found in disk cache") return value, args_mutated
def _read_from_disk_cache(key): path = file_util.get_streamlit_file_path("cache", "%s.pickle" % key) try: with file_util.streamlit_read(path, binary=True) as input: entry = pickle.load(input) value = entry.value _LOGGER.debug("Disk cache HIT: %s", type(value)) except util.Error as e: _LOGGER.error(e) raise CacheError("Unable to read from cache: %s" % e) except FileNotFoundError: raise CacheKeyNotFoundError("Key not found in disk cache") return value
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
def test_streamlit_read_binary(self): """Test streamlitfile_util.streamlit_read.""" with file_util.streamlit_read(FILENAME, binary=True) as input: data = input.read() self.assertEqual(b"\xaa\xbb", data)
def test_streamlit_read(self): """Test streamlitfile_util.streamlit_read.""" with file_util.streamlit_read(FILENAME) as input: data = input.read() self.assertEqual("data", data)