Pull common script code into igor.script and add bin/igorpackedexperiment.py.
authorW. Trevor King <wking@tremily.us>
Sat, 21 Jul 2012 12:18:14 +0000 (08:18 -0400)
committerW. Trevor King <wking@tremily.us>
Sat, 21 Jul 2012 12:27:48 +0000 (08:27 -0400)
To avoid conflicts with the build-in `-v` `--version` argument, the
short form of `--verbose` is now `-V`.

bin/igorbinarywave.py
bin/igorpackedexperiment.py [new file with mode: 0755]
igor/script.py [new file with mode: 0644]

index 9d0fa0eb87d336529eb21d7cbdaf2bbb6d8d023d..dc55ceb7ec6b67f653c817cb602c0eb95c47b253 100755 (executable)
@@ -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 (executable)
index 0000000..056017b
--- /dev/null
@@ -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 (file)
index 0000000..be34b61
--- /dev/null
@@ -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()