|
18 | 18 | import numpy as np |
19 | 19 |
|
20 | 20 |
|
| 21 | +def _resolve_cli_expression(expression, namespace): |
| 22 | + """Resolve a CLI expression against an explicit namespace. |
| 23 | +
|
| 24 | + Parameters |
| 25 | + ---------- |
| 26 | + expression : str |
| 27 | + The user-supplied CLI expression. |
| 28 | + namespace : dict |
| 29 | + The explicit namespace allowed during evaluation. |
| 30 | +
|
| 31 | + Returns |
| 32 | + ------- |
| 33 | + object |
| 34 | + The resolved class or instance. |
| 35 | + """ |
| 36 | + return eval(expression, {"__builtins__": {}}, namespace) |
| 37 | + |
| 38 | + |
| 39 | +def _baseline_namespace(): |
| 40 | + """Return the baseline classes supported by the CLI.""" |
| 41 | + from diffpy.srmise.baselines.arbitrary import Arbitrary |
| 42 | + from diffpy.srmise.baselines.fromsequence import FromSequence |
| 43 | + from diffpy.srmise.baselines.nanospherical import NanoSpherical |
| 44 | + from diffpy.srmise.baselines.polynomial import Polynomial |
| 45 | + |
| 46 | + return { |
| 47 | + "Arbitrary": Arbitrary, |
| 48 | + "FromSequence": FromSequence, |
| 49 | + "NanoSpherical": NanoSpherical, |
| 50 | + "Polynomial": Polynomial, |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | +def _peakfunction_namespace(): |
| 55 | + """Return the peak-function classes supported by the CLI.""" |
| 56 | + from diffpy.srmise.peaks.gaussian import Gaussian |
| 57 | + from diffpy.srmise.peaks.gaussianoverr import GaussianOverR |
| 58 | + from diffpy.srmise.peaks.terminationripples import TerminationRipples |
| 59 | + |
| 60 | + return { |
| 61 | + "Gaussian": Gaussian, |
| 62 | + "GaussianOverR": GaussianOverR, |
| 63 | + "TerminationRipples": TerminationRipples, |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +def _modelevaluator_namespace(): |
| 68 | + """Return the model evaluators supported by the CLI.""" |
| 69 | + from diffpy.srmise.modelevaluators.aic import AIC |
| 70 | + from diffpy.srmise.modelevaluators.aicc import AICc |
| 71 | + |
| 72 | + return { |
| 73 | + "AIC": AIC, |
| 74 | + "AICc": AICc, |
| 75 | + } |
| 76 | + |
| 77 | + |
21 | 78 | def main(): |
22 | 79 | """Default SrMise entry-point.""" |
23 | 80 |
|
@@ -433,15 +490,21 @@ def main(): |
433 | 490 |
|
434 | 491 | if options.peakfunction: |
435 | 492 | try: |
436 | | - options.peakfunction = eval("peaks." + options.peakfunction) |
| 493 | + options.peakfunction = _resolve_cli_expression( |
| 494 | + options.peakfunction, |
| 495 | + _peakfunction_namespace(), |
| 496 | + ) |
437 | 497 | except Exception as err: |
438 | 498 | print(err) |
439 | 499 | print("Could not create peak function '%s'. Exiting." % options.peakfunction) |
440 | 500 | return |
441 | 501 |
|
442 | 502 | if options.modelevaluator: |
443 | 503 | try: |
444 | | - options.modelevaluator = eval("modelevaluators." + options.modelevaluator) |
| 504 | + options.modelevaluator = _resolve_cli_expression( |
| 505 | + options.modelevaluator, |
| 506 | + _modelevaluator_namespace(), |
| 507 | + ) |
445 | 508 | except Exception as err: |
446 | 509 | print(err) |
447 | 510 | print("Could not find ModelEvaluator '%s'. Exiting." % options.modelevaluator) |
@@ -483,10 +546,12 @@ def main(): |
483 | 546 |
|
484 | 547 | bl = NanoSpherical() |
485 | 548 | options.baseline = parsepars(bl, options.bspherical) |
486 | | - |
| 549 | + elif options.baseline: |
487 | 550 | try: |
488 | | - options.baseline = eval("baselines." + options.baseline) |
489 | | - |
| 551 | + options.baseline = _resolve_cli_expression( |
| 552 | + options.baseline, |
| 553 | + _baseline_namespace(), |
| 554 | + ) |
490 | 555 | except Exception as err: |
491 | 556 | print(err) |
492 | 557 | print("Could not create baseline '%s'. Exiting." % options.baseline) |
|
0 commit comments