Skip to content

Commit 22d7385

Browse files
committed
Switch from re.finditer to re.sub
Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
1 parent d77f09b commit 22d7385

2 files changed

Lines changed: 103 additions & 30 deletions

File tree

src/saltrewrite/salt/fix_docstrings.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,34 @@ def autofix_docstring(docstring, filename):
108108
)
109109

110110

111-
def _convert_version_names_to_numbers(docstring, filename):
111+
def _handle_convert_version_names_to_numbers_match(match):
112112
"""
113-
Convert Salt version names to their version numbers counterpart
113+
Convert every match with a properly formatted version.
114114
"""
115-
for match in CONVERT_VERSION_NAMES_TO_NUMBERS_RE.finditer(docstring):
116-
vtype = match.group("vtype")
117-
version = match.group("version")
118-
versions = [vs.strip() for vs in version.split(",")]
119-
parsed_versions = []
120-
for version in versions:
115+
vtype = match.group("vtype")
116+
version = match.group("version")
117+
versions = [vs.strip() for vs in version.split(",")]
118+
parsed_versions = []
119+
for version in versions:
120+
try:
121+
version = SaltStackVersion.from_name(version).string
122+
except ValueError:
121123
try:
122-
version = SaltStackVersion.from_name(version).string
124+
version = SaltStackVersion.parse(version).string
123125
except ValueError:
124-
try:
125-
version = SaltStackVersion.parse(version).string
126-
except ValueError:
127-
pass
128-
parsed_versions.append(version)
129-
replace_contents = ".. {}:: {}".format(vtype, ",".join(parsed_versions))
130-
docstring = docstring.replace(match.group(0), replace_contents.rstrip())
131-
return docstring
126+
pass
127+
parsed_versions.append(version)
128+
replace_contents = ".. {}:: {}".format(vtype, ",".join(parsed_versions))
129+
return replace_contents
130+
131+
132+
def _convert_version_names_to_numbers(docstring, filename):
133+
"""
134+
Convert Salt version names to their version numbers counterpart
135+
"""
136+
return CONVERT_VERSION_NAMES_TO_NUMBERS_RE.sub(
137+
_handle_convert_version_names_to_numbers_match, docstring
138+
)
132139

133140

134141
CLI_EXAMPLE_CASE_AND_SPACING_RE = re.compile(
@@ -157,21 +164,22 @@ def _fix_simple_cli_example_spacing_issues(docstring, filename):
157164
)
158165

159166

167+
def _handle_fix_directives_formatting_match(match):
168+
return (
169+
"\n{}.. {}:: {}".format(
170+
match.group("spc1") or "",
171+
match.group("directive"),
172+
match.group("remaining") or "",
173+
).rstrip()
174+
+ "\n"
175+
)
176+
177+
160178
def _fix_directives_formatting(docstring, filename):
161179
"""
162180
Fix directive definition spacing
163181
"""
164-
for match in DIRECTIVES_FORMATTING_RE.finditer(docstring):
165-
replacement = (
166-
"\n{}.. {}:: {}".format(
167-
match.group("spc1") or "",
168-
match.group("directive"),
169-
match.group("remaining") or "",
170-
).rstrip()
171-
+ "\n"
172-
)
173-
docstring = docstring.replace(match.group(0), replacement)
174-
return docstring
182+
return DIRECTIVES_FORMATTING_RE.sub(_handle_fix_directives_formatting_match, docstring)
175183

176184

177185
FIX_CODE_BLOCKS_RE = re.compile(

tests/salt/test_docstrings.py

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,14 @@ def test_fix_versionadded_module_docstring_dot_zero_suffix(tempfiles, vtype):
243243
'''
244244
New module blah
245245
246-
..{}:3006
246+
..{0}:3006
247247
'''
248248
249249
def one():
250250
'''
251251
One function
252+
253+
..{0}::3006.0
252254
'''
253255
print("one")
254256
""".format(
@@ -261,12 +263,75 @@ def one():
261263
'''
262264
New module blah
263265
264-
.. {}:: 3006.0
266+
.. {0}:: 3006.0
265267
'''
266268
267269
def one():
268270
'''
269271
One function
272+
273+
.. {0}:: 3006.0
274+
'''
275+
print("one")
276+
""".format(
277+
vtype
278+
)
279+
)
280+
fpath = tempfiles.makepyfile(code, prefix="test_")
281+
fix_docstrings.rewrite(fpath)
282+
with open(fpath) as rfh:
283+
new_code = rfh.read()
284+
assert new_code == expected_code
285+
286+
287+
@pytest.mark.parametrize("vtype", ["versionadded", "versionchanged", "deprecated"])
288+
def test_fix_version_repeated_in_same_docstring(tempfiles, vtype):
289+
code = textwrap.dedent(
290+
"""
291+
# -*- coding: utf-8 -*-
292+
'''
293+
New module blah
294+
295+
..{0}:3006
296+
'''
297+
298+
def one(two, three):
299+
'''
300+
One function
301+
302+
..{0}::3006.0
303+
304+
Args:
305+
two: Two
306+
.. {0}::3006
307+
three: Three
308+
.. {0}:: 3006
309+
'''
310+
print("one")
311+
""".format(
312+
vtype
313+
)
314+
)
315+
expected_code = textwrap.dedent(
316+
"""
317+
# -*- coding: utf-8 -*-
318+
'''
319+
New module blah
320+
321+
.. {0}:: 3006.0
322+
'''
323+
324+
def one(two, three):
325+
'''
326+
One function
327+
328+
.. {0}:: 3006.0
329+
330+
Args:
331+
two: Two
332+
.. {0}:: 3006.0
333+
three: Three
334+
.. {0}:: 3006.0
270335
'''
271336
print("one")
272337
""".format(

0 commit comments

Comments
 (0)