diff --git a/doc/api/index.rst b/doc/api/index.rst index 301aac99e0c..fb481441fbd 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -72,6 +72,15 @@ Operations on grids: grdinfo grdtrack +GMT Defaults +------------ + +Operations on GMT defaults: + +.. autosummary:: + :toctree: generated + + config Miscellaneous ------------- diff --git a/pygmt/__init__.py b/pygmt/__init__.py index fd6e8626d7c..712ea64e8d5 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -17,7 +17,7 @@ from .gridding import surface from .sampling import grdtrack from .mathops import makecpt -from .modules import info, grdinfo, which +from .modules import config, info, grdinfo, which from . import datasets diff --git a/pygmt/modules.py b/pygmt/modules.py index 12e1f0e7c7b..1d96fc3e11b 100644 --- a/pygmt/modules.py +++ b/pygmt/modules.py @@ -142,3 +142,45 @@ def which(fname, **kwargs): if not path: raise FileNotFoundError("File '{}' not found.".format(fname)) return path + + +class config: # pylint: disable=invalid-name + """ + Set GMT defaults globally or locally. + + Change GMT defaults globally:: + + pygmt.config(PARAMETER=value) + + Change GMT defaults locally by using it as a context manager:: + + with pygmt.config(PARAMETER=value): + ... + + Full GMT defaults list at :gmt-docs:`gmt.conf.html` + """ + + def __init__(self, **kwargs): + # Save values so that we can revert to their initial values + self.old_defaults = {} + with Session() as lib: + for key in kwargs: + self.old_defaults[key] = lib.get_default(key) + + # call gmt set to change GMT defaults + arg_str = " ".join( + ["{}={}".format(key, value) for key, value in kwargs.items()] + ) + with Session() as lib: + lib.call_module("set", arg_str) + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + # revert to initial values + arg_str = " ".join( + ["{}={}".format(key, value) for key, value in self.old_defaults.items()] + ) + with Session() as lib: + lib.call_module("set", arg_str) diff --git a/pygmt/tests/baseline/test_config.png b/pygmt/tests/baseline/test_config.png new file mode 100644 index 00000000000..d234eb2684c Binary files /dev/null and b/pygmt/tests/baseline/test_config.png differ diff --git a/pygmt/tests/test_config.py b/pygmt/tests/test_config.py new file mode 100644 index 00000000000..7b499f355f5 --- /dev/null +++ b/pygmt/tests/test_config.py @@ -0,0 +1,37 @@ +""" +Tests for gmt config +""" +import pytest + +from .. import Figure, config + + +@pytest.mark.mpl_image_compare +def test_config(): + """ + Test if config works globally and locally. + """ + # Change global settings + config(FONT_ANNOT_PRIMARY="blue") + fig = Figure() + fig.basemap( + region="0/10/0/10", projection="X10c/10c", frame=["af", '+t"Blue Annotation"'] + ) + + with config(FONT_LABEL="red", FONT_ANNOT_PRIMARY="red"): + fig.basemap( + region="0/10/0/10", + projection="X10c/10c", + frame=['xaf+l"red label"', "yaf", '+t"red annotation"'], + X="15c", + ) + + fig.basemap( + region="0/10/0/10", + projection="X10c/10c", + frame=["af", '+t"Blue Annotation"'], + X="15c", + ) + # Revert to default settings + config(FONT_ANNOT_PRIMARY="black") + return fig