Source code for semantic_release.version.declaration
from__future__importannotations# TODO: Remove v10fromabcimportABC,abstractmethodfromloggingimportgetLoggerfrompathlibimportPathfromtypingimportTYPE_CHECKINGfromdeprecated.sphinximportdeprecatedfromsemantic_release.version.declarations.enumimportVersionStampTypefromsemantic_release.version.declarations.i_version_replacerimportIVersionReplacerfromsemantic_release.version.declarations.patternimportPatternVersionDeclarationfromsemantic_release.version.declarations.tomlimportTomlVersionDeclarationifTYPE_CHECKING:# pragma: no coverfromsemantic_release.version.versionimportVersion# Globals__all__=["IVersionReplacer","VersionStampType","PatternVersionDeclaration","TomlVersionDeclaration","VersionDeclarationABC",]log=getLogger(__name__)
[docs]@deprecated(version="9.20.0",reason=str.join(" ",["Refactored to composition paradigm using the new IVersionReplacer interface.","This class will be removed in a future release",],),)classVersionDeclarationABC(ABC):""" ABC for classes representing a location in which a version is declared somewhere within the source tree of the repository """def__init__(self,path:Path|str,search_text:str)->None:self.path=Path(path)ifnotself.path.exists():raiseFileNotFoundError(f"path {self.path.resolve()!r} does not exist")self.search_text=search_textself._content:str|None=None@propertydefcontent(self)->str:""" The content of the source file in which the version is stored. This property is cached in the instance variable _content """ifself._contentisNone:log.debug("No content stored, reading from source file %s",self.path.resolve())self._content=self.path.read_text()returnself._content@content.deleterdefcontent(self)->None:log.debug("resetting instance-stored source file contents")self._content=None
[docs]@abstractmethoddefparse(self)->set[Version]:""" Return a set of the versions which can be parsed from the file. Because a source can match in multiple places, this method returns a set of matches. Generally, there should only be one element in this set (i.e. even if the version is specified in multiple places, it should be the same version in each place), but enforcing that condition is not mandatory or expected. """
[docs]@abstractmethoddefreplace(self,new_version:Version)->str:""" Update the versions. This method reads the underlying file, replaces each occurrence of the matched pattern, then writes the updated file. :param new_version: The new version number as a `Version` instance """
[docs]defwrite(self,content:str)->None:r""" Write new content back to the source path. Use alongside .replace(): >>> class MyVD(VersionDeclarationABC): ... def parse(self): ... ... def replace(self, new_version: Version): ... ... def write(self, content: str): ... >>> new_version = Version.parse("1.2.3") >>> vd = MyVD("path", r"__version__ = (?P<version>\d+\d+\d+)") >>> vd.write(vd.replace(new_version)) """log.debug("writing content to %r",self.path.resolve())self.path.write_text(content)self._content=None