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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing arguments containing spaces into pygmt functions #1487

Merged
merged 23 commits into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b5ad405
Replace spaces in arguments with octal code 040
weiji14 Sep 7, 2021
14648c2
Remove workarounds for spaces in fig.subplot's autolabel and title args
weiji14 Sep 7, 2021
924f439
Remove workaround for spaces in fig.text's -F argument
weiji14 Sep 7, 2021
707745f
Remove double quotes around legend label test examples
weiji14 Sep 7, 2021
03e4308
Edit test_rose_no_sectors to remove single quotes from title
weiji14 Sep 7, 2021
095449a
Remove workaround for spaces in fig.psconvert prefix
weiji14 Sep 7, 2021
d81b80b
Format using blackdoc and fix flake8 error
weiji14 Sep 7, 2021
dd849cb
Merge branch 'main' into arg_with_space
weiji14 Jan 10, 2022
c29e632
Ensure spaces in pygmt.config arguments can work
weiji14 Jan 10, 2022
83c8c3b
Manually handle prefix -F in psconvert
weiji14 Jan 13, 2022
2ba8420
Merge branch 'main' into arg_with_space
weiji14 Jan 13, 2022
b58af8b
Merge branch 'main' into arg_with_space
weiji14 Mar 6, 2022
3ec7727
Handle PROJ4 strings with spaces
weiji14 Mar 6, 2022
f19c41b
Use Modifier Letter Colon instead of regular colon to fix WIndows tests
weiji14 Mar 6, 2022
7a518a1
Merge branch 'main' into arg_with_space
weiji14 Mar 7, 2022
9774d5e
Try using underscore instead of Modifier Letter Colon
weiji14 Mar 7, 2022
ff40d27
Refactor the whole function using a single loop
weiji14 Mar 10, 2022
36fdec5
Merge branch 'main' into arg_with_space
weiji14 Mar 10, 2022
6c399ba
Merge branch 'main' into arg_with_space
weiji14 Mar 11, 2022
4770396
Merge branch 'main' into arg_with_space
weiji14 Mar 12, 2022
801ba01
Raise GMTInvalidInput if no prefix argument is passed to psconvert
weiji14 Mar 13, 2022
ecb580e
Merge branch 'main' into arg_with_space
weiji14 Mar 13, 2022
a526d45
Fix rst syntax in pygmt/helpers/utils.py
weiji14 Mar 13, 2022
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
Prev Previous commit
Next Next commit
Manually handle prefix -F in psconvert
So that fig.savefig won't insert `\040` characters when saving filenames with spaces. Resolves problem mentioned in https://github.com/GenericMappingTools/pygmt/pull/1487/files#r703116544
  • Loading branch information
weiji14 committed Jan 13, 2022
commit 83c8c3b0e8c699cd52e7d539c49dabbbf7bf384c
7 changes: 6 additions & 1 deletion pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,13 @@ def psconvert(self, **kwargs):
# Default cropping the figure to True
if "A" not in kwargs:
kwargs["A"] = ""
# Manually handle prefix -F argument so spaces aren't converted to \040
# by build_arg_string function. For more information, see
# https://github.com/GenericMappingTools/pygmt/pull/1487
prefix = kwargs.pop("F")
Copy link
Member

@maxrjones maxrjones Mar 12, 2022

Choose a reason for hiding this comment

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

Should there be a check before this that prefix is provided so that the user doesn't get a KeyError here? Or you could raise GMTInvalidInput in an except statement.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Looking at the docs at https://docs.generic-mapping-tools.org/6.3/psconvert.html, it reads like prefix/-F is optional, but when I tried running psconvert without the -F option, it throws an error: psconvert [ERROR]: Modern GMT mode requires the -F option. See https://github.com/GenericMappingTools/gmt/blob/adb244afa51ca7246cc051080c9d47193087d6c2/src/psconvert.c#L1041-L1042

So since PyGMT is modern mode only, it should be fine to keep this line intact.

Copy link
Member

Choose a reason for hiding this comment

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

The docs say "If no input files are given, will convert the current active figure (see pygmt.figure). In this case, an output name must be given using parameter prefix." How does one provide other input files than the current figure using pygmt.Figure.psconvert?

If we chose not to support operations analogous to psconvert -Tg test.ps (which uses the same prefix as the original file name), I think we should make prefix required in the function signature or raise an invalid input exception to avoid the traceback below. I don't think it's obvious to that the error is caused by not using prefix.

image

Copy link
Member Author

Choose a reason for hiding this comment

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

How does one provide other input files than the current figure using pygmt.Figure.psconvert?

We can't, because it has not been implemented yet. The current fig.psconvert is a method of the pygmt.Figure() class and is tied to the fig, and can only 'psconvert' the active figure.

If we chose not to support operations analogous to psconvert -Tg test.ps (which uses the same prefix as the original file name), I think we should make prefix required in the function signature or raise an invalid input exception to avoid the traceback below. I don't think it's obvious to that the error is caused by not using prefix.

Ok, I've added a try-except block in 801ba01 to raise a GMTInvalidInput if prefix/-F is not given.


with Session() as lib:
lib.call_module("psconvert", build_arg_string(kwargs))
lib.call_module("psconvert", f'-F"{prefix}" {build_arg_string(kwargs)}')

def savefig(
self, fname, transparent=False, crop=True, anti_alias=True, show=False, **kwargs
Expand Down
2 changes: 1 addition & 1 deletion pygmt/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_figure_savefig_filename_with_spaces():
fig = Figure()
fig.basemap(region=[0, 1, 0, 1], projection="X1c/1c", frame=True)
with GMTTempFile(prefix="pygmt-filename with spaces", suffix=".png") as imgfile:
fig.savefig(imgfile.name)
fig.savefig(fname=imgfile.name)
assert r"\040" not in os.path.abspath(imgfile.name)
assert os.path.exists(imgfile.name)

Expand Down