@@ -289,55 +289,6 @@ def _find_modifier(self, modifier: Path | str) -> Path:
289
289
raise FileNotFoundError (msg )
290
290
291
291
292
- def check_folder_choices (
293
- parent : Path ,
294
- msg : str ,
295
- selection : str ,
296
- list_choices : bool = False ,
297
- predicate = iotools .always_true ,
298
- ) -> Path :
299
- """
300
- A helper function that scans a selected folder for children, which will then be displayed as possible choices.
301
- This funciton allows the model-creator to get only the file/folder names, check
302
- in the desired folder if the choice is present and return the full path to the selected folder.
303
-
304
- Args:
305
- parent (Path): The folder to scan.
306
- msg (str): The message to display, on failure.
307
- selection (str): The current selection.
308
- list_choices (bool): Whether to just list the choices.
309
- In that case `None` is returned, instead of an error
310
- predicate (callable): A function that takes a path and returns True.
311
- if the path results in a valid choice.
312
-
313
- Returns:
314
- Path: Full path of the selected choice in `parent`.
315
-
316
- Examples:
317
- Let's say we expect a choice for a sequence file in the folder `model_root`.
318
-
319
- ```
320
- check_folder_choices(model_root, "Expected sequence file", predicate=lambda p: p.suffix == ".seq")
321
- ```
322
-
323
- Or we want all subfolder of `scenarios`
324
-
325
- ```
326
- check_folder_choices(scenarios, "Expected scenario folder", predicate=lambda p: p.is_dir())
327
- ```
328
- """
329
- choices = [d .name for d in parent .iterdir () if predicate (d )]
330
-
331
- if selection in choices :
332
- return parent / selection
333
-
334
- if list_choices :
335
- for choice in choices :
336
- print (choice )
337
- raise AcceleratorDefinitionError (f"{ msg } .\n Selected: '{ selection } '.\n Choices: [{ ', ' .join (choices )} ]" )
338
-
339
-
340
-
341
292
class SegmentCreator (ModelCreator , ABC ):
342
293
""" Model creator for Segments, to be used in the Segment-by-Segment algorithm.
343
294
These segments propagate the measured values from the beginning of the segment to the end.
@@ -435,3 +386,55 @@ def __init__(self, accel: Accelerator, twiss_out: Path | str, corr_files: Sequen
435
386
self .logfile = self .twiss_out .parent .absolute () / f"job.create_{ self .twiss_out .stem } .log"
436
387
self .corr_files = corr_files
437
388
self .update_dpp = update_dpp
389
+
390
+
391
+ # Helper functions -------------------------------------------------------------
392
+
393
+ def check_folder_choices (
394
+ parent : Path ,
395
+ msg : str ,
396
+ selection : str ,
397
+ list_choices : bool = False ,
398
+ predicate = iotools .always_true ,
399
+ stem_only : bool = False ,
400
+ ) -> Path | str :
401
+ """
402
+ A helper function that scans a selected folder for children, which will then be displayed as possible choices.
403
+ This funciton allows the model-creator to get only the file/folder names, check
404
+ in the desired folder if the choice is present and return the full path to the selected folder.
405
+
406
+ Args:
407
+ parent (Path): The folder to scan.
408
+ msg (str): The message to display, on failure.
409
+ selection (str): The current selection.
410
+ list_choices (bool): Whether to just list the choices.
411
+ In that case `None` is returned, instead of an error
412
+ predicate (callable): A function that takes a path and returns True.
413
+ if the path results in a valid choice.
414
+ stem_only (bool): If True, only the stem of the path is checked.
415
+
416
+ Returns:
417
+ Path: Full path of the selected choice in `parent`.
418
+
419
+ Examples:
420
+ Let's say we expect a choice for a sequence file in the folder `model_root`.
421
+
422
+ ```
423
+ check_folder_choices(model_root, "Expected sequence file", predicate=lambda p: p.suffix == ".seq")
424
+ ```
425
+
426
+ Or we want all subfolder of `scenarios`
427
+
428
+ ```
429
+ check_folder_choices(scenarios, "Expected scenario folder", predicate=lambda p: p.is_dir())
430
+ ```
431
+ """
432
+ choices = [d .stem if stem_only else d .name for d in parent .iterdir () if predicate (d )]
433
+
434
+ if selection in choices :
435
+ return parent / selection
436
+
437
+ if list_choices :
438
+ for choice in choices :
439
+ print (choice )
440
+ raise AcceleratorDefinitionError (f"{ msg } .\n Selected: '{ selection } '.\n Choices: [{ ', ' .join (choices )} ]" )
0 commit comments