← 输入框|github.com|刷新
Skip to content

version file open method #340

Closed
Closed

Description

@curtisma

Situation

I would like to have a simple text file in my repo, named "version", which contains only the semantic version number of the project. This could then be consumed by every place in the code that needs to know the package number.

For example, I may need it for the version attribute of the package, again for the documentation, and again for the Pip or Conda packaging configuration. Having all these share a single, simple source for the version number would make it easy to update the version number. It would also make it easy to get the version number for tools and packages written in other languages since it is stored as a simple text file that almost all languages can read. Especially since you have a CLI that would make it easy to use with other languages also.

Possible Solution/Idea

I propose adding a static or class method "open" which takes a string or path object pointing to the version file. The version file will only contain the semantic version. The method will read in the file and return a Version object.

Additional context

While it seems easy enough to open the file and pass it to the Version constructor as a string, this would require adding scripts or modules to each project to add this basic functionality. Python is all about "batteries included", right?

Activity

  1. added
    Enhancement
    on Aug 27, 2021
  2. curtisma commented on Aug 27, 2021

    @curtisma
    Author

    It should be as simple as the following first draft of the function. It would need to be converted to a static or class method of the Version class.

    from semver.version import Version
    
    def get_version(file_path: str = "./version") -> Version:
        """
        Construct a Version from a file
        
        Args:
            file_path: A text file only containing the semantic version
    
        Returns: A Version object containing the semantic version from the file.
    
        """
        with open(file_path,"r") as file:
            version = file.read()
        return Version.parse(version)
  3. tomschr commented on Aug 31, 2021

    @tomschr
    Member

    Thanks @curtisma for your idea!

    I'm not sure if this specific use case is appropriate for the Version class. It sounds a bit too special for my taste. I would prefer to keep the class as small as possible.

    From my gut feelings, this would be an example of a customization by a user. I'd add it to the documentation of how to customize the class.

    However, that's my taste and I could be wrong. Maybe you have some convincing arguments why this could be useful for more people?

    @tlaferriere What do you think?

  4. added
    Doc
    on Jan 11, 2022
  5. added a commit that references this issue on Feb 24, 2022
    ffe686a
  6. curtisma commented on Feb 26, 2022

    @curtisma
    Author

    Awesome, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocEnhancement

    Type

    No type

    Fields

    SIze

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions