diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index b0b0e8c..f6b07e5 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,12 @@ Changelog ========= +v1.0.5 (2024-09-25) +------------------- + +- str(TripletVersion) now returns XYZ instead of X.Y.Z to make serialization work inside AntaresWeb + + v1.0.4 (2024-09-24) ------------------- diff --git a/docs/usage.md b/docs/usage.md index f67de1b..e8af5a8 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -15,7 +15,7 @@ Using the `antares-study-version` module is straightforward: from antares.study.version import StudyVersion version = StudyVersion(8, 7, 2) # patch component is not used -print(version) # 8.7 +print(version) # 870 ``` You can also create a version object from a dotted string: @@ -24,7 +24,7 @@ You can also create a version object from a dotted string: from antares.study.version import StudyVersion version = StudyVersion.parse("8.7") -print(version) # 8.7 +print(version) # 870 ``` You can create a version object from a compact string: @@ -33,7 +33,7 @@ You can create a version object from a compact string: from antares.study.version import StudyVersion version = StudyVersion.parse("870") -print(version) # 8.7 +print(version) # 870 ``` You can create a version object from an integer: @@ -42,7 +42,7 @@ You can create a version object from an integer: from antares.study.version import StudyVersion version = StudyVersion.parse(870) -print(version) # 8.7 +print(version) # 870 ``` You can compare versions: @@ -61,10 +61,10 @@ You can convert a version to string using format specifiers: from antares.study.version import StudyVersion version = StudyVersion(8, 7) -print(f"{version}") # 8.7 +print(f"{version}d.d.d") # 8.7 print(f"{version:02d}") # 08.07 print(f"{version:03d}") # 08.07.00 -print(f"{version:ddd}") # 870 +print(version) # 870 ``` You can convert a version to an integer: @@ -84,7 +84,7 @@ Of course, the same operations can be done with `SolverVersion` objects, but wit from antares.study.version import SolverVersion version = SolverVersion(8, 7, 2) -print(version) # 8.7.2 +print(version) # 872 ``` Objects of the `StudyVersion` and `SolverVersion` classes can be compared to each other: diff --git a/src/antares/study/version/__about__.py b/src/antares/study/version/__about__.py index 56dd7de..5c784b9 100644 --- a/src/antares/study/version/__about__.py +++ b/src/antares/study/version/__about__.py @@ -4,7 +4,7 @@ # Standard project metadata -__version__ = "1.0.4" +__version__ = "1.0.5" __author__ = "RTE, Antares Web Team" __date__ = "2024-07-06" __credits__ = "© Réseau de Transport de l’Électricité (RTE)" diff --git a/src/antares/study/version/model/study_antares.py b/src/antares/study/version/model/study_antares.py index d59297c..7c3bd58 100644 --- a/src/antares/study/version/model/study_antares.py +++ b/src/antares/study/version/model/study_antares.py @@ -168,7 +168,7 @@ def to_ini_file(self, study_dir: t.Union[str, Path], update_save_date: bool = Tr if self.version < DOTTED_VERSION: # type: ignore # Old versions of Antares Studies used a different format for the version number - section_dict["version"] = f"{self.version:ddd}" + section_dict["version"] = f"{self.version:}" else: section_dict["version"] = f"{self.version.major}.{self.version.minor}" diff --git a/src/antares/study/version/model/study_version.py b/src/antares/study/version/model/study_version.py index eb4a36f..6b65af6 100644 --- a/src/antares/study/version/model/study_version.py +++ b/src/antares/study/version/model/study_version.py @@ -32,12 +32,7 @@ def parse(cls: t.Type[T], other: object) -> T: # Conversion methods def __str__(self) -> str: - if self.patch: - return f"{self.major}.{self.minor}.{self.patch}" - elif self.minor: - return f"{self.major}.{self.minor}" - else: - return f"{self.major}" + return f"{int(self):03d}" def __int__(self) -> int: return self.major * 100 + self.minor * 10 + self.patch @@ -95,12 +90,12 @@ def __format__(self, format_spec: str) -> str: """ Format the version number "X.Y.Z" according to the format specifier: - - "" => "X.Y.Z" or "X.Y" if patch is 0, or "X" if minor is 0. + - "" => "XYZ" - "1d" => "X", - "2d" => "X.Y", - "01d" => "0X", - "02d" => "0X.0Y" - - "ddd" => "XYZ" + - "d.d.d" => "X.Y.Z" or "X.Y" if patch is 0, or "X" if minor is 0. :param format_spec: format specifier. @@ -121,8 +116,13 @@ def __format__(self, format_spec: str) -> str: return f"{major:02d}.{minor:02d}" elif format_spec == "03d": return f"{major:02d}.{minor:02d}.{patch:02d}" - elif format_spec == "ddd": - return f"{int(self):03d}" + elif format_spec == "d.d.d": + if self.patch: + return f"{self.major}.{self.minor}.{self.patch}" + elif self.minor: + return f"{self.major}.{self.minor}" + else: + return f"{self.major}" else: raise ValueError(f"Invalid format specifier: '{format_spec}'") diff --git a/tests/create_app/test_create_app.py b/tests/create_app/test_create_app.py index 8aee8c6..345d78d 100644 --- a/tests/create_app/test_create_app.py +++ b/tests/create_app/test_create_app.py @@ -18,7 +18,7 @@ def test_no_template_available(self, tmp_path: Path): study_dir = tmp_path.joinpath("my-new-study") study_version = StudyVersion.parse("2.8") app = CreateApp(study_dir=study_dir, caption="My New App", version=study_version, author="Robert Smith") - with pytest.raises(ApplicationError, match=re.escape(f"{study_version:2d}")): + with pytest.raises(ApplicationError, match=re.escape(str(study_version))): app() @pytest.mark.parametrize("study_version", AVAILABLE_VERSIONS) diff --git a/tests/test_cli.py b/tests/test_cli.py index 66e434d..a5c2507 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -53,7 +53,7 @@ def test_cli__create(self, tmp_path: Path, study_version: StudyVersion) -> None: parser.read(study_antares_file, encoding="utf-8") section_dict = dict(parser["antares"]) if study_version < 900: - expected_version = f"{study_version:ddd}" + expected_version = f"{study_version:}" else: expected_version = f"{study_version.major}.{study_version.minor}" diff --git a/tests/test_model.py b/tests/test_model.py index 659a102..9add781 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -34,7 +34,7 @@ def test___str__(self) -> None: version = SolverVersion(4, 5, 6) result = version.__str__() assert isinstance(result, str) - assert result == "4.5.6" + assert result == "456" def test___int__(self) -> None: version = SolverVersion(4, 5, 6) @@ -51,14 +51,14 @@ def test_parse(self) -> None: @pytest.mark.parametrize( "format_spec, expected", [ - pytest.param("", "4.5.6", id="empty"), + pytest.param("d.d.d", "4.5.6", id="empty"), pytest.param("1d", "4", id="format-1d"), pytest.param("2d", "4.5", id="format-2d"), pytest.param("3d", "4.5.6", id="format-3d"), pytest.param("01d", "04", id="format-01d"), pytest.param("02d", "04.05", id="format-02d"), pytest.param("03d", "04.05.06", id="format-03d"), - pytest.param("ddd", "456", id="format-ddd"), + pytest.param("", "456", id="format-ddd"), pytest.param("X", "", marks=pytest.mark.xfail(raises=ValueError, strict=True)), ], ) @@ -93,7 +93,7 @@ def test___str__(self) -> None: version = StudyVersion(4, 5) result = version.__str__() assert isinstance(result, str) - assert result == "4.5" + assert result == "450" def test___int__(self) -> None: version = StudyVersion(4, 5) @@ -109,14 +109,14 @@ def test_parse(self) -> None: @pytest.mark.parametrize( "format_spec, expected", [ - pytest.param("", "4.5", id="empty"), + pytest.param("d.d.d", "4.5", id="empty"), pytest.param("1d", "4", id="format-1d"), pytest.param("2d", "4.5", id="format-2d"), pytest.param("3d", "4.5.0", id="format-3d"), pytest.param("01d", "04", id="format-01d"), pytest.param("02d", "04.05", id="format-02d"), pytest.param("03d", "04.05.00", id="format-03d"), - pytest.param("ddd", "450", id="format-ddd"), + pytest.param("", "450", id="format-ddd"), pytest.param("X", "", marks=pytest.mark.xfail(raises=ValueError, strict=True)), ], ) @@ -154,7 +154,7 @@ def test_ini_file(self): # We can write the study version to an INI file version = StudyVersion(10, 2) - config["antares"]["version"] = str(version) + config["antares"]["version"] = version.__format__("d.d.d") stream.seek(0) config.write(stream) @@ -252,7 +252,7 @@ def test_version_str(self): assert version == StudyVersion(8, 7) # We can convert a version number to a string - assert str(version) == "8.7" + assert str(version) == "870" # We can compare string versions using the standard comparison operators assert version == "8.7" @@ -272,7 +272,7 @@ def test_version_str(self): # We can construct a version number from a string with a single number version = StudyVersion.parse("8") assert version == StudyVersion(8) - assert str(version) == "8" + assert str(version) == "800" # noinspection PyDataclass,PyTypeChecker def test_version_triplet(self):