From 7bb319f9e641f76ecb69fd729ec6d40ec2eb8042 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 19 Mar 2012 14:51:40 -0400 Subject: [PATCH] Consolidate demo argument parsing in pycomedi_demo_args.py to reduce duplication. --- doc/demo/cmd.py | 61 +++++-------------------- doc/demo/info.py | 9 +--- doc/demo/insn.py | 46 ++++--------------- doc/demo/pycomedi_demo_args.py | 83 ++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 94 deletions(-) create mode 100644 doc/demo/pycomedi_demo_args.py diff --git a/doc/demo/cmd.py b/doc/demo/cmd.py index 6d16bff..3ee20fd 100755 --- a/doc/demo/cmd.py +++ b/doc/demo/cmd.py @@ -11,7 +11,6 @@ by on-board timers or external events. Based on David A. Schleef's `comedilib/demo/cmd.c`. """ -import logging as _logging import sys as _sys import time as _time @@ -103,62 +102,24 @@ def read(device, subdevice=None, channels=[0], range=0, aref=0, period=0, if __name__ == '__main__': - import argparse - - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument( - '-f', '--filename', default='/dev/comedi0', - help='path to comedi device file') - parser.add_argument( - '-s', '--subdevice', type=int, help='subdevice for analog input') - parser.add_argument( - '-c', '--channel', type=int, action='append', - help='add a channel for analog input') - parser.add_argument( - '-a', '--analog-reference', dest='aref', default='ground', - choices=['diff', 'ground', 'other', 'common'], - help='reference for analog input') - parser.add_argument( - '-r', '--range', type=int, default=0, help='range for analog input') - parser.add_argument( - '-N', '--num-scans', type=int, default=10, - help='number of analog input scans') - parser.add_argument( - '-F', '--frequency', type=float, help='scan frequency in hertz') - parser.add_argument( - '-p', '--physical', default=False, action='store_const', const=True, - help='convert input to physical values before printing') - parser.add_argument( - '-v', '--verbose', default=0, action='count') - - args = parser.parse_args() - - if args.verbose >= 3: - _LOG.setLevel(_logging.DEBUG) - elif args.verbose >= 2: - _LOG.setLevel(_logging.INFO) - elif args.verbose >= 1: - _LOG.setLevel(_logging.WARN) + import pycomedi_demo_args + + args = pycomedi_demo_args.parse_args( + description=__doc__, + argnames=['filename', 'subdevice', 'channels', 'aref', 'range', 'num-scans', + 'frequency', 'physical', 'verbose']) _LOG.info(('measuring device={0.filename} subdevice={0.subdevice} ' - 'channel={0.channel} range={0.range} analog reference={0.aref}' + 'channels={0.channels} range={0.range} ' + 'analog-reference={0.aref}' ).format(args)) - channel_indexes = args.channel - if not channel_indexes: - channel_indexes = [0] # user gave no channels on the command line - aref = _constant.AREF.index_by_name(args.aref) - if args.frequency is None: - period = 0 - else: - period = 1/args.frequency - device = _Device(filename=args.filename) device.open() try: read( - device=device, subdevice=args.subdevice, - channels=channel_indexes, range=args.range, aref=aref, - period=period, num_scans=args.num_scans, physical=args.physical) + device=device, subdevice=args.subdevice, channels=args.channels, + range=args.range, aref=args.aref, period=args.period, + num_scans=args.num_scans, physical=args.physical) finally: device.close() diff --git a/doc/demo/info.py b/doc/demo/info.py index 552bd8a..5acded8 100755 --- a/doc/demo/info.py +++ b/doc/demo/info.py @@ -95,14 +95,9 @@ def display(device): if __name__ == '__main__': - import argparse + import pycomedi_demo_args - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument( - '-f', '--filename', default='/dev/comedi0', - help='path to comedi device file') - - args = parser.parse_args() + args = pycomedi_demo_args.parse_args(description=__doc__, argnames=['filename']) device = _Device(filename=args.filename) device.open() diff --git a/doc/demo/insn.py b/doc/demo/insn.py index 9fc1001..52133cf 100755 --- a/doc/demo/insn.py +++ b/doc/demo/insn.py @@ -8,8 +8,6 @@ Do a ``gettimeofday()`` call, read some samples from an analog input, and do another ``gettimeofday()`` call. """ -import logging as _logging - from pycomedi import LOG as _LOG import pycomedi.constant as _constant from pycomedi.device import Device as _Device @@ -18,36 +16,12 @@ from pycomedi.chanspec import ChanSpec as _ChanSpec if __name__ == '__main__': import argparse + import pycomedi_demo_args - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument( - '-f', '--filename', default='/dev/comedi0', - help='path to comedi device file') - parser.add_argument( - '-s', '--subdevice', type=int, help='subdevice for analog input') - parser.add_argument( - '-c', '--channel', type=int, default=0, - help='channel for analog input') - parser.add_argument( - '-a', '--analog-reference', dest='aref', default='ground', - choices=['diff', 'ground', 'other', 'common'], - help='reference for analog input') - parser.add_argument( - '-r', '--range', type=int, default=0, help='range for analog input') - parser.add_argument( - '-N', '--num-scans', type=int, default=10, - help='number of analog input scans') - parser.add_argument( - '-v', '--verbose', default=0, action='count') - - args = parser.parse_args() - - if args.verbose >= 3: - _LOG.setLevel(_logging.DEBUG) - elif args.verbose >= 2: - _LOG.setLevel(_logging.INFO) - elif args.verbose >= 1: - _LOG.setLevel(_logging.WARN) + args = pycomedi_demo_args.parse_args( + description=__doc__, + argnames=['filename', 'subdevice', 'channel', 'aref', 'range', 'num-scans', + 'verbose']) _LOG.info(('measuring device={0.filename} subdevice={0.subdevice} ' 'channel={0.channel} range={0.range} analog reference={0.aref}' @@ -65,18 +39,16 @@ if __name__ == '__main__': insns[0].data = insns[2].data = [0, 0] insns[1].insn = _constant.INSN.read insns[1].data = [0] * args.num_scans - aref = _constant.AREF.index_by_name(args.aref) insns[1].chanspec = _ChanSpec( - chan=args.channel, range=args.range, aref=aref) + chan=args.channel, range=args.range, aref=args.aref) device.do_insnlist(insns) device.close() t1 = insns[0].data[0] + insns[1].data[1]/1e6 t2 = insns[2].data[0] + insns[2].data[1]/1e6 - print('initial time: {}'.format(t1)) - print('final time: {}'.format(t2)) - print('difference: {}'.format(t2-t1)) - print('data:') + _LOG.info('initial time: {}'.format(t1)) + _LOG.info('final time: {}'.format(t2)) + _LOG.info('difference: {}'.format(t2-t1)) for x in insns[1].data: print(x) diff --git a/doc/demo/pycomedi_demo_args.py b/doc/demo/pycomedi_demo_args.py new file mode 100644 index 0000000..83f9f5d --- /dev/null +++ b/doc/demo/pycomedi_demo_args.py @@ -0,0 +1,83 @@ +# Copyright + +import argparse as _argparse +import logging as _logging + +from pycomedi import LOG as _LOG +import pycomedi.constant as _constant + + +class _SetFrequencyAction (_argparse.Action): + def __call__(self, parser, namespace, values, option_string=None): + setattr(namespace, self.dest, values) + setattr(namespace, 'period', 1.0/values) + + +class _IncrementVerbosityAction (_argparse.Action): + def __call__(self, parser, namespace, values, option_string=None): + level = _LOG.level + level -= 10 # e.g. logging.INFO -> logging.DEBUG + if level >= _logging.DEBUG: + _LOG.setLevel(level) + + +# name -> (args, kwargs) +ARGUMENTS = { + 'filename':( + ['-f', '--filename'], + {'default':'/dev/comedi0', + 'help':'path to comedi device file'}), + 'subdevice':( + ['-s', '--subdevice'], + {'type':int, + 'help':'subdevice for analog input/output'}), + 'channel':( + ['-c', '--channel'], + {'type':int, + 'default':0, + 'help':'channel for analog input/output'}), + 'channels':( + ['-c', '--channels'], + {'type':lambda x: [int(i) for i in x.split(',')], + 'default':0, + 'help':'comma-separated channels for analog input/output'}), + 'aref':( + ['-a', '--analog-reference'], + {'dest':'aref', + 'default':_constant.AREF.ground, + 'type':lambda x: _constant.AREF.index_by_name(x), + 'choices':_constant.AREF, + 'help':'reference for analog input/output'}), + 'range':( + ['-r', '--range'], + {'type':int, + 'default':0, + 'help':'range for analog input/output'}), + 'num-scans':( + ['-N', '--num-scans'], + {'type':int, + 'default':10, + 'help':'number of input/output scans'}), + 'frequency':( + ['-F', '--frequency'], + {'type':float, + 'action':_SetFrequencyAction, + 'help':'scan frequency in hertz'}), + 'physical':( + ['-p', '--physical'], + {'default':False, + 'action':'store_const', + 'const':True, + 'help':'convert input to physical values before printing'}), + 'verbose':( + ['-v', '--verbose'], + {'action':_IncrementVerbosityAction}), + } + +def parse_args(description, argnames): + parser = _argparse.ArgumentParser(description=description) + for argument in ['filename', 'subdevice', 'channels', 'aref', 'range', + 'num-scans', 'frequency', 'physical', 'verbose']: + args,kwargs = ARGUMENTS[argument] + parser.add_argument(*args, **kwargs) + return parser.parse_args() -- 2.26.2