|
1 | 1 | """Collection of utilities.""" |
2 | 2 | import re |
| 3 | +import sys |
3 | 4 | from dataclasses import dataclass |
| 5 | +from pathlib import Path |
4 | 6 |
|
5 | 7 | import roman |
| 8 | +import win32api |
6 | 9 |
|
7 | 10 |
|
8 | 11 | @dataclass |
@@ -96,3 +99,33 @@ def match_titles(section: str, title: str) -> bool: |
96 | 99 | pattern = r"^" + re.escape(sc) + r"(\b|$)" |
97 | 100 | return bool(re.match(pattern, tc)) |
98 | 101 |
|
| 102 | + |
| 103 | +def get_version() -> str: |
| 104 | + """Read the application version from version.txt file.""" |
| 105 | + try: |
| 106 | + if hasattr(__import__("sys"), "_MEIPASS"): |
| 107 | + # Pyinstaller fix |
| 108 | + exe = Path(sys.executable) |
| 109 | + info = win32api.GetFileVersionInfo(str(exe), "\\") |
| 110 | + ms = info["FileVersionMS"] |
| 111 | + ls = info["FileVersionLS"] |
| 112 | + version = (win32api.HIWORD(ms), win32api.LOWORD(ms), |
| 113 | + win32api.HIWORD(ls), win32api.LOWORD(ls)) |
| 114 | + return f"{version[0]}.{version[1]}.{version[2]}.{version[3]}" |
| 115 | + |
| 116 | + version_file = Path(__file__).resolve().parent.parent / "version.txt" |
| 117 | + if version_file.exists(): |
| 118 | + with Path.open(version_file, "r", encoding="utf-8") as f: |
| 119 | + content = f.read() |
| 120 | + # Extract filevers tuple: filevers=(1, 0, 3, 0) |
| 121 | + match = re.search( |
| 122 | + r"filevers=\((\d+),\s*(\d+),\s*(\d+),\s*(\d+)\)", content, |
| 123 | + ) |
| 124 | + if match: |
| 125 | + major, minor, patch, build = match.groups() |
| 126 | + # Ignore the build number, format as X.Y.Z |
| 127 | + return f"{major}.{minor}.{patch}" |
| 128 | + except (OSError, AttributeError): |
| 129 | + pass |
| 130 | + |
| 131 | + return None |
0 commit comments