Skip to content

Commit d063887

Browse files
authored
Merge pull request #356 from tomschr/feature/340-doc-version-file-open
Add topic to read version from file
2 parents 340b4f9 + ffe686a commit d063887

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

changelog.d/340.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Describe how to get version from a file

docs/advanced/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Advanced topics
99
display-deprecation-warnings
1010
combine-pydantic-and-semver
1111
convert-pypi-to-semver
12+
version-from-file
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.. _sec_reading_versions_from_file:
2+
3+
Reading versions from file
4+
==========================
5+
6+
In cases where a version is stored inside a file, one possible solution
7+
is to use the following function:
8+
9+
.. code-block:: python
10+
11+
from semver.version import Version
12+
13+
def get_version(path: str = "version") -> Version:
14+
"""
15+
Construct a Version from a file
16+
17+
:param path: A text file only containing the semantic version
18+
:return: A :class:`Version` object containing the semantic
19+
version from the file.
20+
"""
21+
with open(path,"r") as fh:
22+
version = fh.read().strip()
23+
return Version.parse(version)

0 commit comments

Comments
 (0)