File: version-from-file.rst

package info (click to toggle)
python-semver 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 700 kB
  • sloc: python: 1,972; makefile: 28
file content (24 lines) | stat: -rw-r--r-- 719 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
.. _sec_reading_versions_from_file:

Reading versions from file
==========================

In cases where a version is stored inside a file, one possible solution
is to use the following function:

.. code-block:: python

    import os
    from typing import Union
    from semver.version import Version

    def get_version(path: Union[str, os.PathLike]) -> semver.Version:
        """
        Construct a Version object from a file
        
        :param path: A text file only containing the semantic version
        :return: A :class:`Version` object containing the semantic
                 version from the file.
        """
        version = open(path,"r").read().strip()
        return Version.parse(version)