Ran update-copyright.py.
[pycomedi.git] / doc / demo / pycomedi_demo_args.py
1 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of pycomedi.
4 #
5 # pycomedi is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # pycomedi is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # pycomedi.  If not, see <http://www.gnu.org/licenses/>.
16
17 import argparse as _argparse
18 import logging as _logging
19
20 from pycomedi import LOG as _LOG
21 import pycomedi.constant as _constant
22
23
24 class _SetFrequencyAction (_argparse.Action):
25     def __call__(self, parser, namespace, values, option_string=None):
26         setattr(namespace, self.dest, values)
27         setattr(namespace, 'period', 1.0/values)
28
29
30 class _IncrementVerbosityAction (_argparse.Action):
31     def __call__(self, parser, namespace, values, option_string=None):
32         level = _LOG.level
33         level -= 10  # e.g. logging.INFO -> logging.DEBUG
34         if level >= _logging.DEBUG:
35             _LOG.setLevel(level)
36
37
38 # name -> (args, kwargs)
39 ARGUMENTS = {
40     'filename':(
41         ['-f', '--filename'],
42         {'default':'/dev/comedi0',
43          'help':'path to comedi device file'}),
44     'subdevice':(
45         ['-s', '--subdevice'],
46         {'type':int,
47          'help':'subdevice for analog input/output'}),
48     'channel':(
49         ['-c', '--channel'],
50         {'type':int,
51          'default':0,
52          'help':'channel for analog input/output'}),
53     'channels':(
54         ['-c', '--channels'],
55         {'type':lambda x: [int(i) for i in x.split(',')],
56          'default':[0],
57          'help':'comma-separated channels for analog input/output'}),
58     'aref':(
59         ['-a', '--analog-reference'],
60         {'dest':'aref',
61          'default':_constant.AREF.ground,
62          'type':lambda x: _constant.AREF.index_by_name(x),
63          'choices':_constant.AREF,
64          'help':'reference for analog input/output'}),
65     'range':(
66         ['-r', '--range'],
67         {'type':int,
68          'default':0,
69          'help':'range for analog input/output'}),
70     'num-scans':(
71         ['-N', '--num-scans'],
72         {'type':int,
73          'default':10,
74          'help':'number of input/output scans'}),
75     'frequency':(
76         ['-F', '--frequency'],
77         {'type':float,
78          'action':_SetFrequencyAction,
79          'help':'scan frequency in hertz'}),
80     'physical':(
81         ['-p', '--physical'],
82         {'default':False,
83          'action':'store_const',
84          'const':True,
85          'help':'convert input to physical values before printing'}),
86     'mmap':(
87         ['--mmap'],
88         {'default':False,
89          'action':'store_const',
90          'const':True,
91          'help':('use a memory-mapped reader/writer rather than '
92                  'reading/writing the input/output subdevice directly')}),
93     'callback':(
94         ['--callback'],
95         {'default':False,
96          'action':'store_const',
97          'const':True,
98          'help':('use a callback reader/writer rather than '
99                  'reading/writing the input/output subdevice directly')}),
100     'verbose':(
101         ['-v', '--verbose'],
102         {'action':_IncrementVerbosityAction,
103          'nargs':0}),
104     }
105
106 def parse_args(description, argnames, args=None):
107     """
108     >>> args = parse_args(
109     ...     description='Parse a bunch of arguments',
110     ...     argnames=['frequency'], args=['--frequency', '2'])
111     >>> args.period
112     0.5
113     """
114     parser = _argparse.ArgumentParser(description=description)
115     for argument in argnames:
116         args_,kwargs = ARGUMENTS[argument]
117         parser.add_argument(*args_, **kwargs)
118     args = parser.parse_args(args=args)
119     if 'frequency' in argnames and not hasattr(args, 'period'):
120         args.period = 0
121     return args