|
1 | 1 | """Handling of PEtab version numbers.""" |
2 | 2 | from __future__ import annotations |
3 | 3 |
|
| 4 | +import re |
4 | 5 | from pathlib import Path |
5 | 6 |
|
6 | 7 | import petab |
7 | | -from petab.v1 import Problem as V1Problem |
8 | | -from petab.v1.C import FORMAT_VERSION |
9 | | -from petab.v1.yaml import load_yaml |
10 | 8 |
|
11 | 9 | __all__ = [ |
12 | 10 | "get_major_version", |
| 11 | + "parse_version", |
13 | 12 | ] |
| 13 | +from . import v1 |
| 14 | + |
| 15 | +# version regex pattern |
| 16 | +_version_pattern = ( |
| 17 | + r"(?P<major>\d+)(?:\.(?P<minor>\d+))?" |
| 18 | + r"(?:\.(?P<patch>\d+))?(?P<suffix>[\w.]+)?" |
| 19 | +) |
| 20 | +_version_re = re.compile(_version_pattern) |
| 21 | + |
| 22 | + |
| 23 | +def parse_version(version: str | int) -> tuple[int, int, int, str]: |
| 24 | + """Parse a version string into a tuple of integers and suffix.""" |
| 25 | + if isinstance(version, int): |
| 26 | + return version, 0, 0, "" |
| 27 | + |
| 28 | + version = str(version) |
| 29 | + match = _version_re.match(version) |
| 30 | + if match is None: |
| 31 | + raise ValueError(f"Invalid version string: {version}") |
| 32 | + |
| 33 | + major = int(match.group("major")) |
| 34 | + minor = int(match.group("minor") or 0) |
| 35 | + patch = int(match.group("patch") or 0) |
| 36 | + suffix = match.group("suffix") or "" |
| 37 | + |
| 38 | + return major, minor, patch, suffix |
14 | 39 |
|
15 | 40 |
|
16 | 41 | def get_major_version( |
17 | | - problem: str | dict | Path | V1Problem | petab.v2.Problem, |
| 42 | + problem: str | dict | Path | petab.v1.Problem | petab.v2.Problem, |
18 | 43 | ) -> int: |
19 | 44 | """Get the major version number of the given problem.""" |
20 | 45 | version = None |
21 | 46 |
|
22 | 47 | if isinstance(problem, str | Path): |
| 48 | + from petab.v1.yaml import load_yaml |
| 49 | + |
23 | 50 | yaml_config = load_yaml(problem) |
24 | | - version = yaml_config.get(FORMAT_VERSION) |
| 51 | + version = yaml_config.get(v1.C.FORMAT_VERSION) |
25 | 52 | elif isinstance(problem, dict): |
26 | | - version = problem.get(FORMAT_VERSION) |
| 53 | + version = problem.get(v1.C.FORMAT_VERSION) |
27 | 54 |
|
28 | 55 | if version is not None: |
29 | 56 | version = str(version) |
30 | 57 | return int(version.split(".")[0]) |
31 | 58 |
|
32 | | - if isinstance(problem, V1Problem): |
| 59 | + if isinstance(problem, petab.v1.Problem): |
33 | 60 | return 1 |
34 | 61 |
|
35 | 62 | from . import v2 |
|
0 commit comments