Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ Operations on grids:

grdinfo

GMT Defaults
------------

Operations on GMT defaults:

.. autosummary::
:toctree: generated

config

Miscellaneous
-------------
Expand Down
2 changes: 1 addition & 1 deletion pygmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .figure import Figure
from .gridding import surface
from .mathops import makecpt
from .modules import info, grdinfo, which
from .modules import config, info, grdinfo, which
from . import datasets


Expand Down
42 changes: 42 additions & 0 deletions pygmt/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,45 @@ def which(fname, **kwargs):
if not path:
raise FileNotFoundError("File '{}' not found.".format(fname))
return path


class config:
"""
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)
Binary file added pygmt/tests/baseline/test_config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions pygmt/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
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.
"""
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",
)
return fig