示例#1
0
def test_simple_config_false():
    # config says the value is False.  Do we return False?
    config = Config("{\"a\": false}")
    assert config.lookup(("a", ), None) is False

    # config says the value is not known.  Do we return the default valueS?
    config = Config("{}")
    assert config.lookup(("a", ), None) is None
    assert config.lookup(("a", ), False) is False
示例#2
0
def test_cache_remove_all():
    config = Config("""{
        "a": {
             "b": {
                "c": true
             }
         }
    }""")
    config.lookup(("a", "b", "c"), {}, remove=True)
    assert not config.data
示例#3
0
def test_cache_config():
    config = Config("""{
        "caching": {
             "enabled": true,
             "size_limit": 5e9,
             "directory": "/tmp"
         }
    }""")
    cache_config = config.lookup(("caching", ), {})
    assert cache_config["enabled"]
    assert cache_config["size_limit"] == 5 * 10**9
示例#4
0
def test_lookup_deep():
    config = Config(deep_str)
    assert config.lookup(["a"]) == {"b": {"c": [1, 2, 3]}}
    assert config.lookup(["a", "b"]) == {"c": [1, 2, 3]}
    assert config.lookup(["a", "b", "c"]) == [1, 2, 3]
    with raises(AttributeError):
        config.lookup(["a", "b", "c", "d"])
    assert config.lookup(["a", "b", "c", "d"], "x") == "x"
示例#5
0
def test_cache_remove_step_wise():
    config = Config("""{
        "a": {
             "b": {
                "c": true,
                "d": true
             },
             "e": 1
         },
         "f": 2
    }""")
    config.lookup(("a", "b", "c"), {}, remove=True)
    assert "c" not in config.data["a"]["b"]
    config.lookup(("a", "b"), {}, remove=True)
    assert "b" not in config.data["a"]
    config.lookup(("a", ), {}, remove=True)
    assert "a" not in config.data
示例#6
0
class StarfishConfig(object):
    """
    Application specific configuration settings which can be loaded throughout
    the starfish codebase.

    Attributes
    ----------
    slicedimage : dictionary
        Subdictionary that can be passed to slicedimage.io methods.
    strict : bool
        Whether or not loaded json should be validated.
    verbose : bool
        Controls output like from tqdm

    Examples
    --------
    Check strict property

        >>> from starfish.core.config import StarfishConfig
        >>> config = StarfishConfig()
        >>> if config.strict:
        >>>     validate(json)

    Default starfish configuration equivalent:

        >>> {
        >>>     "slicedimage": {
        >>>         "caching": {
        >>>             "debug": false,
        >>>             "directory": "~/.starfish/cache",
        >>>             "size_limit": 5e9
        >>>         },
        >>>     },
        >>>     "validation": {
        >>>         "strict": false
        >>>     },
        >>>     "verbose": true
        >>> }

    Example of a ~/.starfish.config file to disable caching:

        >>> {
        >>>     "slicedimage": {
        >>>         "caching": {
        >>>             "size_limit": 0
        >>>         }
        >>>     }
        >>> }

    """

    def __init__(self) -> None:
        """
        Loads the configuration specified by the STARFISH_CONFIG environment variable.

        Parameters
        ----------
        STARISH_CONFIG :
            This parameter is read from the environment to permit setting configuration
            values either directly or via a file. Keys read include:

             - ["slicedimage"]["caching"]["directory"]   (default: ~/.starfish/cache)
             - ["slicedimage"]["caching"]["size_limit"]  (default: None; 0 disables caching)
             - ["validation"]["strict"]                  (default: False)
             - ["verbose"]                               (default: True)

            Note: all keys can also be set by and environment variable constructed from the
            key parts and prefixed with STARFISH, e.g. STARFISH_VALIDATION_STRICT.
        """
        config = os.environ.get("STARFISH_CONFIG", "@~/.starfish/config")
        self._config_obj = Config(config)
        self._env_keys = [
            x for x in os.environ.keys()
            if special_prefix(x) and x != "STARFISH_CONFIG"]

        # If no directory is set, then force the default
        self._slicedimage = self._config_obj.lookup(("slicedimage",), NestedDict(), remove=True)
        if not self._slicedimage["caching"]["directory"]:
            self._slicedimage["caching"]["directory"] = "~/.starfish/cache"
        self._slicedimage_update(('caching', 'directory'))
        self._slicedimage_update(('caching', 'size_limit'), int)

        self._strict = self._config_obj.lookup(
            ("validation", "strict"), self.flag("STARFISH_VALIDATION_STRICT", "false"), remove=True)

        self._verbose = self._config_obj.lookup(
            ("verbose",), self.flag("STARFISH_VERBOSE", "true"), remove=True)

        if self._config_obj.data:
            warnings.warn(f"unknown configuration: {self._config_obj.data}")
        if self._env_keys:
            warnings.warn(f"unknown environment variables: {self._env_keys}")

    def _slicedimage_update(self, lookup, parse=lambda x: x):
        """ accept STARFISH_SLICEDIMAGE_ or SLICEDIMAGE_ prefixes"""

        value = None

        name1 = "SLICEDIMAGE_" + "_".join([x.upper() for x in lookup])
        if name1 in os.environ:
            self._env_keys.remove(name1)
            value = parse(os.environ[name1])

        name2 = "STARFISH_" + name1
        if name2 in os.environ:
            if value:
                warnings.warn(f"duplicate variable: (STARFISH_){name1}")
            self._env_keys.remove(name2)
            value = parse(os.environ[name2])

        if value is None:
            return  # Nothing found

        v = self._slicedimage
        for k in lookup[:-1]:
            v = v[k]
        v[lookup[-1]] = value

    def flag(self, name, default_value=""):

        if name in os.environ:
            value = os.environ[name]
            self._env_keys.remove(name)
        else:
            value = default_value

        if isinstance(value, str):
            value = value.lower()
            return value in ("true", "1", "yes", "y", "on", "active", "enabled")

    @property
    def slicedimage(self):
        return dict(self._slicedimage)

    @property
    def strict(self):
        return self._strict

    @property
    def verbose(self):
        return self._verbose
示例#7
0
def test_lookup_dne():
    config = Config(simple_str)
    with raises(KeyError):
        config.lookup(["foo"])
    assert config.lookup(["foo"], 1) == 1
    assert config.lookup(["foo", "bar"], 2) == 2