def test_settings(): with settings.temporary(): settings.reset() assert settings.get("plotting-options") == {}, "Check 1" settings.set("plotting-options", width=400) assert settings.get("plotting-options") == {"width": 400} settings.reset("plotting-options") assert settings.get("plotting-options") == {}, "Check 2" settings.set("plotting-options", {"width": 400}) assert settings.get("plotting-options") == {"width": 400} settings.reset() assert settings.get("plotting-options") == {}, "Check 3" with pytest.raises(TypeError): settings.set("plotting-options", 3) settings.set("styles-directories", ["/a", "/b"]) assert settings.get("styles-directories") == ["/a", "/b"] settings.set("styles-directories", "/c", "/d") assert settings.get("styles-directories") == ["/c", "/d"] with pytest.raises(KeyError): settings.set("test", 42) with pytest.raises(KeyError): settings.get("test") with pytest.raises(ValueError): settings.set("url-download-timeout", "1M")
def check_user_defined_objects(collection, setting, obj, tree, get_list, get_entry): # Clear cache clear_cache() paths = settings.get(setting) assert isinstance(paths, (list, tuple)), paths assert len(paths) > 0 for i, path in enumerate(paths): name = "pytest-%s-%s" % (tree[1], i) if os.path.exists(path) and os.path.isdir(path): assert path in directories(), directories() if not os.path.exists(path): os.mkdir(path) with open(os.path.join(path, "%s.yaml" % (name, )), "w") as f: a = obj for t in tree[:-1]: a = a[t] a[tree[-1]] = i yaml.dump(obj, f, default_flow_style=False) for i in range(len(paths)): name = "pytest-%s-%s" % (tree[1], i) get_data_entry(collection, name) assert name in get_list() p = get_entry(name).data a = p for t in tree: a = a[t] assert a == i # TODO: Move to tear-down for i, path in enumerate(paths): name = "pytest-%s-%s" % (tree[1], i) os.unlink(os.path.join(path, "%s.yaml" % (name, )))
def directories(owner: bool = False) -> list: """Return a list of directories that are used in the project . If owner = False, return a list of directories where to search for plugins. If owner = True, return a list of 2-uples to include the owner in the return value. Parameters ---------- owner : bool, optional """ result = [] for conf in ( "styles-directories", "projections-directories", "layers-directories", "datasets-directories", ): for d in settings.get(conf): if os.path.exists(d) and os.path.isdir(d): result.append(("user-settings", d)) for kind in ("dataset", "source"): for name, v in load_plugins(kind).items(): try: module = import_module(v.module_name) result.append((name, os.path.dirname(module.__file__))) except Exception: LOG.error("Cannot load module %s", v.module_name, exc_info=True) result.append(("climetlab", os.path.dirname(climetlab.__file__))) if owner: return result return [x[1] for x in result]
def do_settings(self, args): """ Display or change CliMetLab settings. See https://climetlab.readthedocs.io/guide/settings.html for more details. Examples: climetlab settings cache-directory /big-disk/climetlab-cache """ from climetlab import settings words = args.args if len(words) == 0: if args.json: result = {} for f in settings.dump(): result[f[0]] = f[1] print(json.dumps(result, indent=4, sort_keys=True)) return print_table((f[0], f[1]) for f in settings.dump()) return if len(words) == 1: name = words[0] print(settings.get(name)) return if len(words) == 2: name = words[0] value = words[1] settings.set(name, value) if len(words) == 3: name = words[0] key = words[1] value = words[2] settings.set(name, {key: value})
def test_temporary(): settings.reset() settings.set("styles-directories", "/c", "/d") settings.set("plotting-options", {"width": 400}) with settings.temporary("plotting-options", {"width": 100}): assert settings.get("styles-directories") == ["/c", "/d"] assert settings.get("plotting-options") == {"width": 100}, settings.get( "plotting-options" ) settings.set("plotting-options", {"width": 200}) assert settings.get("plotting-options") == {"width": 200} settings.reset() assert settings.get("plotting-options") == {} settings.set("plotting-options", {"width": 400}) settings.set("styles-directories", "/c", "/d") settings.reset() assert settings.get("plotting-options") == {}
def test_numbers(): with temp_directory() as tmpdir: with settings.temporary("cache-directory", tmpdir): settings.set("url-download-timeout", 30) assert settings.get("url-download-timeout") == 30 settings.set("url-download-timeout", "30") assert settings.get("url-download-timeout") == 30 settings.set("url-download-timeout", "30s") assert settings.get("url-download-timeout") == 30 settings.set("url-download-timeout", "2m") assert settings.get("url-download-timeout") == 120 settings.set("url-download-timeout", "10h") assert settings.get("url-download-timeout") == 36000 settings.set("url-download-timeout", "7d") assert settings.get("url-download-timeout") == 7 * 24 * 3600 with pytest.raises(ValueError): settings.set("url-download-timeout", "1x") settings.set("maximum-cache-size", "1") assert settings.get("maximum-cache-size") == 1 settings.set("maximum-cache-size", "1k") assert settings.get("maximum-cache-size") == 1024 settings.set("maximum-cache-size", "1kb") assert settings.get("maximum-cache-size") == 1024 settings.set("maximum-cache-size", "1k") assert settings.get("maximum-cache-size") == 1024 settings.set("maximum-cache-size", "1kb") assert settings.get("maximum-cache-size") == 1024 settings.set("maximum-cache-size", "1K") assert settings.get("maximum-cache-size") == 1024 settings.set("maximum-cache-size", "1M") assert settings.get("maximum-cache-size") == 1024 * 1024 settings.set("maximum-cache-size", "1G") assert settings.get("maximum-cache-size") == 1024 * 1024 * 1024 settings.set("maximum-cache-size", "1T") assert settings.get("maximum-cache-size") == 1024 * 1024 * 1024 * 1024 settings.set("maximum-cache-size", "1P") assert ( settings.get("maximum-cache-size") == 1024 * 1024 * 1024 * 1024 * 1024 ) settings.set("maximum-cache-size", None) assert settings.get("maximum-cache-size") is None settings.set("maximum-cache-disk-usage", "2%") assert settings.get("maximum-cache-disk-usage") == 2