Skip to content

Commit

Permalink
Adjust messages that refer to "tool.versioningit.…"
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Nov 4, 2023
1 parent 3546537 commit c2bef11
Show file tree
Hide file tree
Showing 28 changed files with 137 additions and 157 deletions.
48 changes: 22 additions & 26 deletions src/versioningit/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,23 @@ def basic_tag2version(*, tag: str, params: dict[str, Any]) -> str:
"""Implements the ``"basic"`` ``tag2version`` method"""
params = params.copy()
try:
rmprefix = str_guard(
params.pop("rmprefix"), "tool.versioningit.tag2version.rmprefix"
)
rmprefix = str_guard(params.pop("rmprefix"), "tag2version.rmprefix")
except KeyError:
pass
else:
tag = strip_prefix(tag, rmprefix)
try:
rmsuffix = str_guard(
params.pop("rmsuffix"), "tool.versioningit.tag2version.rmsuffix"
)
rmsuffix = str_guard(params.pop("rmsuffix"), "tag2version.rmsuffix")
except KeyError:
pass
else:
tag = strip_suffix(tag, rmsuffix)
require_match = bool_guard(
params.pop("require-match", False),
"tool.versioningit.tag2version.require-match",
"tag2version.require-match",
)
try:
regex = str_guard(params.pop("regex"), "tool.versioningit.tag2version.regex")
regex = str_guard(params.pop("regex"), "tag2version.regex")
except KeyError:
pass
else:
Expand All @@ -67,12 +63,12 @@ def basic_tag2version(*, tag: str, params: dict[str, Any]) -> str:
tag = m[0]
if tag is None:
raise InvalidTagError(
"'version' group in tool.versioningit.tag2version.regex did"
"'version' group in versioningit's tag2version.regex did"
" not participate in match"
)
warn_extra_fields(
params,
"tool.versioningit.tag2version",
"tag2version",
["rmprefix", "rmsuffix", "regex", "require-match"],
)
return tag.lstrip("v")
Expand Down Expand Up @@ -104,10 +100,10 @@ def basic_format(
except KeyError:
raise ConfigError(
f"No format string for {description.state!r} state found in"
" tool.versioningit.format"
" versioningit's format table"
)
if not isinstance(fmt, str):
raise ConfigError("tool.versioningit.format.* values must be strings")
raise ConfigError("versioningit: format.* values must be strings")
return fmt.format_map(fields)


Expand All @@ -119,26 +115,22 @@ def basic_write(
) -> None:
"""Implements the ``"basic"`` ``write`` method"""
params = params.copy()
filename = str_guard(params.pop("file", None), "tool.versioningit.write.file")
filename = str_guard(params.pop("file", None), "write.file")
path = Path(project_dir, filename)
encoding = str_guard(
params.pop("encoding", "utf-8"), "tool.versioningit.write.encoding"
)
encoding = str_guard(params.pop("encoding", "utf-8"), "write.encoding")
try:
template = str_guard(params.pop("template"), "tool.versioningit.write.template")
template = str_guard(params.pop("template"), "write.template")
except KeyError:
if path.suffix == ".py":
template = '__version__ = "{version}"'
elif path.suffix == ".txt" or path.suffix == "":
template = "{version}"
else:
raise ConfigError(
"tool.versioningit.write.template not specified and file has"
"versioningit: write.template not specified and file has"
f" unknown suffix {path.suffix!r}"
)
warn_extra_fields(
params, "tool.versioningit.write", ["file", "encoding", "template"]
)
warn_extra_fields(params, "write", ["file", "encoding", "template"])
log.debug("Ensuring parent directories of %s exist", path)
path.parent.mkdir(parents=True, exist_ok=True)
log.info("Writing version to file %s", path)
Expand All @@ -156,9 +148,9 @@ def basic_template_fields(
"""Implements the ``"basic"`` ``template-fields`` method"""
params = deepcopy(params)
vtuple_params = params.pop("version-tuple", {})
SUBTABLE = "tool.versioningit.template-fields.version-tuple"
SUBTABLE = "template-fields.version-tuple"
if not isinstance(vtuple_params, dict):
raise ConfigError(f"{SUBTABLE} must be a table")
raise ConfigError(f"versioningit: {SUBTABLE} must be a table")
pep440 = bool_guard(vtuple_params.pop("pep440", False), f"{SUBTABLE}.pep440")
epoch: Optional[bool]
try:
Expand All @@ -167,19 +159,23 @@ def basic_template_fields(
epoch = None
else:
if not pep440:
log.warning("%s.epoch is ignored when pep440 is false", SUBTABLE)
log.warning(
"versioningit's %s.epoch is ignored when pep440 is false", SUBTABLE
)
split_on = optional_str_guard(
vtuple_params.pop("split-on", None), f"{SUBTABLE}.split-on"
)
if pep440 and split_on is not None:
log.warning("%s.split-on is ignored when pep440 is true", SUBTABLE)
log.warning(
"versioningit's %s.split-on is ignored when pep440 is true", SUBTABLE
)
double_quote = bool_guard(
vtuple_params.pop("double-quote", True), f"{SUBTABLE}.double-quote"
)
warn_extra_fields(
vtuple_params, SUBTABLE, ["pep440", "epoch", "split-on", "double-quote"]
)
warn_extra_fields(params, "tool.versioningit.template-fields", ["version-tuple"])
warn_extra_fields(params, "template-fields", ["version-tuple"])
if pep440:
version_tuple = split_pep440_version(
version, epoch=epoch, double_quote=double_quote
Expand Down
25 changes: 11 additions & 14 deletions src/versioningit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ def parse_obj(cls, obj: Any) -> Config:
fields are not of the correct type
"""
if not isinstance(obj, dict):
raise ConfigError("tool.versioningit must be a table")
raise ConfigError("versioningit config must be a table")
default_version = optional_str_guard(
obj.pop("default-version", None), "tool.versioningit.default-version"
obj.pop("default-version", None), "default-version"
)
sections: dict[str, Optional[ConfigSection]] = {}
for f in fields(cls):
Expand All @@ -134,7 +134,7 @@ def parse_obj(cls, obj: Any) -> Config:
sections[f.name] = cls.parse_section(f, obj.pop(attr2key(f.name), None))
warn_extra_fields(
obj,
"tool.versioningit",
None,
[attr2key(f.name) for f in fields(cls)],
)
return cls(
Expand All @@ -144,9 +144,8 @@ def parse_obj(cls, obj: Any) -> Config:
@staticmethod
def parse_section(f: Field, obj: Any) -> Optional["ConfigSection"]:
"""
Parse a ``tool.versioniningit.STEP`` field according to the metadata in
the given `dataclasses.Field`, which must consist of the following
items:
Parse a step configuration field according to the metadata in the given
`dataclasses.Field`, which must consist of the following items:
``default_entry_point`` : string
The name of the default method to use for the step if one is not
Expand Down Expand Up @@ -180,7 +179,7 @@ def parse_section(f: Field, obj: Any) -> Optional["ConfigSection"]:
return ConfigSection(method_spec, obj)
else:
raise ConfigError(
f"tool.versioningit.{attr2key(f.name)} must be a string or table"
f"versioningit: {attr2key(f.name)} must be a string or table"
)

@staticmethod
Expand All @@ -205,30 +204,28 @@ def parse_method_spec(f: Field, method: Any) -> MethodSpec:
module = method.pop("module", None)
if not isinstance(module, str):
raise ConfigError(
f"tool.versioningit.{key}.method.module is required and"
f"versioningit: {key}.method.module is required and"
" must be a string"
)
value = method.pop("value", None)
if not isinstance(value, str):
raise ConfigError(
f"tool.versioningit.{key}.method.value is required and"
f"versioningit: {key}.method.value is required and"
" must be a string"
)
module_dir = method.pop("module-dir", None)
if module_dir is not None and not isinstance(module_dir, str):
raise ConfigError(
f"tool.versioningit.{key}.method.module-dir must be a string"
f"versioningit: {key}.method.module-dir must be a string"
)
warn_extra_fields(
method,
f"tool.versioningit.{key}.method",
f"{key}.method",
["module", "value", "module-dir"],
)
return CustomMethodSpec(module, value, module_dir)
else:
raise ConfigError(
f"tool.versioningit.{key}.method must be a string or table"
)
raise ConfigError(f"versioningit: {key}.method must be a string or table")


def attr2key(name: str) -> str:
Expand Down
51 changes: 27 additions & 24 deletions src/versioningit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class VCSDescription:
#: The relationship of the repository's current state to the tag. If the
#: repository state is exactly the tagged state, this field should equal
#: ``"exact"``; otherwise, it will be a string that will be used as a key
#: in the ``[tool.versioningit.format]`` subtable. Recommended values are
#: ``"distance"``, ``"dirty"``, and ``"distance-dirty"``.
#: in the ``format`` subtable of the versioningit configuration.
#: Recommended values are ``"distance"``, ``"dirty"``, and
#: ``"distance-dirty"``.
state: str

#: The name of the repository's current branch, or `None` if it cannot be
Expand Down Expand Up @@ -197,8 +198,8 @@ def get_version(self, write: bool = False, fallback: bool = True) -> str:
"""
Determine the version for the project.
If ``write`` is true, then the file specified in the
``[tool.versioningit.write]`` subtable, if any, will be updated.
If ``write`` is true, then the file specified in the ``write`` subtable
of the versioningit configuration, if any, will be updated.
If ``fallback`` is true, then if ``project_dir`` is not under version
control (or if the VCS executable is not installed), ``versioningit``
Expand Down Expand Up @@ -231,8 +232,8 @@ def run(
optionally, "write" — and return an object containing the final version
and intermediate values.
If ``write`` is true, then the file specified in the
``[tool.versioningit.write]`` subtable, if any, will be updated.
If ``write`` is true, then the file specified in the ``write`` subtable
of the versioningit configuration, if any, will be updated.
If ``fallback`` is true, then if ``project_dir`` is not under version
control (or if the VCS executable is not installed), ``versioningit``
Expand Down Expand Up @@ -282,15 +283,15 @@ def run(
)
if self.default_version is not None:
log.error("%s: %s", type(e).__name__, str(e))
log.info("Falling back to tool.versioningit.default-version")
log.info("Falling back to default-version")
version = self.default_version
using_default_version = True
else:
raise
except Exception: # pragma: no cover
if self.default_version is not None:
log.exception("An unexpected error occurred:")
log.info("Falling back to tool.versioningit.default-version")
log.info("Falling back to default-version")
version = self.default_version
using_default_version = True
else:
Expand Down Expand Up @@ -458,14 +459,15 @@ def get_version(
Determine the version for the project at ``project_dir``.
If ``config`` is `None`, then ``project_dir`` must contain a
:file:`pyproject.toml` file containing a ``[tool.versioningit]`` table; if
it does not, a `NotVersioningitError` is raised. If ``config`` is not
`None`, then any :file:`pyproject.toml` file in ``project_dir`` will be
ignored, and the configuration will be taken from ``config`` instead; see
":ref:`config_dict`".
:file:`pyproject.toml` file containing either a ``[tool.versioningit]``
table or a ``[tool.hatch.version]`` table with the ``source`` key set to
``"versioningit"``; if it does not, a `NotVersioningitError` is raised. If
``config`` is not `None`, then any :file:`pyproject.toml` file in
``project_dir`` will be ignored, and the configuration will be taken from
``config`` instead. See ":ref:`config_dict`".
If ``write`` is true, then the file specified in the
``[tool.versioningit.write]`` subtable, if any, will be updated.
If ``write`` is true, then the file specified in the ``write`` subtable of
the versioningit configuration, if any, will be updated.
If ``fallback`` is true, then if ``project_dir`` is not under version
control (or if the VCS executable is not installed), ``versioningit`` will
Expand All @@ -481,8 +483,8 @@ def get_version(
:raises NotVersioningitError:
- if ``config`` is `None` and ``project_dir`` does not contain a
:file:`pyproject.toml` file
- if the :file:`pyproject.toml` file does not contain a
``[tool.versioningit]`` table
- if ``config`` is `None` and the :file:`pyproject.toml` file does not
contain a versioningit configuration table
:raises ConfigError:
if any of the values in ``config`` are not of the correct type
:raises MethodError: if a method returns a value of the wrong type
Expand All @@ -501,19 +503,20 @@ def get_next_version(
``project_dir``.
If ``config`` is `None`, then ``project_dir`` must contain a
:file:`pyproject.toml` file containing a ``[tool.versioningit]`` table; if
it does not, a `NotVersioningitError` is raised. If ``config`` is not
`None`, then any :file:`pyproject.toml` file in ``project_dir`` will be
ignored, and the configuration will be taken from ``config`` instead; see
":ref:`config_dict`".
:file:`pyproject.toml` file containing either a ``[tool.versioningit]``
table or a ``[tool.hatch.version]`` table with the ``source`` key set to
``"versioningit"``; if it does not, a `NotVersioningitError` is raised. If
``config`` is not `None`, then any :file:`pyproject.toml` file in
``project_dir`` will be ignored, and the configuration will be taken from
``config`` instead. See ":ref:`config_dict`".
:raises NotVCSError:
if ``project_dir`` is not under version control
:raises NotVersioningitError:
- if ``config`` is `None` and ``project_dir`` does not contain a
:file:`pyproject.toml` file
- if the :file:`pyproject.toml` file does not contain a
``[tool.versioningit]`` table
- if ``config`` is `None` and the :file:`pyproject.toml` file does not
contain a versioningit configuration table
:raises ConfigError:
if any of the values in ``config`` are not of the correct type
:raises MethodError: if a method returns a value of the wrong type
Expand Down
32 changes: 11 additions & 21 deletions src/versioningit/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,10 @@ def get_branch(self) -> Optional[str]:
def describe_git(*, project_dir: str | Path, params: dict[str, Any]) -> VCSDescription:
"""Implements the ``"git"`` ``vcs`` method"""
params = params.copy()
match = list_str_guard(params.pop("match", []), "tool.versioningit.vcs.match")
exclude = list_str_guard(params.pop("exclude", []), "tool.versioningit.vcs.exclude")
default_tag = optional_str_guard(
params.pop("default-tag", None), "tool.versioningit.vcs.default-tag"
)
warn_extra_fields(
params, "tool.versioningit.vcs", ["match", "exclude", "default-tag"]
)
match = list_str_guard(params.pop("match", []), "vcs.match")
exclude = list_str_guard(params.pop("exclude", []), "vcs.exclude")
default_tag = optional_str_guard(params.pop("default-tag", None), "vcs.default-tag")
warn_extra_fields(params, "vcs", ["match", "exclude", "default-tag"])
build_date = get_build_date()
repo = GitRepo(project_dir)
repo.ensure_is_repo()
Expand All @@ -244,15 +240,9 @@ def describe_git_archive(
) -> VCSDescription:
"""Implements the ``"git-archive"`` ``vcs`` method"""
params = params.copy()
default_tag = optional_str_guard(
params.pop("default-tag", None), "tool.versioningit.vcs.default-tag"
)
describe_subst = str_guard(
params.pop("describe-subst", None), "tool.versioningit.vcs.describe-subst"
)
warn_extra_fields(
params, "tool.versioningit.vcs", ["default-tag", "describe-subst"]
)
default_tag = optional_str_guard(params.pop("default-tag", None), "vcs.default-tag")
describe_subst = str_guard(params.pop("describe-subst", None), "vcs.describe-subst")
warn_extra_fields(params, "vcs", ["default-tag", "describe-subst"])
build_date = get_build_date()
repo = GitRepo(project_dir)
try:
Expand All @@ -262,15 +252,15 @@ def describe_git_archive(
pass
elif describe_subst == "":
raise NoTagError(
"tool.versioningit.vcs.describe-subst is empty in Git archive"
"versioningit's vcs.describe-subst is empty in Git archive"
)
elif describe_subst.startswith("$Format"):
raise NoTagError(
"tool.versioningit.vcs.describe-subst not expanded in Git archive"
"versioningit's vcs.describe-subst not expanded in Git archive"
)
elif describe_subst.startswith("%(describe"):
raise NoTagError(
"tool.versioningit.vcs.describe-subst format was invalid,"
"versioningit's vcs.describe-subst format was invalid,"
f" expanded to {describe_subst!r}"
)
else:
Expand Down Expand Up @@ -299,7 +289,7 @@ def describe_git_archive(
try:
opts = DescribeOpts.parse_describe_subst(describe_subst)
except ValueError as e:
raise ConfigError(f"Invalid tool.versioningit.vcs.describe-subst value: {e}")
raise ConfigError(f"versioningit: Invalid vcs.describe-subst value: {e}")
vdesc = describe_git_core(repo, build_date, default_tag, opts)
vdesc.fields.pop("revision", None)
vdesc.fields.pop("author_date", None)
Expand Down
Loading

0 comments on commit c2bef11

Please sign in to comment.