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

Add support for passing pathlib.Path objects as filenames #1382

Merged
merged 9 commits into from
Sep 13, 2021
Merged

Add support for passing pathlib.Path objects as filenames #1382

merged 9 commits into from
Sep 13, 2021

Conversation

aitorres
Copy link
Contributor

@aitorres aitorres commented Jul 10, 2021

Description of proposed changes

This PR addresses and fixes issue #1381. By adding support to pathlib.Path values that represent file paths to the data_kind helper function, it can be used interchangeably with file name strings on pygmt modules.

Fixes #1381

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 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

@welcome
Copy link

welcome bot commented Jul 10, 2021

💖 Thanks for opening this pull request! 💖

Please make sure you read our contributing guidelines and abide by our code of conduct.

A few things to keep in mind:

  • If you need help writing tests, take a look at the existing ones for inspiration. If you don't know where to start, let us know and we'll walk you through it.
  • All new features should be documented. It helps to write the docstrings for your functions/classes before writing the code. This will help you think about your code design and results in better code.
  • No matter what, we are really grateful that you put in the effort to do this! 🎉

@weiji14
Copy link
Member

weiji14 commented Jul 13, 2021

Hmm, supporting pathlib.Path might not be as simple as I thought. We'll need to convert the pathlib.Path object into a str in order for the GMT C API to understand it (the new test_info_path unit test is currently failing at https://github.com/GenericMappingTools/pygmt/pull/1382/checks?check_run_id=3040356636#step:11:589). Do you want to walk through the virtualfile_from_data function at https://github.com/GenericMappingTools/pygmt/blob/master/pygmt/clib/session.py and see if you can find a good place to do the pathlib.Path->str conversion? Or we could change the line at

yield arg
to say yield str(arg) perhaps?

@weiji14 weiji14 added the enhancement Improving an existing feature label Jul 13, 2021
@aitorres
Copy link
Contributor Author

I'm sorry for the delay! I've examined the virtualfile_from_data function and modified the conditional statement to cast data to str in presence of "file" data kind, instead of modifying the dummy_context function.

I've pushed my changes to my branch already, and will keep an eye on the tests in case something fails again. Thanks for your help!

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.

I'm sorry for the delay! I've examined the virtualfile_from_data function and modified the conditional statement to cast data to str in presence of "file" data kind, instead of modifying the dummy_context function.

Nice one, that's a much better solution! Will see if the tests pass on all platforms (Linux/macOS/Windows).

pygmt/clib/session.py Outdated Show resolved Hide resolved
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.

Thanks @aitorres, looks good. I'll merge in 24hrs unless any of the other devs have something to add.

Edit: Please resolve #1382 (comment) before we can proceed.

@weiji14 weiji14 added the final review call This PR requires final review and approval from a second reviewer label Jul 19, 2021
@@ -66,7 +71,7 @@ def data_kind(data, x=None, y=None, z=None):
if data is None and (x is None or y is None):
raise GMTInvalidInput("Must provided both x and y.")

if isinstance(data, str):
if isinstance(data, (str, pathlib.Path)):
Copy link
Member

Choose a reason for hiding this comment

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

What will happen if people use pathlib.PurePath or path.PurePosixPath?

Copy link
Member

@weiji14 weiji14 Jul 21, 2021

Choose a reason for hiding this comment

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

The answer at https://stackoverflow.com/questions/58647584/how-to-test-if-object-is-a-pathlib-path/58966089#58966089 suggests using isinstance(pathlib.PurePath). @aitorres, could you perhaps see if this works?

Suggested change
if isinstance(data, (str, pathlib.Path)):
if isinstance(data, (str, pathlib.PurePath)):

Also, it might be a good idea to test that PureWindowsPath and PurePosixPath objects work on Windows and macOS/Linux respectively. Let us know if you're able to update the unit test to do that. If not, we can also just ignore it.

Copy link
Member

Choose a reason for hiding this comment

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

Gentle ping @aitorres to see if you're still available to finish up this PR! If not, we can get one of the maintainers to finish this up in the next week or so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Really sorry for the delay! I've just added and pushed the changes to use PurePath, and added two tests that specifically use PureWindowsPath and PurePosixPath.

@weiji14 weiji14 removed the final review call This PR requires final review and approval from a second reviewer label Jul 31, 2021
@weiji14 weiji14 added this to the 0.5.0 milestone Aug 11, 2021
pygmt/tests/test_info.py Outdated Show resolved Hide resolved
pygmt/tests/test_info.py Outdated Show resolved Hide resolved
pygmt/tests/test_info.py Outdated Show resolved Hide resolved
pygmt/tests/test_info.py Show resolved Hide resolved
@seisman
Copy link
Member

seisman commented Aug 29, 2021

Ping @aitorres to finalize this PR if you have time. Otherwise, I'll take over this PR and make edits to this branch.

@seisman seisman added the final review call This PR requires final review and approval from a second reviewer label Sep 10, 2021
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.

Should be ok to merge after tests all pass, but prefer to have a second approval.

@seisman seisman changed the title Add support for passing pathlib.Path into pygmt modules Add support for passing pathlib.Path objects as filenames Sep 13, 2021
@seisman seisman merged commit 7f53b9d into GenericMappingTools:main Sep 13, 2021
@welcome
Copy link

welcome bot commented Sep 13, 2021

🎉🎉🎉 Congrats on merging your first pull request and welcome to the team! 🎉🎉🎉

Please open a new pull request to add yourself to the AUTHORS.md file. We hope that this was a good experience for you. Let us know if there is any way that the contributing process could be improved.

@weiji14 weiji14 removed the final review call This PR requires final review and approval from a second reviewer label Sep 18, 2021
sixy6e pushed a commit to sixy6e/pygmt that referenced this pull request Dec 21, 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.

Support passing in pathlib.Path into pygmt modules
4 participants