55import argparse
66from pathlib import Path
77from shutil import rmtree
8+ import sys
89from textwrap import indent
910from typing import TYPE_CHECKING
1011
@@ -171,6 +172,42 @@ def get_filename(fmt: str, key: str) -> str:
171172 return fmt % key
172173
173174
175+ def clear_folder (folder : Path , * , force : bool = False , verbose : bool = False ) -> None :
176+ """Delete folder and create new (empty) one.
177+
178+ Parameters
179+ ----------
180+ folder : Path
181+ Folder to clear.
182+ force : bool
183+ Do not ask whether to remove folder.
184+ verbose : bool
185+ Print status.
186+ """
187+ if not folder .exists ():
188+ return
189+
190+ if folder .samefile (Path .cwd ()):
191+ print ("Cannot clear folder as this is current working directory." )
192+ return
193+
194+ if (
195+ not force
196+ and input (f"Running this will clear { folder } , are you sure you want to continue? [y/N] " )
197+ .strip ()
198+ .lower ()
199+ != "y"
200+ ):
201+ print ("Cancelling." )
202+ sys .exit ()
203+
204+ if verbose :
205+ print (f"Deleting { folder } ..." )
206+
207+ rmtree (folder , ignore_errors = True )
208+ folder .mkdir ()
209+
210+
174211def main (args_in : Sequence [str ] | None = None , / ) -> None :
175212 """Parse schemas and dump to file.
176213
@@ -193,25 +230,8 @@ def main(args_in: Sequence[str] | None = None, /) -> None:
193230 if args .verbose :
194231 print (f"Generating schemas for keys { ', ' .join (map (repr , schemas .values ()))} ..." )
195232
196- if args .clear and args .out_folder .exists () and not args .out_folder .samefile (Path .cwd ()):
197- if (
198- not args .force
199- and input (
200- f"Running this will clear { args .out_folder } ,"
201- " are you sure you want to continue? [y/N] "
202- )
203- .strip ()
204- .lower ()
205- != "y"
206- ):
207- print ("Cancelling." )
208- return
209-
210- if args .verbose :
211- print (f"Deleting { args .out_folder } ..." )
212-
213- rmtree (args .out_folder , ignore_errors = True )
214- args .out_folder .mkdir ()
233+ if args .clear :
234+ clear_folder (args .out_folder , force = args .force , verbose = args .verbose )
215235
216236 for key , out_name in zip (schemas .values (), out_names , strict = True ):
217237 out_path = args .out_folder / out_name
0 commit comments