From 86b41d98a71fc53dd0024cd327fba2ec61b0d894 Mon Sep 17 00:00:00 2001 From: Georg Meinhardt Date: Fri, 16 Mar 2018 17:24:43 +0100 Subject: [PATCH] Added an [-x FILE] option to igorpackedexperiment.py. --- bin/igorpackedexperiment.py | 5 +++-- igor/script.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/bin/igorpackedexperiment.py b/bin/igorpackedexperiment.py index 0a08444..84b5e71 100755 --- a/bin/igorpackedexperiment.py +++ b/bin/igorpackedexperiment.py @@ -42,11 +42,12 @@ def _run(self, args): f.close() if args.verbose > 0: pprint.pprint(filesystem) - walk(filesystem, self._plot_wave_callback) + walk(filesystem, self._plot_wave_and_execute_callback) - def _plot_wave_callback(self, dirpath, key, value): + def _plot_wave_and_execute_callback(self, dirpath, key, value): if isinstance(value, WaveRecord): self.plot_wave(self.args, value.wave, title=dirpath + [key]) + self.execute(self.args, value.wave, dirpath + [key]) s = PackedScript( diff --git a/igor/script.py b/igor/script.py index 83fde93..ca9da4a 100644 --- a/igor/script.py +++ b/igor/script.py @@ -52,6 +52,13 @@ def __init__(self, description=None, filetype='IGOR Binary Wave (.ibw) file'): self.parser.add_argument( '-V', '--verbose', action='count', default=0, help='increment verbosity') + self.parser.add_argument( + '-x', '--execute', metavar='FILE', default='-', + help=('Path to a custom Python script, which will be executed on ' + 'every wave file. The script can access the wave and the ' + 'corresponding path through the global variables wave and ' + 'dirpath.') + ) self._num_plots = 0 def run(self, *args, **kwargs): @@ -86,6 +93,15 @@ def plot_wave(self, args, wave, title=None): pass self._num_plots += 1 + def execute(self, args, wave, dirpath): + if args.execute == '-': + return # no-op + try: + exec(open(args.execute).read(), {"wave": wave, "dirpath": dirpath}) + except Exception as e: + _LOG.error('error executing on wave {} {}'.format(dirpath, e)) + raise + def display_plots(self): if self._num_plots: _matplotlib_pyplot.show()