def parse_config_file(force=False): """Parse the config file and update config parameters.""" global _config_file_has_been_parsed if _config_file_has_been_parsed and force == False: return # Read ~/.streamlit/config.toml, and then overlay # $CWD/.streamlit/config.toml if it exists. config_filenames = [ file_util.get_streamlit_file_path("config.toml"), file_util.get_project_streamlit_file_path("config.toml"), ] for filename in config_filenames: # Parse the config file. if not os.path.exists(filename): continue with open(filename, "r") as input: file_contents = input.read() _update_config_with_toml(file_contents, filename) _config_file_has_been_parsed = True _on_config_parsed.send()
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 _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 _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
pass try: return float(v) except Exception: pass return v # Allow outside modules to wait for the config file to be parsed before doing # something. _on_config_parsed = Signal(doc="Emitted when the config file is parsed.") CONFIG_FILENAMES = [ file_util.get_streamlit_file_path("config.toml"), file_util.get_project_streamlit_file_path("config.toml"), ] def get_config_options( force_reparse=False, options_from_flags: Optional[Dict[str, Any]] = None ) -> Dict[str, ConfigOption]: """Create and return a dict mapping config option names to their values, returning a cached dict if possible. Config option values are sourced from the following locations. Values set in locations further down the list overwrite those set earlier. 1. default values defined in this file 2. the global `~/.streamlit/config.toml` file 3. per-project `$CWD/.streamlit/config.toml` files
def _get_credential_file_path(): return file_util.get_streamlit_file_path("credentials.toml")
def get_cache_path(): return file_util.get_streamlit_file_path("cache")
def get_cache_path() -> str: return get_streamlit_file_path(_CACHE_DIR_NAME)
def _get_file_path(self, value_key: str): """Return the path of the disk cache file for the given value.""" return get_streamlit_file_path(_CACHE_DIR_NAME, f"{self.key}-{value_key}.memo")