Skip to content

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

Merged
seisman merged 9 commits into
GenericMappingTools:mainfrom
aitorres:feature/pathlib-support
Sep 13, 2021
Merged

Add support for passing pathlib.Path objects as filenames#1382
seisman merged 9 commits into
GenericMappingTools:mainfrom
aitorres:feature/pathlib-support

Conversation

@aitorres

@aitorresaitorres commented Jul 10, 2021

Copy link
Copy Markdown
Contributor

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

welcomeBot commented Jul 10, 2021

Copy link
Copy Markdown

💖 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

weiji14 commented Jul 13, 2021

Copy link
Copy Markdown
Member

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

to say yield str(arg) perhaps?

@weiji14weiji14 added the enhancement Improving an existing feature label Jul 13, 2021
@aitorres

Copy link
Copy Markdown
ContributorAuthor

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!

@weiji14weiji14 left a comment

Copy link
Copy Markdown
Member

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

Comment threadpygmt/clib/session.py Outdated

@weiji14weiji14 left a comment

Copy link
Copy Markdown
Member

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.

@weiji14weiji14 added the final review call This PR requires final review and approval from a second reviewer label Jul 19, 2021
Comment threadpygmt/helpers/utils.py Outdated
raise GMTInvalidInput("Must provided both x and y.")

if isinstance(data, str):
if isinstance(data, (str, pathlib.Path)):

Copy link
Copy Markdown
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?

@weiji14weiji14Jul 21, 2021

Copy link
Copy Markdown
Member

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
ifisinstance(data, (str, pathlib.Path)):
ifisinstance(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
Copy Markdown
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
Copy Markdown
ContributorAuthor

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.

@weiji14weiji14 removed the final review call This PR requires final review and approval from a second reviewer label Jul 31, 2021
@weiji14weiji14 added this to the 0.5.0 milestone Aug 11, 2021
Comment threadpygmt/tests/test_info.py Outdated
Comment threadpygmt/tests/test_info.py Outdated
Comment threadpygmt/tests/test_info.py Outdated
Comment threadpygmt/tests/test_info.py
@seisman

Copy link
Copy Markdown
Member

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

@seismanseisman added the final review call This PR requires final review and approval from a second reviewer label Sep 10, 2021

@weiji14weiji14 left a comment

Copy link
Copy Markdown
Member

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.

@seismanseisman changed the title Add support for passing pathlib.Path into pygmt modulesAdd support for passing pathlib.Path objects as filenamesSep 13, 2021
@seisman
seisman merged commit 7f53b9d into GenericMappingTools:mainSep 13, 2021
@welcome

welcomeBot commented Sep 13, 2021

Copy link
Copy Markdown

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

@weiji14weiji14 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
…pingTools#1382)
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancementImproving 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

@aitorres@weiji14@seisman@michaelgrund