Index: autopilot/trunk/autopilot/add_glider.py =================================================================== --- autopilot/trunk/autopilot/add_glider.py (revision 68) +++ autopilot/trunk/autopilot/add_glider.py (revision 72) @@ -3,4 +3,8 @@ Usage: + + add-glider glider + + or python add_glider.py path/to/config/file glider Index: autopilot/trunk/autopilot/add_option.py =================================================================== --- (revision ) +++ autopilot/trunk/autopilot/add_option.py (revision 72) @@ -1,0 +1,172 @@ +""" +Add option to glider configuration. + + Usage: + + add-option glider option value + + or + + python add_option.py path/to/config/file glider option value +""" + +import sys +import math +import errno +import lockfile as lf +import ConfigParser as cp + +def assert_degrees(deg): + assert 0 <= math.radians(deg) <= math.pi*2 + +valid_options = {"bearingdeg": + {"description": + "Direction 0-360 degrees CW from north " \ + "for bearing task.", + "default": None, + "prereqs": None, + "validators": [assert_degrees,], + }, + "bearingdist": + {"description": + "Distance in kilometers per step " \ + "for the bearing task.", + "default": 2, + "prereqs": ["bearingdeg",], + "validators": [float,], + }, + "bearingsteps": + {"description": + "Number of steps for the bearing task.", + "default": 20, + "prereqs": ["bearingdeg", "bearingdist",], + "validators": [int,], + }, + "targetlat": + {"description": + "Latitude in ISO format (+/-ddmm.mmmm) " \ + "for target task.", + "default": None, + "prereqs": None, + "validators": [float,], + }, + "targetlon": + {"description": + "Longitude in ISO format (+/-ddmm.mmmm) " \ + "for target task.", + "default": None, + "prereqs": None, + "validators": [float,], + }, + "targetsteps": + {"description": + "Number of steps for the target task.", + "default": 20, + "prereqs": ["targetlat", "targetlon"], + "validators": [int,], + }, + } + +def _usage(): + options = valid_options.items() + options.sort() + print __doc__ + print " Valid options:" + print "\n ", + option_list = [] + for k, v in options: + option_list.append("{0}: {1[description]}".format(k, v)) + if v["default"]: + option_list.append("{0:>{width}}Default = {1[default]}.". + format('', v, width=(len(k) + 2))) + print "\n ".join(option_list) + print + return + +if __name__ == "__main__": + num_args = len(sys.argv) - 1 + if num_args != 4: + _usage() + else: + cfg_path = sys.argv[1] + glider = sys.argv[2].lower() + option = sys.argv[3].lower() + value = sys.argv[4].lower() + if option not in valid_options: + _usage() + else: + report = {"glider": glider, + "path": cfg_path, + "option": option, + "value": value, + } + try: + lock = lf.FileLock(cfg_path) + lock.acquire(5) + except lf.LockTimeout: + lock.break_lock() + lock.acquire(0) + config = cp.SafeConfigParser() + try: + with open(cfg_path) as handle: + config.readfp(handle) + except IOError as e: + if e.errno == errno.ENOENT: + print "Glider {glider} does not exist in {path}.". \ + format(**report) + else: + raise + else: + prereqs = valid_options[option]["prereqs"] + if prereqs: + for prereq in prereqs: + try: + config.get(glider,prereq) + except cp.NoOptionError: + report["prereq"] = prereq + print "The {prereq} option is a prerequisite " \ + "option for the {option} option.". \ + format(**report) + validators = valid_options[option]["validators"] + if validators: + for validator in validators: + try: + validator(value) + except: + report["validator"] = validator + print "The value of {value} is not valid " \ + "for option {option}.". \ + format(**report) + report.update(valid_options[option]) + print "Valid {option}: {description}". \ + format(**report) + if "prereq" in report or "validator" in report: + print "The {option} option for glider {glider} " \ + "not updated in {path}". \ + format(**report) + else: + try: + report["old_value"] = config.get(glider, option) + config.set(glider, option, value) + print "The {option} option for glider {glider} " \ + "changed from {old_value} to {value} " \ + "in {path}.". \ + format(**report) + except cp.NoOptionError: + config.set(glider, option, value) + print "The {option} option for glider {glider} " \ + "set to {value} in {path}.". \ + format(**report) + except cp.NoSectionError: + config.add_section(glider) + print "Glider {glider} added to {path}.". \ + format(**report) + config.set(glider, option, value) + print "The {option} option for glider {glider} " \ + "set to {value} in {path}.". \ + format(**report) + finally: + with open(cfg_path, "w") as handle: + config.write(handle) + finally: + lock.release() Index: autopilot/trunk/autopilot/add_task.py =================================================================== --- autopilot/trunk/autopilot/add_task.py (revision 68) +++ autopilot/trunk/autopilot/add_task.py (revision 72) @@ -3,4 +3,8 @@ Usage: + + add-task glider task + + or python add_task.py path/to/config/file glider task Index: autopilot/trunk/autopilot/delete_glider.py =================================================================== --- autopilot/trunk/autopilot/delete_glider.py (revision 68) +++ autopilot/trunk/autopilot/delete_glider.py (revision 72) @@ -3,4 +3,8 @@ Usage: + + delete-glider glider + + or python delete_glider.py path/to/config/file glider Index: autopilot/trunk/autopilot/delete_task.py =================================================================== --- autopilot/trunk/autopilot/delete_task.py (revision 68) +++ autopilot/trunk/autopilot/delete_task.py (revision 72) @@ -3,4 +3,8 @@ Usage: + + delete-task glider task + + or python delete_task.py path/to/config/file glider task Index: autopilot/trunk/autopilot/list_gliders.py =================================================================== --- autopilot/trunk/autopilot/list_gliders.py (revision 68) +++ autopilot/trunk/autopilot/list_gliders.py (revision 72) @@ -3,4 +3,8 @@ Usage: + + list-gliders + + or python list_gliders.py path/to/config/file Index: autopilot/trunk/autopilot/list_tasks.py =================================================================== --- autopilot/trunk/autopilot/list_tasks.py (revision 68) +++ autopilot/trunk/autopilot/list_tasks.py (revision 72) @@ -3,4 +3,8 @@ Usage: + + list-tasks glider + + or python list_tasks.py path/to/config/file glider Index: autopilot/trunk/cmd/add-glider =================================================================== --- autopilot/trunk/cmd/add-glider (revision 67) +++ autopilot/trunk/cmd/add-glider (revision 72) @@ -3,4 +3,4 @@ cd /home/localuser/pilottools/lb/toolshed/autopilot/ source bin/activate -python autopilot/add_glider.py config/autopilot.cfg $1 +python autopilot/add_glider.py etc/autopilot.cfg $1 Index: autopilot/trunk/cmd/add-option =================================================================== --- (revision ) +++ autopilot/trunk/cmd/add-option (revision 72) @@ -1,0 +1,5 @@ +#!/bin/bash + +cd /home/localuser/pilottools/lb/toolshed/autopilot/ +source bin/activate +python autopilot/add_option.py etc/autopilot.cfg $1 $2 $3 Index: autopilot/trunk/cmd/add-task =================================================================== --- autopilot/trunk/cmd/add-task (revision 67) +++ autopilot/trunk/cmd/add-task (revision 72) @@ -3,4 +3,4 @@ cd /home/localuser/pilottools/lb/toolshed/autopilot/ source bin/activate -python autopilot/add_task.py config/autopilot.cfg $1 $2 +python autopilot/add_task.py etc/autopilot.cfg $1 $2 Index: autopilot/trunk/cmd/delete-glider =================================================================== --- autopilot/trunk/cmd/delete-glider (revision 67) +++ autopilot/trunk/cmd/delete-glider (revision 72) @@ -3,4 +3,4 @@ cd /home/localuser/pilottools/lb/toolshed/autopilot/ source bin/activate -python autopilot/delete_glider.py config/autopilot.cfg $1 +python autopilot/delete_glider.py etc/autopilot.cfg $1 Index: autopilot/trunk/cmd/delete-task =================================================================== --- autopilot/trunk/cmd/delete-task (revision 67) +++ autopilot/trunk/cmd/delete-task (revision 72) @@ -3,3 +3,3 @@ cd /home/localuser/pilottools/lb/toolshed/autopilot/ source bin/activate -python autopilot/delete_task.py config/autopilot.cfg $1 $2 +python autopilot/delete_task.py etc/autopilot.cfg $1 $2 Index: autopilot/trunk/cmd/list-gliders =================================================================== --- autopilot/trunk/cmd/list-gliders (revision 67) +++ autopilot/trunk/cmd/list-gliders (revision 72) @@ -3,4 +3,4 @@ cd /home/localuser/pilottools/lb/toolshed/autopilot/ source bin/activate -python autopilot/list_gliders.py config/autopilot.cfg +python autopilot/list_gliders.py etc/autopilot.cfg Index: autopilot/trunk/cmd/list-tasks =================================================================== --- autopilot/trunk/cmd/list-tasks (revision 67) +++ autopilot/trunk/cmd/list-tasks (revision 72) @@ -3,4 +3,4 @@ cd /home/localuser/pilottools/lb/toolshed/autopilot/ source bin/activate -python autopilot/list_tasks.py config/autopilot.cfg $1 +python autopilot/list_tasks.py etc/autopilot.cfg $1