Exemple #1
0
def parse_config_file():
    """Parse the config file and update config parameters."""
    global _config_file_has_been_parsed

    if _config_file_has_been_parsed:
        return

    # Read ~/.streamlit/config.toml, and then overlay
    # $CWD/.streamlit/config.toml if it exists.
    config_filenames = [
        util.get_streamlit_file_path("config.toml"),
        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()
Exemple #2
0
def parse_config_file(file_contents=None):
    """Parse the config file and update config parameters.

    Parameters
    ----------
    file_contents : string or None
        The contents of the config file (for use in tests) or None to load the
        config from ~/.streamlit/config.toml.
    """
    global config_file_has_been_parsed

    if config_file_has_been_parsed:
        return

    if file_contents:
        config_filename = "mock_config_file"
    else:
        config_filename = util.get_streamlit_file_path("config.toml")

        # Parse the config file.
        if not os.path.exists(config_filename):
            return

        with open(config_filename) as input:
            file_contents = input.read()

    _update_config_with_toml(file_contents, config_filename)

    config_file_has_been_parsed = True
    _on_config_parsed.send()
Exemple #3
0
    def __init__(self):
        """Initialize class."""
        if Credentials._singleton is not None:
            raise RuntimeError(
                'Credentials already initialized. Use .get_current() instead')

        self.activation = None
        self._conf_file = util.get_streamlit_file_path('credentials.toml')

        Credentials._singleton = self
Exemple #4
0
    def test_streamlit_write(self):
        """Test streamlit.util.streamlit.write."""

        dirname = os.path.dirname(util.get_streamlit_file_path(FILENAME))
        with patch("streamlit.util.open", mock_open()) as open, patch(
                "streamlit.util.os.makedirs"
        ) as makedirs, util.streamlit_write(FILENAME) as output:
            output.write("some data")
            open().write.assert_called_once_with("some data")
            makedirs.assert_called_once_with(dirname)
Exemple #5
0
def read_from_disk_cache(key):
    path = util.get_streamlit_file_path('cache', '%s.pickle' % key)

    try:
        with util.streamlit_read(path, binary=True) as input:
            rv = pickle.load(input)
            LOGGER.debug('Cache HIT: ' + str(type(rv)))
    except util.Error as e:
        LOGGER.debug(e)
        raise CacheError('Unable to read from cache: %s' % e)
    return rv
Exemple #6
0
def _read_from_disk_cache(key):
    path = util.get_streamlit_file_path("cache", "%s.pickle" % key)
    try:
        with 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
Exemple #7
0
def write_to_disk_cache(key, rv):
    path = util.get_streamlit_file_path('cache', '%s.pickle' % key)

    try:
        with util.streamlit_write(path, binary=True) as output:
            pickle.dump(rv, 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)
        # Cleanup 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)
Exemple #8
0
def _write_to_disk_cache(key, value, args_mutated):
    path = util.get_streamlit_file_path("cache", "%s.pickle" % key)

    try:
        with 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)
Exemple #9
0
def _read_from_disk_cache(key):
    path = util.get_streamlit_file_path('cache', '%s.pickle' % key)

    try:
        with 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,  # Python 2
            FileNotFoundError  # Python 3
        ):
        raise CacheKeyNotFoundError('Key not found in disk cache')
    return value, args_mutated
Exemple #10
0
def get_cache_path():
    return util.get_streamlit_file_path('cache')
Exemple #11
0
tests not READ from your local home directory but instead this mock
config.
"""

import os

from mock import patch, mock_open
from streamlit import config
from streamlit import util

os.environ["HOME"] = "/mock/home/folder"

CONFIG_FILE_CONTENTS = """
[global]
sharingMode = "off"
unitTest = true

[browser]
gatherUsageStats = false
"""

config_path = util.get_streamlit_file_path("config.toml")

with patch(
        "streamlit.config.open",
        mock_open(read_data=CONFIG_FILE_CONTENTS),
        create=True), patch("streamlit.config.os.path.exists") as path_exists:

    path_exists.side_effect = lambda path: path == config_path
    config.parse_config_file()