From 1db460428639f218eaa133326d03d3f105847285 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 21 Jul 2012 08:18:14 -0400 Subject: [PATCH] Pull common script code into igor.script and add bin/igorpackedexperiment.py. To avoid conflicts with the build-in `-v` `--version` argument, the short form of `--verbose` is now `-V`. --- bin/igorbinarywave.py | 41 +++++++++--------------------------- bin/igorpackedexperiment.py | 32 ++++++++++++++++++++++++++++ igor/script.py | 42 +++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 31 deletions(-) create mode 100755 bin/igorpackedexperiment.py create mode 100644 igor/script.py diff --git a/bin/igorbinarywave.py b/bin/igorbinarywave.py index 9d0fa0e..dc55ceb 100755 --- a/bin/igorbinarywave.py +++ b/bin/igorbinarywave.py @@ -4,42 +4,21 @@ "IBW -> ASCII conversion" -import logging -import optparse import pprint -import sys import numpy -from igor import __version__, LOG from igor.binarywave import load +from igor.script import Script -p = optparse.OptionParser(version=__version__) +def run(args): + wave = load(args.infile) + numpy.savetxt(args.outfile, wave['wave']['wData'], fmt='%g', delimiter='\t') + if args.verbose > 0: + wave['wave'].pop('wData') + pprint.pprint(wave) -p.add_option('-f', '--infile', dest='infile', metavar='FILE', - default='-', help='Input IGOR Binary Wave (.ibw) file.') -p.add_option('-o', '--outfile', dest='outfile', metavar='FILE', - default='-', help='File for ASCII output.') -p.add_option('-v', '--verbose', dest='verbose', default=0, - action='count', help='Increment verbosity') - -options,args = p.parse_args() - -if len(args) > 0 and options.infile == None: - options.infile = args[0] -if options.infile == '-': - options.infile = sys.stdin -if options.outfile == '-': - options.outfile = sys.stdout - -if options.verbose > 1: - log_levels = [logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG] - log_level = log_levels[min(options.verbose-1, len(log_levels)-1)] - LOG.setLevel(log_level) - -wave = load(options.infile) -numpy.savetxt(options.outfile, wave['wave']['wData'], fmt='%g', delimiter='\t') -if options.verbose > 0: - wave['wave'].pop('wData') - pprint.pprint(wave) +s = Script(description=__doc__) +s._run = run +s.run() diff --git a/bin/igorpackedexperiment.py b/bin/igorpackedexperiment.py new file mode 100755 index 0000000..056017b --- /dev/null +++ b/bin/igorpackedexperiment.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# +# Copyright + +"PXP -> ASCII conversion" + +import pprint + +import numpy + +from igor.packed import load +from igor.script import Script + + +def run(args): + records,filesystem = load(args.infile) + if hasattr(args.outfile, 'write'): + f = args.outfile # filename is actually a stream object + else: + f = open(args.outfile, 'w') + try: + f.write(pprint.pformat(records)) + f.write('\n') + finally: + if f != args.outfile: + f.close() + if args.verbose > 0: + pprint.pprint(filesystem) + +s = Script(description=__doc__, filetype='IGOR Packed Experiment (.pxp) file') +s._run = run +s.run() diff --git a/igor/script.py b/igor/script.py new file mode 100644 index 0000000..be34b61 --- /dev/null +++ b/igor/script.py @@ -0,0 +1,42 @@ +# Copyright + +"Common code for scripts distributed with the `igor` package." + +from __future__ import absolute_import +import argparse as _argparse +import logging as _logging +import sys as _sys + +from . import __version__ +from . import LOG as _LOG + + +class Script (object): + log_levels = [_logging.ERROR, _logging.WARNING, _logging.INFO, _logging.DEBUG] + + def __init__(self, description=None, filetype='IGOR Binary Wave (.ibw) file'): + self.parser = _argparse.ArgumentParser( + description=description, version=__version__) + self.parser.add_argument( + '-f', '--infile', metavar='FILE', default='-', + help='input {}'.format(filetype)) + self.parser.add_argument( + '-o', '--outfile', metavar='FILE', default='-', + help='file for ASCII output') + self.parser.add_argument( + '-V', '--verbose', action='count', default=0, + help='increment verbosity') + + def run(self, *args, **kwargs): + args = self.parser.parse_args(*args, **kwargs) + if args.infile == '-': + args.infile = _sys.stdin + if args.outfile == '-': + args.outfile = _sys.stdout + if args.verbose > 1: + log_level = self.log_levels[min(args.verbose-1, len(self.log_levels)-1)] + _LOG.setLevel(log_level) + self._run(args) + + def _run(self, args): + raise NotImplementedError() -- 2.26.2