Skip to content

Commit 8034bd0

Browse files
committed
code style improvements
1 parent 55c2193 commit 8034bd0

5 files changed

Lines changed: 203 additions & 140 deletions

File tree

test/examples.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ def _remove_version_suffixes(path: pathlib.Path) -> t.Optional[pathlib.Path]:
148148

149149

150150
def case_to_version_tuple(args, kwargs):
151+
"""Convert parameters of a given test case to a version tuple.
152+
153+
To be converted are args and kwargs meant for Version.__init__().
154+
"""
151155
return args + tuple(
152156
v for _, v in sorted(kwargs.items(), key=lambda _: KWARG_NAMES.index(_[0])))
153157

version_query/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Tool for querying current versions of Python packages."""
1+
"""Entry point of the command-line interface of version_query package."""
22

33
from .main import main
44

version_query/git_query.py

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ def _git_version_tags(repo: git.Repo) -> t.Mapping[git.Tag, Version]:
4040
return versions
4141

4242

43+
def _latest_git_version_tag_on_branches(
44+
repo: git.Repo, assume_if_none: bool, commit: git.Commit, commit_distance: int,
45+
skip_commits: t.Set[git.Commit]):
46+
_LOG.log(logging.NOTSET, 'entering %i branches...', len(commit.parents))
47+
results = []
48+
main_commit_distance = None
49+
for parent in commit.parents:
50+
try:
51+
result = _latest_git_version_tag(
52+
repo, assume_if_none, parent, commit_distance, skip_commits)
53+
if main_commit_distance is None:
54+
main_commit_distance = result[3]
55+
except ValueError:
56+
continue
57+
if result[2] is not None:
58+
results.append(result)
59+
if not results:
60+
return main_commit_distance
61+
final_result = sorted(results, key=lambda _: _[2])[-1]
62+
_LOG.log(logging.NOTSET, 'result from %i branches is %s and %s',
63+
len(commit.parents), *final_result[1:3])
64+
return final_result
65+
66+
4367
MAX_COMMIT_DISTANCE = 999
4468

4569

@@ -76,31 +100,35 @@ def _latest_git_version_tag(
76100
commit_distance += 1
77101
if len(commit.parents) <= 1:
78102
continue
79-
_LOG.log(logging.NOTSET, 'entering %i branches...', len(commit.parents))
80-
results = []
81-
main_commit_distance = None
82-
for parent in commit.parents:
83-
try:
84-
result = _latest_git_version_tag(
85-
repo, assume_if_none, parent, commit_distance, skip_commits)
86-
if main_commit_distance is None:
87-
main_commit_distance = result[3]
88-
except ValueError:
89-
continue
90-
if result[2] is not None:
91-
results.append(result)
92-
if not results:
93-
commit_distance = main_commit_distance
103+
result = _latest_git_version_tag_on_branches(
104+
repo, assume_if_none, commit, commit_distance, skip_commits)
105+
if not isinstance(result, tuple):
106+
commit_distance = result # main_commit_distance
94107
break
95-
result = sorted(results, key=lambda _: _[2])[-1]
96-
_LOG.log(logging.NOTSET, 'result from %i branches is %s and %s',
97-
len(commit.parents), *result[1:3])
108+
# _LOG.log(logging.NOTSET, 'entering %i branches...', len(commit.parents))
109+
# results = []
110+
# main_commit_distance = None
111+
# for parent in commit.parents:
112+
# try:
113+
# result = _latest_git_version_tag(
114+
# repo, assume_if_none, parent, commit_distance, skip_commits)
115+
# if main_commit_distance is None:
116+
# main_commit_distance = result[3]
117+
# except ValueError:
118+
# continue
119+
# if result[2] is not None:
120+
# results.append(result)
121+
# if not results:
122+
# commit_distance = main_commit_distance
123+
# break
124+
# result = sorted(results, key=lambda _: _[2])[-1]
125+
# _LOG.log(logging.NOTSET, 'result from %i branches is %s and %s',
126+
# len(commit.parents), *result[1:3])
98127
return result
99128
if not current_version_tags:
100129
if assume_if_none:
101130
return commit, None, Version.from_str('0.1.0.dev0'), commit_distance
102-
else:
103-
raise ValueError('the given repo {} has no version tags'.format(repo))
131+
raise ValueError('the given repo {} has no version tags'.format(repo))
104132
tag, version = sorted(current_version_tags.items(), key=lambda _: _[1])[-1]
105133
_LOG.log(logging.NOTSET, 'result is %s and %s', tag, version)
106134
return commit, tag, version, commit_distance

version_query/query.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,27 @@ def query_folder(path: pathlib.Path, search_parent_directories: bool = False) ->
3737

3838

3939
def query_caller(stack_level: int = 1) -> Version:
40+
"""Determine the version of code associated with the caller of this function."""
4041
here = _caller_folder(stack_level + 1)
4142
return query_folder(here, True)
4243

4344

4445
def query_version_str() -> str:
46+
"""Determine the version of code that calls this function and get it as string.
47+
48+
You can use this utility function like this:
49+
50+
'''
51+
from version_query import query_version_str
52+
53+
VERSION = query_version_str()
54+
'''
55+
"""
4556
return query_caller(2).to_str()
4657

4758

4859
def predict_folder(path: pathlib.Path, search_parent_directories: bool = True) -> Version:
60+
"""Predict version of code residing in a given folder."""
4961
priority_cutoff = 2
5062
paths = [path] + (list(path.parents)[:priority_cutoff] if search_parent_directories else [])
5163
for pth in paths:
@@ -71,4 +83,14 @@ def predict_caller(stack_level: int = 1) -> Version:
7183

7284

7385
def predict_version_str() -> str:
86+
"""Predict version of the code that calls this function and get it as string.
87+
88+
You can use this utility function like this:
89+
90+
'''
91+
from version_query import predict_version_str
92+
93+
VERSION = predict_version_str()
94+
'''
95+
"""
7496
return predict_caller(2).to_str()

0 commit comments

Comments
 (0)