Skip to content

Support upper-case file extensions #2683

Closed

Description

@seisman

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

Activity

  1. added
    feature request
    on Sep 11, 2023
  2. seisman commented on Sep 12, 2023

    @seisman
    MemberAuthor

    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.

  3. MichaelCni commented on Sep 12, 2023

    @MichaelCni

    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

  4. self-assigned this
    on Sep 17, 2023
  5. added this to the 0.11.0 milestone on Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

feature request

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions