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

Support upper-case file extensions #2683

Closed
seisman opened this issue Sep 11, 2023 · 2 comments · Fixed by #2697
Closed

Support upper-case file extensions #2683

seisman opened this issue Sep 11, 2023 · 2 comments · Fixed by #2697
Assignees
Labels
feature request New feature wanted
Milestone

Comments

@seisman
Copy link
Member

seisman commented Sep 11, 2023

Description of the desired feature

Currently, PyGMT only allows lower-case file extensions like pdf and png, but doesn't support upper-case extensions like PDF or PNG. The Windows OS is case insensitive, so both lower-case and upper-case file extensions should be allowed.

import pygmt
fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], frame=True)
fig.savefig("map.PNG")
---------------------------------------------------------------------------
GMTInvalidInput                           Traceback (most recent call last)
Cell In[4], line 1
----> 1 fig.savefig("map.PNG")

File pygmt/pygmt/figure.py:315, in Figure.savefig(self, fname, transparent, crop, anti_alias, show, **kwargs)
    310     if ext == "ps":
    311         raise GMTInvalidInput(
    312             "Extension '.ps' is not supported. "
    313             "Please use '.eps' or '.pdf' instead."
    314         )
--> 315     raise GMTInvalidInput(f"Unknown extension '.{ext}'.")
    316 fmt = fmts[ext]
    317 if transparent:

GMTInvalidInput: Unknown extension '.PNG'.

Are you willing to help implement and maintain this feature?

Maybe

@seisman seisman added the feature request New feature wanted label Sep 11, 2023
@seisman
Copy link
Member Author

seisman commented Sep 12, 2023

Supporting upper-case file extensions can be implemented by simply change the following line

ext = ext[1:] # Remove the .

to:

ext = ext[1:].lower()

However, GMT's psconvert module can only generate files with lower-case extensions. It means when users give fig.savefig("map.PNG"), it no longer raise the GMTInvalidInput error, but it saves the image as map.png, unless we rename the files manually.

@MichaelCni
Copy link

MichaelCni commented Sep 12, 2023

Hy there i can see your post and i must say

Normalization:

When dealing with file extensions provided by users or retrieved from external sources, normalize them to lowercase before processing. This ensures consistency.
Validation and Handling: Aafes Star Card

Ensure that your application correctly validates and handles files with uppercase or lowercase extensions. This includes loading, displaying, and processing the files.
File Type Mapping:

Maintain a mapping of file extensions to corresponding file types or actions in your application. Make sure this mapping is case-insensitive so that both uppercase and lowercase extensions trigger the correct behavior.
User Input Handling:

If your application allows users to input file extensions (e.g., in filters or preferences), make sure that it gracefully handles both uppercase and lowercase inputs.

Thanks and regards
MichaelCni

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants