Automatically detect the true Python type of a string and cast it to the correct type.Based on https://github.com/cgreer/cgAutoCast/blob/master/cgAutoCast.py
+
+Only used by Legacy configuration file parser."""importcontextlib
diff --git a/_modules/bumpversion/bump.html b/_modules/bumpversion/bump.html
index baa90c63..11afe447 100644
--- a/_modules/bumpversion/bump.html
+++ b/_modules/bumpversion/bump.html
@@ -184,6 +184,13 @@
elifversion_part:logger.info("Attempting to increment part '%s'",version_part)logger.indent()
- next_version=current_version.bump(version_part,config.version_config.order)
+ next_version=current_version.bump(version_part)else:raiseConfigurationError("Unable to get the next version.")
- logger.info("Values are now: %s",key_val_string(next_version.values))
+ logger.info("Values are now: %s",key_val_string(next_version.components))logger.dedent()returnnext_version
@@ -364,7 +370,7 @@
Source code for bumpversion.bump
dry_run:bool=False,)->None:"""
- Commit and tag the changes, if a tool is configured.
+ Commit and tag the changes if a tool is configured. Args: config: The configuration
diff --git a/_modules/bumpversion/config.html b/_modules/bumpversion/config.html
index 32b46b9a..8a1fb9cb 100644
--- a/_modules/bumpversion/config.html
+++ b/_modules/bumpversion/config.html
@@ -184,6 +184,13 @@
-[docs]
-classVersionPartConfig(BaseModel):
-"""Configuration of a part of the version."""
-
- values:Optional[list]=None# Optional. Numeric is used if missing or no items in list
- optional_value:Optional[str]=None# Optional.
- # Defaults to first value. 0 in the case of numeric. Empty string means nothing is optional.
- first_value:Union[str,int,None]=None# Optional. Defaults to first value in values
- independent:bool=False
"""Return the version configuration."""frombumpversion.version_partimportVersionConfig
- returnVersionConfig(self.parse,self.serialize,self.search,self.replace,self.parts)
"""Module for managing Versions and their internal parts."""importre
-importstring
-fromcopyimportcopy
-fromtypingimportAny,Dict,List,MutableMapping,Optional,Tuple,Union
+fromtypingimportAny,Dict,List,MutableMapping,Optional,TuplefromclickimportUsageError
-frombumpversion.config.modelsimportVersionPartConfig
-frombumpversion.exceptionsimportFormattingError,InvalidVersionPartError,MissingValueError
-frombumpversion.functionsimportNumericFunction,PartFunction,ValuesFunctionfrombumpversion.uiimportget_indented_logger
-frombumpversion.utilsimportkey_val_string,labels_for_format
+frombumpversion.utilsimportlabels_for_format
+frombumpversion.versioning.modelsimportVersion,VersionComponentConfig,VersionSpec
+frombumpversion.versioning.serializationimportparse_version,serializelogger=get_indented_logger(__name__)
-
-[docs]
-classVersionPart:
-"""
- Represent part of a version number.
-
- Determines the PartFunction that rules how the part behaves when increased or reset
- based on the configuration given.
- """
-
- def__init__(self,config:VersionPartConfig,value:Union[str,int,None]=None):
- self._value=str(value)ifvalueisnotNoneelseNone
- self.config=config
- self.func:Optional[PartFunction]=None
- ifconfig.values:
- str_values=[str(v)forvinconfig.values]
- str_optional_value=str(config.optional_value)ifconfig.optional_valueisnotNoneelseNone
- str_first_value=str(config.first_value)ifconfig.first_valueisnotNoneelseNone
- self.func=ValuesFunction(str_values,str_optional_value,str_first_value)
- else:
- self.func=NumericFunction(config.optional_value,config.first_valueor"0")
-
- @property
- defvalue(self)->str:
-"""Return the value of the part."""
- returnself._valueorself.func.optional_value
-
-
-[docs]
- defcopy(self)->"VersionPart":
-"""Return a copy of the part."""
- returnVersionPart(self.config,self._value)
-
-
-
-[docs]
- defbump(self)->"VersionPart":
-"""Return a part with bumped value."""
- returnVersionPart(self.config,self.func.bump(self.value))
-
-
-
-[docs]
- defnull(self)->"VersionPart":
-"""Return a part with first value."""
- returnVersionPart(self.config,self.func.first_value)
-
-
- @property
- defis_optional(self)->bool:
-"""Is the part optional?"""
- returnself.value==self.func.optional_value
-
- @property
- defis_independent(self)->bool:
-"""Is the part independent of the other parts?"""
- returnself.config.independent
-
-
-[docs]
-classVersion:
-"""The specification of a version and its parts."""
-
- def__init__(self,values:Dict[str,VersionPart],original:Optional[str]=None):
- self.values=values
- self.original=original
-
-
self.serialize_formats=serializeself.part_configs=part_configsor{}
+ self.version_spec=VersionSpec(self.part_configs)# TODO: I think these two should be removed from the config objectself.search=searchself.replace=replace
@@ -480,136 +331,14 @@
Source code for bumpversion.version_part
Returns: A Version object representing the string. """
- ifnotversion_string:
- returnNone
-
- regexp_one_line="".join([line.split("#")[0].strip()forlineinself.parse_regex.pattern.splitlines()])
-
- logger.info(
- "Parsing version '%s' using regexp '%s'",
- version_string,
- regexp_one_line,
- )
- logger.indent()
+ parsed=parse_version(version_string,self.parse_regex.pattern)
- match=self.parse_regex.search(version_string)
-
- ifnotmatch:
- logger.warning(
- "Evaluating 'parse' option: '%s' does not parse current version '%s'",
- self.parse_regex.pattern,
- version_string,
- )
+ ifnotparsed:returnNone
- _parsed={
- key:VersionPart(self.part_configs[key],value)
- forkey,valueinmatch.groupdict().items()
- ifkeyinself.part_configs
- }
- v=Version(_parsed,version_string)
-
- logger.info("Parsed the following values: %s",key_val_string(v.values))
- logger.dedent()
-
- returnv
-
-
-
-[docs]
- def_serialize(
- self,version:Version,serialize_format:str,context:MutableMapping,raise_if_incomplete:bool=False
- )->str:
-"""
- Attempts to serialize a version with the given serialization format.
-
- Args:
- version: The version to serialize
- serialize_format: The serialization format to use, using Python's format string syntax
- context: The context to use when serializing the version
- raise_if_incomplete: Whether to raise an error if the version is incomplete
-
- Raises:
- FormattingError: if not serializable
- MissingValueError: if not all parts required in the format have values
-
- Returns:
- The serialized version as a string
- """
- values=copy(context)
- forkinversion:
- values[k]=version[k]
-
- # TODO dump complete context on debug level
-
- try:
- # test whether all parts required in the format have values
- serialized=serialize_format.format(**values)
-
- exceptKeyErrorase:
- missing_key=getattr(e,"message",e.args[0])
- raiseMissingValueError(
- f"Did not find key {missing_key!r} in {version!r} when serializing version number"
- )frome
-
- keys_needing_representation=set()
-
- keys=list(self.order)
- fori,kinenumerate(keys):
- v=values[k]
-
- ifnotisinstance(v,VersionPart):
- # values coming from environment variables don't need
- # representation
- continue
-
- ifnotv.is_optional:
- keys_needing_representation=set(keys[:i+1])
-
- required_by_format=set(labels_for_format(serialize_format))
-
- # try whether all parsed keys are represented
- ifraise_if_incompleteandnotkeys_needing_representation<=required_by_format:
- missing_keys=keys_needing_representation^required_by_format
- raiseFormattingError(
- f"""Could not represent '{"', '".join(missing_keys)}' in format '{serialize_format}'"""
- )
-
- returnserialized
-
-
-
-[docs]
- def_choose_serialize_format(self,version:Version,context:MutableMapping)->str:
- chosen=None
-
- logger.debug("Evaluating serialization formats")
- logger.indent()
- forserialize_formatinself.serialize_formats:
- try:
- self._serialize(version,serialize_format,context,raise_if_incomplete=True)
- # Prefer shorter or first search expression.
- chosen_part_count=len(list(string.Formatter().parse(chosen)))ifchosenelseNone
- serialize_part_count=len(list(string.Formatter().parse(serialize_format)))
- ifnotchosenorchosen_part_count>serialize_part_count:
- chosen=serialize_format
- logger.debug("Found '%s' to be a usable serialization format",chosen)
- else:
- logger.debug("Found '%s' usable serialization format, but it's longer",serialize_format)
- exceptFormattingError:
- # If chosen, prefer shorter
- ifnotchosen:
- chosen=serialize_format
- exceptMissingValueErrorase:
- logger.info(e.message)
- raisee
-
- ifnotchosen:
- raiseKeyError("Did not find suitable serialization format")
- logger.dedent()
- logger.debug("Selected serialization format '%s'",chosen)
-
- returnchosen
Returns: The serialized version as a string """
- logger.debug("Serializing version '%s'",version)
- logger.indent()
- serialized=self._serialize(version,self._choose_serialize_format(version,context),context)
- logger.debug("Serialized to '%s'",serialized)
- logger.dedent()
- returnserialized