First pass at new command options for utility wee_config

This commit is contained in:
Tom Keffer
2015-06-09 07:54:48 -07:00
parent db8fafad9d
commit b4e43e1656
3 changed files with 64 additions and 60 deletions

View File

@@ -13,43 +13,42 @@ import optparse
from weecfg.config import ConfigEngine
usage = """wee_config --help
usage = """Usage: wee_config --help
wee_config --version
wee_config --list-drivers
wee_config --install --dist-config=DIST_CONFIG --output=OUT_CONFIG
[--driver=DRIVER]
[--driver=DRIVER] [--units=(us|metric)]
[--latitude=yy.y] [--longitude=xx.x] [--altitude=zz.z,(foot|meter)]
[--location="Home Sweet Home"] [--units=(us|metric)]
[--location="Home Sweet Home"]
[--no-prompt]
wee_config --update CONFIG_FILE|--config=CONFIG_FILE --dist-config=DIST_CONFIG
[--output=OUT_CONFIG] [--no-prompt]
wee_config --merge CONFIG_FILE|--config=CONFIG_FILE --dist-config=DIST_CONFIG
--output=OUT_CONFIG
wee_config --modify CONFIG_FILE|--config=CONFIG_FILE
[--driver=DRIVER]
wee_config --upgrade CONFIG_FILE|--config=CONFIG_FILE
--dist-config=DIST_CONFIG
[--output=OUT_CONFIG] [--no-prompt] [--warn-on-error]
wee_config --reconfigure CONFIG_FILE|--config=CONFIG_FILE
[--driver=DRIVER] [--units=(us|metric)]
[--latitude=yy.y] [--longitude=xx.x] [--altitude=zz.z,(foot|meter)]
[--location="Home Sweet Home"] [--units=(us|metric)]
[--location="Home Sweet Home"]
[--output=OUT_CONFIG] [--no-prompt]
Configure a weewx configuration file, nominally weewx.conf.
Commands:
--help Show this message then exit.
--version Show the weewx version then exit.
--list-drivers List the available weewx device drivers, then exit.
--install Install a new configuration file starting with the contents of
DIST_CONFIG, prompting as necessary, then save to OUT_CONFIG.
--update Update the contents of configuration file CONFIG_FILE to the
installed version of weewx, then merge the result with the
contents of configuration file DIST_CONFIG. Write the results
to CONFIG_FILE unless an OUT_CONFIG is specified.
--merge Update the contents of configuration file CONFIG_FILE,
then merge the result into configuration file DIST_CONFIG.
Write the results to OUT_CONFIG.
--modify Modify the configuration file, prompting as necessary.
Use this to select a driver, specify the units for display,
or specify station information such as latitude and longitude.
DIST_CONFIG, prompting as necessary. Can take command-line
station values, including a driver.
--upgrade Update the contents of configuration file CONFIG_FILE to the
installer version, then merge the result with the
contents of configuration file DIST_CONFIG.
--reconfigure Modify an existing configuration file CONFIG_FILE. Can take command-line
station values, including a driver. Use this command to add a driver.
Command-line "station" values:
If given, the value will replace what's in CONFIG_FILE.
--driver --units
--latitude --longitude
--altitude --location
"""
def main():
@@ -66,24 +65,22 @@ def main():
parser.add_option("--list-drivers", action="store_true",
help="List the available device drivers.")
parser.add_option("--install", action="store_true",
help="Install a new configuration file, prompting"
" as necessary.")
parser.add_option("--update", action="store_true",
help="Update an existing configuration file.")
parser.add_option("--merge", action="store_true",
help="Merge an existing configuration file.")
parser.add_option("--modify", action="store_true",
help="Modify an existing configuration file, prompting"
" as necessary.")
help="Install a new configuration file.")
parser.add_option("--upgrade", action="store_true",
help="Update an existing configuration file, then merge "
"with a DIST_CONFIG.")
parser.add_option("--reconfigure", action="store_true",
help="Reconfigure an existing configuration file.")
parser.add_option("--config", dest="config_path", metavar="CONFIG_FILE",
help="Use configuration file CONFIG_FILE.")
parser.add_option("--dist-config",
help="Use new/template configuration file DIST_CONFIG.")
parser.add_option("--output", metavar="OUT_CONFIG",
help="Save to configuration file OUT_CONFIG.")
help="Save to configuration file OUT_CONFIG. If not given, "
"then replace existing configuration file.")
parser.add_option("--driver", metavar="DRIVER",
help="Use the driver DRIVER."
" For example, weewx.driver.vantage")
help="Use the driver DRIVER. "
"For example, weewx.driver.vantage")
parser.add_option("--latitude", metavar="yy.y",
help="The station latitude in decimal degrees.")
parser.add_option("--longitude", metavar="xx.x",
@@ -95,11 +92,16 @@ def main():
help="""A text description of the station."""
""" For example, "Santa's workshop, North Pole" """)
parser.add_option("--units", choices=["us", "metric"], metavar="(metric|us)",
help="Set display units to 'metric' or 'us'")
help="Set display units to 'metric' or 'us'.")
parser.add_option("--no-prompt", action="store_true",
help="Do not prompt. Use default or specified values.")
help="Do not prompt. Use default or command-line "
"station values only.")
parser.add_option("--no-backup", action="store_true", default=False,
help="Do not save a backup of any existing files.")
help="If replacing an existing configuration file, "
"do not backup first.")
parser.add_option("--warn-on-error", action="store_true", default=False,
help="Warn if an update is not possible. "
"Otherwise, die (the default).")
parser.add_option("--debug", action="store_true",
help="Show diagnostic information while running.")

View File

@@ -31,36 +31,37 @@ class ConfigEngine(object):
if options.list_drivers:
weecfg.print_drivers()
sys.exit(0)
#
# Check for errors in the options.
#
# Must have some command:
if not options.install and not options.update and \
not options.merge and not options.modify:
sys.exit("No command specified")
# Can have only one of install, update, and merge:
# Must have one, and only one, of install, upgrade, and reconfigure:
if sum(1 if x is True else 0 for x in [options.install,
options.update,
options.merge]) > 1:
sys.exit("Only one of install, update, or merge may be specified")
options.upgrade,
options.reconfigure]) != 1:
sys.exit("One, and only one, of --install, --upgrade, or --reconfigure must be specified.")
# Check for missing --dist-config
if (options.install or options.update or options.merge) and not options.dist_config:
sys.exit("The option --dist-config must be specified")
if (options.install or options.upgrade) and not options.dist_config:
sys.exit("The commands --install or --upgrade require option --dist-config.")
# The install and merge commands require --output. Indeed,
# this is the only difference between --update and --merge.
if (options.install or options.merge) and not options.output:
sys.exit("The option --output must be specified")
if options.install and not options.output:
sys.exit("The --install command requires option --output.")
# The install option does not take an old config file
if options.install and (options.config_path or len(args)):
sys.exit("The install command does not require the config option")
sys.exit("The --install command does not require the config option.")
#
# Error checking done. Now run the commands.
#
# First, fiddle with option --altitude to convert it into a list:
if options.altitude:
options.altitude = options.altitude.split(",")
if options.install or options.update or options.merge:
if options.install or options.upgrade:
# These options require a distribution config file.
# Open it up and parse it:
try:
@@ -88,7 +89,7 @@ class ConfigEngine(object):
output_path = None
if options.update or options.merge:
if options.upgrade:
# Update the old configuration contents
weecfg.update_config(config_dict)
@@ -98,11 +99,11 @@ class ConfigEngine(object):
# Save to the specified output
output_path = options.output
if options.install or options.modify:
if options.install or options.reconfigure:
# Modify the configuration contents
self.modify_config(config_dict, options)
# Save to the specified output, or the original location if not specified
# Save to the specified output, or the original location, if not specified
output_path = options.output if options.output else config_path
if output_path is not None:

View File

@@ -230,7 +230,8 @@ class SerialWrapper(BaseWrapper):
import serial
# Open up the port and store it
self.serial_port = serial.Serial(self.port, self.baudrate, timeout=self.timeout)
syslog.syslog(syslog.LOG_DEBUG, "vantage: Opened up serial port %s, baudrate %d" % (self.port, self.baudrate))
syslog.syslog(syslog.LOG_DEBUG, "vantage: Opened up serial port %s; baud %d; timeout %.2f" %
(self.port, self.baudrate, self.timeout))
def closePort(self):
try: