Skip to content

Commit e9138be

Browse files
committed
Qt_convert_enum.py can now be used as a qc check
With --check added to the command, the return code will be updated with the number of enums that require(d) fixing.
1 parent 610ed38 commit e9138be

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

CAVEATS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,7 @@ enums that need replaced in each file.
427427

428428
To actually update the code add `--write` flag. This updates existing files and
429429
does not make backups of the existing files, so make sure to do that first.
430+
431+
To check for enum use regression you can add `--check`. This will change the return
432+
code to the number of enums that require changing. A return code of zero indicates
433+
that no enum changes are required.

Qt_convert_enum.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def enums_for_qt_py(self):
9898
module = getattr(self.Qt, module_name)
9999
self.enums_for_module(module)
100100

101-
def convert_enums_in_file(self, filepath: Path, root: Path, dry_run: bool) -> None:
101+
def convert_enums_in_file(self, filepath: Path, root: Path, dry_run: bool) -> int:
102102
"""Convert the enums in the given file.
103103
104104
Based on https://stackoverflow.com/a/72658216 by Kristof Mulier
@@ -111,6 +111,7 @@ def convert_enums_in_file(self, filepath: Path, root: Path, dry_run: bool) -> No
111111
with filepath.open('r', encoding='utf-8', newline='\n', errors='replace') as f:
112112
content = f.read()
113113

114+
changes = 0
114115
# Loop over all the keys in 'self.enum_map'. Perform a replacement in the
115116
# 'content' for each of them.
116117
for k, v in self.enum_map.items():
@@ -137,17 +138,20 @@ def convert_enums_in_file(self, filepath: Path, root: Path, dry_run: bool) -> No
137138
q = "'"
138139
print(f'{q}{relative_path}{q}: Replace {q}{k}{q} => {q}{v}{q} ({n})')
139140
content = new_content
141+
# Record the number of enums updated in this file
142+
changes += n
140143

141144
if dry_run:
142-
return
145+
return changes
143146

144147
with filepath.open('w', encoding='utf-8', newline='\n', errors='replace') as f:
145148
f.write(content)
146-
return
149+
return changes
147150

148-
def convert_all(self, directory: Path, dry_run: bool) -> None:
151+
def convert_all(self, directory: Path, dry_run: bool) -> int:
149152
"""Search and replace all enums."""
150153
ignored = [directory / i for i in self.ignored]
154+
changes = 0
151155
# Using os.walk instead of pathlib's walk to support older python's
152156
for _root, _, files in os.walk(directory):
153157
root = Path(_root)
@@ -170,8 +174,9 @@ def convert_all(self, directory: Path, dry_run: bool) -> None:
170174
continue
171175
if self.verbosity >= 2:
172176
print(f"Checking: {filepath}")
173-
self.convert_enums_in_file(filepath, directory, dry_run)
174-
continue
177+
changes += self.convert_enums_in_file(filepath, directory, dry_run)
178+
179+
return changes
175180

176181

177182
class DuplicateEnums:
@@ -251,6 +256,13 @@ def parse_args():
251256
default=0,
252257
help="Increase the verbosity of the output.",
253258
)
259+
parser.add_argument(
260+
'--check',
261+
action='store_true',
262+
help="The Return Code becomes the number of enums that were/require "
263+
"changing. Can be used with --write. Use this to check for enum use "
264+
"regression.",
265+
)
254266
parser.add_argument(
255267
'target', type=Path, nargs="?", help="Directory to process recursively"
256268
)
@@ -291,4 +303,13 @@ def add_binding_info(data, qt):
291303
print(json.dumps(mappings, indent=4, sort_keys=True))
292304
else:
293305
# Search .py files and update to fully qualified enum names
294-
mapper.convert_all(args.target, dry_run=not args.write)
306+
changes = mapper.convert_all(args.target, dry_run=not args.write)
307+
308+
# Report the number of enum changes
309+
if args.write:
310+
print(f"{changes} enums changed.")
311+
else:
312+
print(f"{changes} enums require changes.")
313+
# Set the return code to the number of enums being changed if enabled
314+
if args.check:
315+
sys.exit(changes)

tests.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,11 +1648,34 @@ def test_convert_enum():
16481648

16491649
assert enum_check in output
16501650

1651+
# Check using the "--check" command outputs the same text but uses
1652+
# the return code for the number of enums being changed.
1653+
try:
1654+
output = subprocess.check_output(
1655+
cmd + ["--check"], cwd=self.tempdir, universal_newlines=True
1656+
)
1657+
except subprocess.CalledProcessError as error:
1658+
assert error.returncode == 6
1659+
assert error.stdout == output
1660+
# The number of changes are added to the end of the output
1661+
assert "6 enums require changes." in output
1662+
16511663
# Test actually updating the files.
16521664
cmd.append("--write")
16531665
output = subprocess.check_output(cmd, cwd=self.tempdir, universal_newlines=True)
16541666
assert enum_check in output
16551667

1668+
# "--check" is respected in --write mode
1669+
try:
1670+
output = subprocess.check_output(
1671+
cmd + ["--check"], cwd=self.tempdir, universal_newlines=True
1672+
)
1673+
except subprocess.CalledProcessError as error:
1674+
assert error.returncode == 6
1675+
assert error.stdout == output
1676+
# The number of changes are added to the end of the output
1677+
assert "6 enums changed." in output
1678+
16561679
check = enum_file_1.replace("WindowActive", "WindowState.WindowActive")
16571680
check = check.replace("Box", "Shape.Box")
16581681
with open(init_file) as fle:

0 commit comments

Comments
 (0)