@@ -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
177182class 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 )
0 commit comments