index f89464cd70a98f586de6b024b40ac713cb9c8620..289fbdadd59497f1f097ddf1c874fa349817e7f5 100644 (file)
"""
import sys
+import time
import argparse
+import ConfigParser
from pyalpha import PyAlpha, ConversionError
+def death(message):
+ print message
+ sys.exit(-2)
+
+##########################
+#### Module Variables ####
+confname = "./config.ini"
+#### Module Variables ####
+##########################
+
# TODO USE ARGPARSE
parser = argparse.ArgumentParser(description='Alpha Command Communicator',
prog='PyAlpha',
(['-h', '--header'], {
'help' : "Add FILE_a.h to symbol definition list and FILE_a.hdM to symbol decomposition list",
'metavar' : 'FILE',
- 'required' : True,
}),
(['-I', '--include'], {
'help' : "Add DIR to the include path. [default: ./alpha]",
'metavar' : 'DIR',
- 'default' : './alpha',
+ #'default' : './alpha',
}),
(['-p', '--port'], {
'help' : "Communication port [default: COM1]",
- 'default' : 'COM1',
+ #'default' : 'COM1',
}),
(['-b', '--baud'], {
'help' : "Baud rate [default: 19200]",
'type' : int,
- 'default' : 19200,
+ #'default' : 19200,
}),
(['-t', '--temp'], {
'help' : "Temporary file save location [default: .]",
'metavar' : 'DIR',
- 'default' : '.',
+ #'default' : '.',
}),
+ (['-bin'], {
+ 'help' : "Binary file location [default: ./bin]",
+ 'metavar' : 'DIR',
+ #'default' : './bin',
+ }),
+
+
(['-r', '--retain'], {
'help' : "Retain temporary files",
'action' : 'store_true',
'action' : 'store_true',
}),
+ (['--repeat'], {
+ 'help' : "Repeat an alpha command N times with a T second break between",
+ 'type' : float,
+ 'nargs' : 2,
+ 'metavar' : 'N T',
+ 'default' : [1.0, 0.0],
+ }),
(['alpha_list'], {
'help' : "Alpha commands, separated by spaces",
- 'nargs' : '+',
- 'metavar' : 'ALPHAS'
+ 'nargs' : '*',
+ 'metavar' : 'ALPHAS',
}),
]
+# Add CLI arguments to parser
for arg in general_args:
parser.add_argument(*arg[0], **arg[1])
+# Parse arguments
args = parser.parse_args()
-p = PyAlpha(args.port, args.header, baud=args.baud, retain=args.retain,
- inc=args.include, alfas=args.temp)
+# User must provide alpha commands
+if not args.alpha_list:
+ parser.print_usage()
+ death('No ALPHAS provided!')
+
+# Parse INI file
+config = ConfigParser.ConfigParser()
+dataset = config.read(confname)
+if not len(dataset):
+ print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
+ print "Could not find config file " + confname + '. Resorting to defaults.'
+ print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
+ print
+ port = header = baud = include = temp = bins = None
+else:
+ port = args.port or config.get('pyalpha', 'port')
+ header = args.header or config.get('pyalpha', 'hfile')
+ baud = args.baud or config.get('pyalpha', 'baud')
+ include = args.include or config.get('pyalpha', 'include')
+ temp = args.temp or config.get('pyalpha', 'temp')
+ bins = args.bin or config.get('pyalpha', 'bins')
+
+# Options are passed, skipped .INI, apply defaults
+if not header:
+ parser.print_usage()
+ death('Please provide header! [-h]')
+
+p = PyAlpha(header, port = port, baud=baud, retain=args.retain,
+ inc=include, bins=bins, alfas=temp)
try:
- ret = p.send(alpha_list=args.alpha_list, fast=args.fast)
- print ret
+ for i in range(int(args.repeat[0])):
+ ret = p.send(alpha_list=args.alpha_list, fast=args.fast)
+ time.sleep(args.repeat[1])
+ print ret
except ConversionError, e:
pass