Skip to content

Commit b4f7853

Browse files
authored
Changes to use docformatter (#1271)
1 parent 1782eeb commit b4f7853

12 files changed

Lines changed: 226 additions & 180 deletions

File tree

data/templates/check_dependencies-with_url.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
import sys
55

66
# Change PYTHONPATH to include dependencies.
7-
sys.path.insert(0, '.')
7+
sys.path.insert(0, ".")
88

99
import utils.dependencies # pylint: disable=wrong-import-position
1010

11+
if __name__ == "__main__":
12+
dependency_helper = utils.dependencies.DependencyHelper()
1113

12-
if __name__ == '__main__':
13-
dependency_helper = utils.dependencies.DependencyHelper()
14+
if not dependency_helper.CheckDependencies():
15+
build_instructions_url = (
16+
"https://${project_name}.readthedocs.io/en/latest/sources/user/Users-Guide.html"
17+
)
18+
print(f"See: {build_instructions_url:s} on how to set up ${project_name}.")
19+
print("")
1420

15-
if not dependency_helper.CheckDependencies():
16-
build_instructions_url = (
17-
'https://${project_name}.readthedocs.io/en/latest/sources/user/Users-Guide.html')
18-
19-
print(f'See: {build_instructions_url:s} on how to set up ${project_name}.')
20-
print('')
21-
22-
sys.exit(1)
21+
sys.exit(1)

data/templates/dependencies.py

Lines changed: 155 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
"""Functionality to check for the availability and version of dependencies.
22
3-
This file is generated by l2tdevtools update-dependencies.py, any dependency
4-
related changes should be made in dependencies.ini.
3+
This file is generated by l2tdevtools update-dependencies.py, any dependency related
4+
changes should be made in dependencies.ini.
55
"""
66

77
import re
88

9-
109
# Dictionary that contains version tuples per module name.
1110
#
1211
# A version tuple consists of:
@@ -15,158 +14,174 @@
1514
# Where version_attribute_name is either a name of an attribute,
1615
# property or method.
1716
PYTHON_DEPENDENCIES = {
18-
${python_dependencies}}
17+
${python_dependencies}
18+
}
1919

20-
_VERSION_SPLIT_REGEX = re.compile(r'\.|\-')
20+
_VERSION_SPLIT_REGEX = re.compile(r"\.|\-")
2121

2222

2323
def _CheckPythonModule(
24-
module_name, version_attribute_name, minimum_version,
25-
is_required=True, maximum_version=None, verbose_output=True):
26-
"""Checks the availability of a Python module.
27-
28-
Args:
29-
module_name (str): name of the module.
30-
version_attribute_name (str): name of the attribute that contains
31-
the module version or method to retrieve the module version.
32-
minimum_version (str): minimum required version.
33-
is_required (Optional[bool]): True if the Python module is a required
34-
dependency.
35-
maximum_version (Optional[str]): maximum required version. Should only be
36-
used if there is a later version that is not supported.
37-
verbose_output (Optional[bool]): True if output should be verbose.
38-
39-
Returns:
40-
bool: True if the Python module is available and conforms to
41-
the minimum required version, False otherwise.
42-
"""
43-
module_object = _ImportPythonModule(module_name)
44-
if not module_object:
45-
if not is_required:
46-
print(f'[OPTIONAL]\tmissing: {module_name:s}.')
47-
return True
48-
49-
print(f'[FAILURE]\tmissing: {module_name:s}.')
50-
return False
51-
52-
if not version_attribute_name or not minimum_version:
53-
if verbose_output:
54-
print(f'[OK]\t\t{module_name:s}')
55-
return True
56-
57-
module_version = None
58-
if not version_attribute_name.endswith('()'):
59-
module_version = getattr(module_object, version_attribute_name, None)
60-
else:
61-
version_method = getattr(module_object, version_attribute_name[:-2], None)
62-
if version_method:
63-
module_version = version_method()
64-
65-
if not module_version:
66-
if not is_required:
67-
print((
68-
f'[OPTIONAL]\tunable to determine version information '
69-
f'for: {module_name:s}'))
70-
return True
71-
72-
print((
73-
f'[FAILURE]\tunable to determine version information '
74-
f'for: {module_name:s}'))
75-
return False
76-
77-
# Make sure the module version is a string.
78-
module_version = f'{module_version!s}'
79-
80-
# Remove a version suffix, such as: 0.7.0~rc1
81-
module_version_list = _VERSION_SPLIT_REGEX.split(module_version)
82-
83-
try:
84-
int(module_version_list[-1], 10)
85-
except (TypeError, ValueError):
86-
module_version_list.pop()
87-
88-
# Split the version string and convert every digit into an integer.
89-
# A string compare of both version strings will yield an incorrect result.
90-
module_version_map = list(map(int, module_version_list))
91-
minimum_version_map = list(
92-
map(int, _VERSION_SPLIT_REGEX.split(minimum_version)))
93-
94-
if module_version_map < minimum_version_map:
95-
if not is_required:
96-
print((
97-
f'[OPTIONAL]\t{module_name:s} version: {module_version!s} is too '
98-
f'old, {minimum_version!s} or later required.'))
99-
return True
100-
101-
print((
102-
f'[FAILURE]\t{module_name:s} version: {module_version!s} is too old, '
103-
f'{minimum_version!s} or later required.'))
104-
return False
105-
106-
if maximum_version:
107-
maximum_version_map = list(
108-
map(int, _VERSION_SPLIT_REGEX.split(maximum_version)))
109-
if module_version_map > maximum_version_map:
110-
if not is_required:
111-
print((
112-
f'[OPTIONAL]\t{module_name:s} version: {module_version!s} is too '
113-
f'recent, {minimum_version!s} or earlier required.'))
24+
module_name,
25+
version_attribute_name,
26+
minimum_version,
27+
is_required=True,
28+
maximum_version=None,
29+
verbose_output=True,
30+
):
31+
"""Checks the availability of a Python module.
32+
33+
Args:
34+
module_name (str): name of the module.
35+
version_attribute_name (str): name of the attribute that contains
36+
the module version or method to retrieve the module version.
37+
minimum_version (str): minimum required version.
38+
is_required (Optional[bool]): True if the Python module is a required
39+
dependency.
40+
maximum_version (Optional[str]): maximum required version. Should only be
41+
used if there is a later version that is not supported.
42+
verbose_output (Optional[bool]): True if output should be verbose.
43+
44+
Returns:
45+
bool: True if the Python module is available and conforms to
46+
the minimum required version, False otherwise.
47+
"""
48+
module_object = _ImportPythonModule(module_name)
49+
if not module_object:
50+
if not is_required:
51+
print(f"[OPTIONAL]\tmissing: {module_name:s}.")
52+
return True
53+
54+
print(f"[FAILURE]\tmissing: {module_name:s}.")
55+
return False
56+
57+
if not version_attribute_name or not minimum_version:
58+
if verbose_output:
59+
print(f"[OK]\t\t{module_name:s}")
11460
return True
11561

116-
print((
117-
f'[FAILURE]\t{module_name:s} version: {module_version!s} is too '
118-
f'recent, {maximum_version!s} or earlier required.'))
119-
return False
62+
module_version = None
63+
if not version_attribute_name.endswith("()"):
64+
module_version = getattr(module_object, version_attribute_name, None)
65+
else:
66+
version_method = getattr(module_object, version_attribute_name[:-2], None)
67+
if version_method:
68+
module_version = version_method()
69+
70+
if not module_version:
71+
if not is_required:
72+
print(
73+
f"[OPTIONAL]\tunable to determine version information "
74+
f"for: {module_name:s}"
75+
)
76+
return True
77+
78+
print(
79+
f"[FAILURE]\tunable to determine version information "
80+
f"for: {module_name:s}"
81+
)
82+
return False
83+
84+
# Make sure the module version is a string.
85+
module_version = f"{module_version!s}"
86+
87+
# Remove a version suffix, such as: 0.7.0~rc1
88+
module_version_list = _VERSION_SPLIT_REGEX.split(module_version)
89+
90+
try:
91+
int(module_version_list[-1], 10)
92+
except (TypeError, ValueError):
93+
module_version_list.pop()
94+
95+
# Split the version string and convert every digit into an integer.
96+
# A string compare of both version strings will yield an incorrect result.
97+
module_version_map = list(map(int, module_version_list))
98+
minimum_version_map = list(map(int, _VERSION_SPLIT_REGEX.split(minimum_version)))
99+
100+
if module_version_map < minimum_version_map:
101+
if not is_required:
102+
print(
103+
f"[OPTIONAL]\t{module_name:s} version: {module_version!s} is too "
104+
f"old, {minimum_version!s} or later required."
105+
)
106+
return True
107+
108+
print(
109+
f"[FAILURE]\t{module_name:s} version: {module_version!s} is too old, "
110+
f"{minimum_version!s} or later required."
111+
)
112+
return False
113+
114+
if maximum_version:
115+
maximum_version_map = list(
116+
map(int, _VERSION_SPLIT_REGEX.split(maximum_version))
117+
)
118+
if module_version_map > maximum_version_map:
119+
if not is_required:
120+
print(
121+
f"[OPTIONAL]\t{module_name:s} version: {module_version!s} is too "
122+
f"recent, {minimum_version!s} or earlier required."
123+
)
124+
return True
125+
126+
print(
127+
f"[FAILURE]\t{module_name:s} version: {module_version!s} is too "
128+
f"recent, {maximum_version!s} or earlier required."
129+
)
130+
return False
120131

121-
if verbose_output:
122-
print(f'[OK]\t\t{module_name:s} version: {module_version!s}')
132+
if verbose_output:
133+
print(f"[OK]\t\t{module_name:s} version: {module_version!s}")
123134

124-
return True
135+
return True
125136

126137

127138
def _ImportPythonModule(module_name):
128-
"""Imports a Python module.
139+
"""Imports a Python module.
129140
130-
Args:
131-
module_name (str): name of the module.
141+
Args:
142+
module_name (str): name of the module.
132143
133-
Returns:
134-
module: Python module or None if the module cannot be imported.
135-
"""
136-
try:
137-
module_object = list(map(__import__, [module_name]))[0]
138-
except ImportError:
139-
return None
144+
Returns:
145+
module: Python module or None if the module cannot be imported.
146+
"""
147+
try:
148+
module_object = list(map(__import__, [module_name]))[0]
149+
except ImportError:
150+
return None
140151

141-
# If the module name contains dots get the upper most module object.
142-
if '.' in module_name:
143-
for submodule_name in module_name.split('.')[1:]:
144-
module_object = getattr(module_object, submodule_name, None)
152+
# If the module name contains dots get the upper most module object.
153+
if "." in module_name:
154+
for submodule_name in module_name.split(".")[1:]:
155+
module_object = getattr(module_object, submodule_name, None)
145156

146-
return module_object
157+
return module_object
147158

148159

149160
def CheckDependencies(verbose_output=True):
150-
"""Checks the availability of the dependencies.
151-
152-
Args:
153-
verbose_output (Optional[bool]): True if output should be verbose.
154-
155-
Returns:
156-
bool: True if the dependencies are available, False otherwise.
157-
"""
158-
print('Checking availability and versions of dependencies.')
159-
check_result = True
160-
161-
for module_name, version_tuple in sorted(PYTHON_DEPENDENCIES.items()):
162-
if not _CheckPythonModule(
163-
module_name, version_tuple[0], version_tuple[1],
164-
is_required=version_tuple[3], maximum_version=version_tuple[2],
165-
verbose_output=verbose_output):
166-
check_result = False
167-
168-
if check_result and not verbose_output:
169-
print('[OK]')
170-
171-
print('')
172-
return check_result
161+
"""Checks the availability of the dependencies.
162+
163+
Args:
164+
verbose_output (Optional[bool]): True if output should be verbose.
165+
166+
Returns:
167+
bool: True if the dependencies are available, False otherwise.
168+
"""
169+
print("Checking availability and versions of dependencies.")
170+
check_result = True
171+
172+
for module_name, version_tuple in sorted(PYTHON_DEPENDENCIES.items()):
173+
if not _CheckPythonModule(
174+
module_name,
175+
version_tuple[0],
176+
version_tuple[1],
177+
is_required=version_tuple[3],
178+
maximum_version=version_tuple[2],
179+
verbose_output=verbose_output,
180+
):
181+
check_result = False
182+
183+
if check_result and not verbose_output:
184+
print("[OK]")
185+
186+
print("")
187+
return check_result
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
[tool.docformatter]
3+
black = true
4+
non-cap = ${docformatter_non_cap}
5+
non-strict = true
File renamed without changes.

data/templates/tox.ini/testenv_black

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ deps =
1313
setuptools >= 65
1414
commands =
1515
black --version
16-
black --check .
16+
black .
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
[testenv:docformatter]
3+
skipsdist = True
4+
pip_pre = True
5+
passenv =
6+
CFLAGS
7+
CPPFLAGS
8+
LDFLAGS
9+
setenv =
10+
PYTHONPATH = {toxinidir}
11+
deps =
12+
docformatter
13+
setuptools >= 65
14+
commands =
15+
docformatter --version
16+
docformatter --in-place --recursive ${paths_to_lint_python}

0 commit comments

Comments
 (0)