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

pygmt.config: Support tab auto-completion for all GMT defaults #2213

Merged
merged 5 commits into from
Nov 30, 2022

Conversation

seisman
Copy link
Member

@seisman seisman commented Nov 26, 2022

Description of proposed changes

Let pygmt.config() support tab auto-completion by manually setting the __signature__ attribute of the pygmt.config() class, inspired by the implementation in #1282.

Screenshot:

image

Fixes #

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.

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 feature Brand new feature label Nov 26, 2022
@seisman seisman added this to the 0.8.0 milestone Nov 26, 2022
Copy link
Member

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tab autocomplete is nice! Just need to decide how to list the 135 (if I didn't miscount) parameters in https://docs.generic-mapping-tools.org/6.4/gmt.conf.html 😅

@@ -20,6 +22,15 @@ class config: # pylint: disable=invalid-name
Full GMT defaults list at :gmt-docs:`gmt.conf.html`
"""

# Manually set the __signature__ attribute to enable tab autocompletion
_keywords = ["FONT_LABEL", "FONT_TAG", "FONT_TITLE"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, would we need to manually list all the possible parameters here? I'm looking at gmt.conf.rst and https://github.com/GenericMappingTools/gmt/blob/34aa940e7a30a6dca4fc554b8dcd747dcef3e93e/src/gmt_defaults.h#L60-L211, but not sure if there's a smart way to get the whole list from the GMT C API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gmt defaults can output all the keywords and their current values. So at least we can call gmt defaults and extract the keywords from the output.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling gmt defaults is not a perfect idea, because the code will be executed when importing the pygmt package. Perhaps manually maintaining the the long list of keywords is not a bad idea because the list of GMT keywords don't change much between versions.

Copy link
Member

@weiji14 weiji14 Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I guess. Even if the list gets out of date because GMT adds new configs, people can still use it, just without tab completion.

@seisman
Copy link
Member Author

seisman commented Nov 29, 2022

The tab autocomplete is nice! Just need to decide how to list the 135 (if I didn't miscount) parameters in docs.generic-mapping-tools.org/6.4/gmt.conf.html 😅

I've manually added the long list of keywords to config.py in 2fa17a5.

The long list contains 128 normal keywords as listed by the following command:

gmt defaults | awk -F"=" '/^[^#]/ {print $1}' | sed 's/,[ \t]\?/,/g' | awk '{printf("\"%s\",\n", $1)}'

plus 8 special keywords like FONT and FONT_ANNOT, so the total number is 136.

Comment on lines 155 to 164
] + [ # special keywords
"FONT",
"FONT_ANNOT",
"FORMAT_TIME_MAP",
"MAP_ANNOT_OFFSET",
"MAP_GRID_CROSS_SIZE",
"MAP_GRID_PEN",
"MAP_TICK_LENGTH",
"MAP_TICK_PEN",
]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes more sense to also put the self.special_params attribute out of the __init__ function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was thinking about that too. Maybe create a _special_params variable to hold that dictionary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 318501b, and the variable name is _special_keywords instead of _special_params.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, just double checked one by one and it looks good!

@seisman seisman marked this pull request as ready for review November 29, 2022 15:23
@weiji14 weiji14 added enhancement Improving an existing feature final review call This PR requires final review and approval from a second reviewer and removed feature Brand new feature labels Nov 29, 2022
@seisman seisman changed the title pygmt.config: Support tab auto-completion of all parameters pygmt.config: Support tab auto-completion for all GMT defaults Nov 30, 2022
@seisman seisman merged commit b465292 into main Nov 30, 2022
@seisman seisman deleted the config-autocomplete branch November 30, 2022 14:40
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants