1515# limitations under the License.
1616
1717
18- """ Script that uses cookiecutter to add a new example to the ITKSphinxExamples.
19- Usage: python CreateNewExample.py
18+ """Script that uses cookiecutter to add a new example to the ITKSphinxExamples.
19+ Usage: python CreateNewExample.py
2020"""
2121
2222import argparse
@@ -47,20 +47,22 @@ if sys.version_info[:2] <= (2, 7):
4747
4848def write_all_itk_headers (itk_source_dir ):
4949 output = StringIO ()
50- for root , dirs , files in os .walk (pjoin (itk_source_dir , ' Modules' )):
51- for f in fnmatch .filter (files , ' itk*.h' ):
52- output .write (f + u' \n ' )
50+ for root , dirs , files in os .walk (pjoin (itk_source_dir , " Modules" )):
51+ for f in fnmatch .filter (files , " itk*.h" ):
52+ output .write (f + " \n " )
5353 return output
5454
55+
5556def get_all_itk_headers (itk_source_dir ):
5657 output = []
57- for root , dirs , files in os .walk (pjoin (itk_source_dir , ' Modules' )):
58- for f in fnmatch .filter (files , ' itk*.h' ):
58+ for root , dirs , files in os .walk (pjoin (itk_source_dir , " Modules" )):
59+ for f in fnmatch .filter (files , " itk*.h" ):
5960 output .append (f )
6061 return output
6162
63+
6264def words (text ):
63- temp = re .findall (' itk[A-Z][a-zA-Z_0-9]+' , text )
65+ temp = re .findall (" itk[A-Z][a-zA-Z_0-9]+" , text )
6466 for i in range (len (temp )):
6567 var = temp [i ]
6668 temp [i ] = var [3 :]
@@ -84,42 +86,92 @@ def edits1(word, alphabet):
8486 uppercases = [a + b [0 ].upper () + b [1 :] for a , b in splits if b ]
8587 all_lowercase = [word [:].lower ()]
8688 one_uppercase = [
87- a [:].lower () + b [0 ].upper () + b [1 :].lower () for a , b in splits if b ]
89+ a [:].lower () + b [0 ].upper () + b [1 :].lower () for a , b in splits if b
90+ ]
8891
8992 return set (
90- deletes + transposes + replaces + inserts + lowercases + uppercases +
91- all_lowercase + one_uppercase )
93+ deletes
94+ + transposes
95+ + replaces
96+ + inserts
97+ + lowercases
98+ + uppercases
99+ + all_lowercase
100+ + one_uppercase
101+ )
92102
93103
94104def known_edits2 (word , NWORDS , alphabet ):
95- return set (e2 for e1 in edits1 (word , alphabet )
96- for e2 in edits1 (e1 , alphabet ) if e2 in NWORDS )
105+ return {
106+ e2
107+ for e1 in edits1 (word , alphabet )
108+ for e2 in edits1 (e1 , alphabet )
109+ if e2 in NWORDS
110+ }
97111
98112
99113def known (words , NWORDS ):
100- return set ( w for w in words if w in NWORDS )
114+ return { w for w in words if w in NWORDS }
101115
102116
103117def itkcorrect (word , NWORDS , alphabet ):
104118 candidates = (
105- known ([word ], NWORDS ) or
106- known (edits1 (word , alphabet ), NWORDS ) or
107- known_edits2 (word , NWORDS , alphabet ) or [word ])
119+ known ([word ], NWORDS )
120+ or known (edits1 (word , alphabet ), NWORDS )
121+ or known_edits2 (word , NWORDS , alphabet )
122+ or [word ]
123+ )
108124 return max (candidates , key = NWORDS .get )
109125
110126
111127def space_out_camel_case (s ):
112- return re .sub ('((?=[A-Z][a-z])|(?<=[a-z])(?=[A-Z]))' , ' ' , s ).strip ()
113-
114- def itk_abbreviations (word ,** kwargs ):
115- if word .upper () in ('ITK' ,'VTK' ,'RGB' ,'VTP' ,'FFT' ,'FEM' ,'TIFF' ,'GMM' ,'EM' ,
116- 'CV' ,'GPU' ,'BMP' ,'CSV' ,'DCMTK' ,'GDCM' ,'GE' ,'GIPL' ,'HDF5' ,
117- 'IPL' ,'JPEG' ,'JPEG2000' ,'LSM' ,'BYU' ,'MINC' ,'MRC' ,'NIFTI' ,
118- 'NRRD' ,'REC' ,'PNG' ,'RAW' ,'XML' ,'RTK' ,'DICOM' ,'2D' ,'3D' ,'IO' ):
128+ return re .sub ("((?=[A-Z][a-z])|(?<=[a-z])(?=[A-Z]))" , " " , s ).strip ()
129+
130+
131+ def itk_abbreviations (word , ** kwargs ):
132+ if word .upper () in (
133+ "ITK" ,
134+ "VTK" ,
135+ "RGB" ,
136+ "VTP" ,
137+ "FFT" ,
138+ "FEM" ,
139+ "TIFF" ,
140+ "GMM" ,
141+ "EM" ,
142+ "CV" ,
143+ "GPU" ,
144+ "BMP" ,
145+ "CSV" ,
146+ "DCMTK" ,
147+ "GDCM" ,
148+ "GE" ,
149+ "GIPL" ,
150+ "HDF5" ,
151+ "IPL" ,
152+ "JPEG" ,
153+ "JPEG2000" ,
154+ "LSM" ,
155+ "BYU" ,
156+ "MINC" ,
157+ "MRC" ,
158+ "NIFTI" ,
159+ "NRRD" ,
160+ "REC" ,
161+ "PNG" ,
162+ "RAW" ,
163+ "XML" ,
164+ "RTK" ,
165+ "DICOM" ,
166+ "2D" ,
167+ "3D" ,
168+ "IO" ,
169+ ):
119170 return word .upper ()
120171
172+
121173def get_group_and_module_from_class_name (itk_src , class_name ):
122- """ Get the ITK group and module a class belongs to.
174+ """Get the ITK group and module a class belongs to.
123175
124176 Parameters
125177 ----------
@@ -134,107 +186,118 @@ def get_group_and_module_from_class_name(itk_src, class_name):
134186 found, and the ITK group and module names found.
135187 """
136188
137- cmakefile = pjoin (itk_src , ' CMake' , ' UseITK.cmake' )
189+ cmakefile = pjoin (itk_src , " CMake" , " UseITK.cmake" )
138190 result = dict ()
139- result [' bool' ] = False
191+ result [" bool" ] = False
140192
141193 if class_name is None :
142194 return result
143195
144196 if not os .path .exists (cmakefile ):
145- print (' Error: wrong path' )
197+ print (" Error: wrong path" )
146198 else :
147- path = ''
199+ path = ""
148200
149- for root , dirs , files in os .walk (pjoin (itk_src , ' Modules' )):
201+ for root , dirs , files in os .walk (pjoin (itk_src , " Modules" )):
150202 for f in files :
151- if f == ' itk' + class_name + '.h' :
203+ if f == " itk" + class_name + ".h" :
152204 path = root
153205
154206 if len (path ) != 0 :
155207
156208 temp = path .split (os .path .sep )
157209 depth = len (temp )
158210
159- result [' Module' ] = temp [depth - 2 ]
160- result [' Group' ] = temp [depth - 3 ]
161- result [' bool' ] = True
211+ result [" Module" ] = temp [depth - 2 ]
212+ result [" Group" ] = temp [depth - 3 ]
213+ result [" bool" ] = True
162214
163215 else :
164- print (' Error: This class is not part of ITK: {}' . format ( class_name ) )
216+ print (f" Error: This class is not part of ITK: { class_name } " )
165217
166218 return result
167219
168220
169221def main ():
170222
171223 parser = argparse .ArgumentParser (
172- description = 'Script to add a new example to the ITKSphinxExamples.' )
224+ description = "Script to add a new example to the ITKSphinxExamples."
225+ )
173226 args = parser .parse_args ()
174227
175228 # Collect directories
176229
177230 # If no `ITK` directory is present in the ITKSphinxExamples build directory,
178231 # ITKSphinxExamples was not configured using the Superbuild functionality.
179- itk_examples_binary_dir = os .path .abspath (' @ITKSphinxExamples_BINARY_DIR@' )
232+ itk_examples_binary_dir = os .path .abspath (" @ITKSphinxExamples_BINARY_DIR@" )
180233 itk_examples_build_dir = os .path .abspath (pjoin (itk_examples_binary_dir , os .pardir ))
181- itk_superbuild_dir = pjoin (itk_examples_build_dir , ' ITK' )
234+ itk_superbuild_dir = pjoin (itk_examples_build_dir , " ITK" )
182235 if not os .path .isdir (itk_superbuild_dir ):
183236 # try to see if the ITK directory was setup manually and get the ITK directory from there:
184- itk_dir = os .path .abspath (' @ITK_CMAKE_DIR@' )
237+ itk_dir = os .path .abspath (" @ITK_CMAKE_DIR@" )
185238 if not os .path .isdir (itk_dir ):
186- print ('Error: ITKSphinxExamples must be configured using the Superbuild'
187- 'functionality to use this script or set ITK_DIR in cmake.' )
239+ print (
240+ "Error: ITKSphinxExamples must be configured using the Superbuild"
241+ "functionality to use this script or set ITK_DIR in cmake."
242+ )
188243 sys .exit ()
189244 itk_src = os .path .dirname (itk_dir )
190245 else :
191246 itk_src = itk_superbuild_dir
192247
193- itk_examples_root_dir = os .path .abspath (' @ITKSphinxExamples_SOURCE_DIR@' )
248+ itk_examples_root_dir = os .path .abspath (" @ITKSphinxExamples_SOURCE_DIR@" )
194249 itk_examples_src = pjoin (itk_examples_root_dir , "src" )
195250
196251 default_class_name = "MyClassName"
197252 default_example_name = "MyExample"
198253 default_synopsis = "MySynopsis"
199254
200255 # Prompt user for appropriate cookiecutter template variable values
201- class_name = get_input ("class_name [" + default_class_name + "]: " )\
202- or default_class_name
203- example_name = get_input ("example_name [" + default_example_name + "]: " )\
204- or default_example_name
205- example_title = titlecase (space_out_camel_case (example_name ), callback = itk_abbreviations )
206- example_synopsis = get_input ("example_synopsis [" + default_synopsis + "]: " )\
207- or default_synopsis
256+ class_name = (
257+ get_input ("class_name [" + default_class_name + "]: " ) or default_class_name
258+ )
259+ example_name = (
260+ get_input ("example_name [" + default_example_name + "]: " )
261+ or default_example_name
262+ )
263+ example_title = titlecase (
264+ space_out_camel_case (example_name ), callback = itk_abbreviations
265+ )
266+ example_synopsis = (
267+ get_input ("example_synopsis [" + default_synopsis + "]: " ) or default_synopsis
268+ )
208269
209270 # Find the ITK module and group name
210271 itk_headers = write_all_itk_headers (itk_src )
211272
212273 NWORDS = train (words (itk_headers .getvalue ()))
213274
214- alphabet = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
275+ alphabet = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
215276
216277 temp_res = dict ()
217- temp_res [' bool' ] = False
278+ temp_res [" bool" ] = False
218279
219280 temp_res = get_group_and_module_from_class_name (itk_src , class_name )
220281
221- while not temp_res [' bool' ]:
282+ while not temp_res [" bool" ]:
222283 class_name = get_input ("Please enter an ITK class name: " )
223284 class_name = itkcorrect (class_name , NWORDS , alphabet )
224285
225- temp_res = get_group_and_module_from_class_name (
226- itk_src , class_name )
286+ temp_res = get_group_and_module_from_class_name (itk_src , class_name )
227287
228- group_name = temp_res [' Group' ]
229- module_name = temp_res [' Module' ]
288+ group_name = temp_res [" Group" ]
289+ module_name = temp_res [" Module" ]
230290
231- print ('Found the following group and module for the provided class: {}'
232- .format (class_name ))
233- print ('Group name: {}' .format (group_name ))
234- print ('Module name: {}' .format (module_name ))
291+ print (
292+ "Found the following group and module for the provided class: {}" .format (
293+ class_name
294+ )
295+ )
296+ print (f"Group name: { group_name } " )
297+ print (f"Module name: { module_name } " )
235298
236- cookiecutter_dir = pjoin (os .path .abspath (os .path .dirname (__file__ )), ' CookieCutter' )
237- cookiecutter_json = pjoin (cookiecutter_dir , ' cookiecutter.json' )
299+ cookiecutter_dir = pjoin (os .path .abspath (os .path .dirname (__file__ )), " CookieCutter" )
300+ cookiecutter_json = pjoin (cookiecutter_dir , " cookiecutter.json" )
238301
239302 data = dict ()
240303 data ["class_name" ] = class_name
@@ -247,8 +310,8 @@ def main():
247310 data ["itk_examples_src" ] = itk_examples_src
248311
249312 if cookiecutter_json :
250- with open (cookiecutter_json , 'w' ) as f :
251- json .dump (data , f )
313+ with open (cookiecutter_json , "w" ) as f :
314+ json .dump (data , f )
252315
253316 # Call the cookiecutter
254317 cookiecutter (cookiecutter_dir , no_input = True )
@@ -257,5 +320,5 @@ def main():
257320 os .remove (cookiecutter_json )
258321
259322
260- if __name__ == ' __main__' :
323+ if __name__ == " __main__" :
261324 main ()
0 commit comments