[docs]classTomlVersionDeclaration(IVersionReplacer):def__init__(self,path:Path|str,search_text:str,stamp_format:VersionStampType)->None:self._content:str|None=Noneself._path=Path(path).resolve()self._stamp_format=stamp_formatself._search_text=search_text@propertydefcontent(self)->str:"""A cached property that stores the content of the configured source file."""ifself._contentisNone:log.debug("No content stored, reading from source file %s",self._path)ifnotself._path.exists():raiseFileNotFoundError(f"path {self._path!r} does not exist")self._content=self._path.read_text()returnself._content@content.deleterdefcontent(self)->None:self._content=None
[docs]@deprecated(version="9.20.0",reason="Function is unused and will be removed in a future release",)defparse(self)->set[Version]:# pragma: no cover"""Look for the version in the source content"""content=self._load()maybe_version:str=content.get(self._search_text)# type: ignore[return-value]ifmaybe_versionisnotNone:log.debug("Found a key %r that looks like a version (%r)",self._search_text,maybe_version,)valid_version=Version.parse(maybe_version)return{valid_version}ifvalid_versionelseset()# Maybe in future raise error if not found?returnset()
[docs]defreplace(self,new_version:Version)->str:""" Replace the version in the source content with `new_version`, and return the updated content. """content=self._load()ifself._search_textincontent:log.info("found %r in source file contents, replacing with %s",self._search_text,new_version,)content[self._search_text]=(new_version.as_tag()ifself._stamp_format==VersionStampType.TAG_FORMATelsestr(new_version))returntomlkit.dumps(cast(Dict[str,Any],content))
def_load(self)->Dotty:"""Load the content of the source file into a Dotty for easier searching"""returnDotty(tomlkit.loads(self.content))
[docs]defupdate_file_w_version(self,new_version:Version,noop:bool=False)->Path|None:ifnoop:ifnotself._path.exists():noop_report(f"FILE NOT FOUND: cannot stamp version in non-existent file {self._path!r}",)returnNoneifself._search_textnotinself._load():noop_report(f"VERSION PATTERN NOT FOUND: no version to stamp in file {self._path!r}",)returnNonereturnself._pathnew_content=self.replace(new_version)ifnew_content==self.content:returnNoneself._path.write_text(new_content)delself.contentreturnself._path
[docs]@classmethoddeffrom_string_definition(cls,replacement_def:str)->TomlVersionDeclaration:""" create an instance of self from a string representing one item of the "version_toml" list in the configuration """parts=replacement_def.split(":",maxsplit=2)iflen(parts)<=1:raiseValueError(f"Invalid TOML replacement definition {replacement_def!r}, missing ':'")iflen(parts)==2:# apply default version_type of "number_format" (ie. "1.2.3")parts=[*parts,VersionStampType.NUMBER_FORMAT.value]path,search_text,version_type=partstry:stamp_type=VersionStampType(version_type)exceptValueErroraserr:raiseValueError(str.join(" ",["Invalid stamp type, must be one of:",str.join(", ",[e.valueforeinVersionStampType]),],))fromerrreturncls(path,search_text,stamp_type)