Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build_arg_string: Accept a dict containing configurable GMT parameters #2324

Merged
merged 12 commits into from
Feb 4, 2023

Conversation

seisman
Copy link
Member

@seisman seisman commented Jan 14, 2023

Description of proposed changes

Fixes #2299

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If wrapping a new module, open a 'Wrap new GMT module' issue and submit reasonably-sized PRs.
  • If adding new functionality, add an example to docstrings or tutorials.
  • Use underscores (not hyphens) in names of Python files and directories.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

@seisman seisman added the maintenance Boring but important stuff for the core devs label Jan 14, 2023
@seisman seisman added this to the 0.9.0 milestone Jan 14, 2023
@seisman seisman marked this pull request as draft January 19, 2023 07:39
@seisman seisman marked this pull request as ready for review January 28, 2023 12:57
@seisman seisman added the needs review This PR has higher priority and needs review. label Jan 29, 2023
@willschlitzer
Copy link
Contributor

Should there be a check on keys passed to confdict to make sure that they are correct configuration names? Something like:

pygmt_configs = ["COLOR_BACKGROUND", "COLOR_FOREGROUND",  "COLOR_CPT", "COLOR_NAN", "COLOR_MODEL"...]
for key in confdict:
    if key not in pygmt_configs:
        raise GMTInvalidInput(f"{key} not a valid PyGMT configuration key.")

@seisman
Copy link
Member Author

seisman commented Jan 31, 2023

Should there be a check on keys passed to confdict to make sure that they are correct configuration names? Something like:

pygmt_configs = ["COLOR_BACKGROUND", "COLOR_FOREGROUND",  "COLOR_CPT", "COLOR_NAN", "COLOR_MODEL"...]
for key in confdict:
    if key not in pygmt_configs:
        raise GMTInvalidInput(f"{key} not a valid PyGMT configuration key.")

I don't think it's necessary, because confdict is only used internally by developers (#2299 (comment)), and developers should be responsible for passing the correct configuration names.

pygmt/helpers/utils.py Outdated Show resolved Hide resolved
pygmt/helpers/utils.py Outdated Show resolved Hide resolved
pygmt/helpers/utils.py Outdated Show resolved Hide resolved
@willschlitzer
Copy link
Contributor

Should there be a check on keys passed to confdict to make sure that they are correct configuration names? Something like:

pygmt_configs = ["COLOR_BACKGROUND", "COLOR_FOREGROUND",  "COLOR_CPT", "COLOR_NAN", "COLOR_MODEL"...]
for key in confdict:
    if key not in pygmt_configs:
        raise GMTInvalidInput(f"{key} not a valid PyGMT configuration key.")

I don't think it's necessary, because confdict is only used internally by developers (#2299 (comment)), and developers should be responsible for passing the correct configuration names.

I don't think I understand; isn't the use of confdict to allow users to set GMT parameters more easily?

@seisman
Copy link
Member Author

seisman commented Feb 1, 2023

I don't think I understand; isn't the use of confdict to allow users to set GMT parameters more easily?

No, users should always use pygmt.config to set GMT parameters.

When I try to implement the Figure.timestamp() method (#2208), I need to pass some GMT parameters to the GMT C API, like:

    with Session() as lib:
        lib.call_module(
            module="plot",
            args=build_arg_string(kwdict)
            + f' --FONT_LOGO="{font}" --FORMAT_TIME_STAMP="{timefmt}"',
        )

I realize that using build_arg_string(kwdict) + f' --FONT_LOGO="{font}" --FORMAT_TIME_STAMP="{timefmt}"' is not convenient, so I propose to allow build_arg_string to accept configuration dicts. After this PR, the above code should be simplified to:

    with Session() as lib:
        lib.call_module(
            module="plot",
            args=build_arg_string(
                     kwdict, 
                     confdict={"FONT_LOGO": f"{font}", "FORMAT_TIME_STAMP": f"{timefmt}"},
            )
        )

@willschlitzer
Copy link
Contributor

Okay, that makes sense.

pygmt/helpers/utils.py Outdated Show resolved Hide resolved
pygmt/helpers/utils.py Outdated Show resolved Hide resolved
pygmt/helpers/utils.py Outdated Show resolved Hide resolved
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
@weiji14 weiji14 added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Feb 3, 2023
@seisman seisman changed the title build_arg_string: Accept a dict containing GMT's configuration key-value pairs build_arg_string: Accept a dict containing configurable GMT parameters Feb 4, 2023
@seisman seisman merged commit 6462fcb into main Feb 4, 2023
@seisman seisman deleted the build_arg_string branch February 4, 2023 12:03
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Feb 4, 2023
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Let the build_arg_string function handle GMT configurations
4 participants