From 08282d7450dc2ec80a3a0059a730f7c811d64607 Mon Sep 17 00:00:00 2001 From: Tom Keffer Date: Tue, 27 Mar 2012 15:36:35 +0000 Subject: [PATCH] Rewrote config_database. Added config_vp and config_database to the list of scripts to be installed. Moved daemon.py to weeutil. --- bin/config_database.py | 104 ++++++++++++++++-------------------- bin/config_vp.py | 21 ++++---- bin/{ => weeutil}/daemon.py | 0 bin/weewx/wxengine.py | 4 +- docs/CHANGES.txt | 11 ++-- setup.py | 8 +-- 6 files changed, 71 insertions(+), 77 deletions(-) rename bin/{ => weeutil}/daemon.py (100%) diff --git a/bin/config_database.py b/bin/config_database.py index 5a6fb189..ae96ee20 100755 --- a/bin/config_database.py +++ b/bin/config_database.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -# Copyright (c) 2009, 2010, 2011 Tom Keffer +# Copyright (c) 2009, 2010, 2011, 2012 Tom Keffer # # See the file LICENSE.txt for your full rights. # @@ -8,87 +8,74 @@ # $Author$ # $Date$ # -"""Configure various resources used by weewx""" +"""Configure the databases used by weewx""" +import argparse +import os.path import sys import syslog -import os.path -import optparse + import configobj import user.extensions #@UnusedImport import weewx.archive import weewx.stats -usagestr = """%prog: config_path [Options] - -Configuration program for the weewx weather system. - -Arguments: - config_path: Path to the configuration file to be used.""" +description="""Configures the weewx databases. Most of these functions are handled automatically +by weewx, but they may be useful as a utility in special cases. In particular, +the reconfigure-database option can be useful if you decide to add or drop data types +from the database schema.""" def main(): # Set defaults for the system logger: - syslog.openlog('configure', syslog.LOG_PID|syslog.LOG_CONS) + syslog.openlog('config_database', syslog.LOG_PID|syslog.LOG_CONS) # Create a command line parser: - parser = optparse.OptionParser(usage=usagestr) - # This is a bit of a cludge. Get the path for the configuration file: - for arg in sys.argv[1:]: - if arg[0] != '-': - config_path = arg - break - else: - sys.stderr.write("Missing configuration file\n\n") - print parser.parse_args(["--help"]) - sys.exit(weewx.CMD_ERROR) - - # Try to open up the given configuration file. Declare an error if unable to. - try : - config_dict = configobj.ConfigObj(config_path, file_error=True) - except IOError: - print "Unable to open configuration file ", config_path - syslog.syslog(syslog.LOG_CRIT, "main: Unable to open configuration file %s" % config_path) - sys.exit(weewx.CONFIG_ERROR) - - # Get the hardware type from the configuration dictionary - # (this will be a string such as "VantagePro"), - # then import the appropriate module: - stationType = config_dict['Station']['station_type'] - station_mod = __import__('weewx.'+ stationType) - - # Add its options to the list: - getattr(station_mod, stationType).getOptionGroup(parser) - - group = optparse.OptionGroup(parser,"Special database configuration options", - "These options are for advanced needs and are not normally needed.") - - # Add the database options: - group.add_option("--create-database", action="store_true", dest="create_database", help="To create the main database archive.") - group.add_option("--create-stats", action="store_true", dest="create_stats", help="To create the statistical database.") - group.add_option("--backfill-stats", action="store_true", dest="backfill_stats", help="To backfill the statistical database from the main database.") - group.add_option("--reconfigure-database",action="store_true", dest="reconfig_database",help="To reconfigure the main database archive.") - parser.add_option_group(group) + parser = argparse.ArgumentParser(description=description) + # Add the various options: + parser.add_argument("config_path", + help="Path to the configuration file (Required)") + parser.add_argument("--create-database", dest="create_database", action='store_true', + help="Create the archive database.") + parser.add_argument("--create-stats", dest="create_stats", action='store_true', + help="Create the statistical database.") + parser.add_argument("--backfill-stats", dest="backfill_stats", action='store_true', + help="Backfill the statistical database using the archive database") + parser.add_argument("--reconfigure-database", dest="reconfigure_database", action='store_true', + help="""Reconfigure the archive database. The schema found in bin/user/schemas.py will + be used for the new database. It will have the same name as the old database, but with + suffic '.new'. It will then be populated with the data from the old database. """) # Now we are ready to parse the command line: - (options, args) = parser.parse_args() - - if options.create_database: + args = parser.parse_args() + + # Try to open up the configuration file. Declare an error if unable to. + try : + config_dict = configobj.ConfigObj(args.config_path, file_error=True) + except IOError: + print >>sys.stderr, "Unable to open configuration file ", args.config_path + syslog.syslog(syslog.LOG_CRIT, "Unable to open configuration file %s" % args.config_path) + exit(1) + except configobj.ConfigObjError: + print >>sys.stderr, "Error wile parsing configuration file %s" % args.config_path + syslog.syslog(syslog.LOG_CRIT, "Error while parsing configuration file %s" % args.config_path) + exit(1) + + syslog.syslog(syslog.LOG_INFO, "Using configuration file %s." % args.config_path) + + if args.create_database: createMainDatabase(config_dict) - if options.create_stats: + if args.create_stats: createStatsDatabase(config_dict) - if options.backfill_stats: + if args.backfill_stats: backfillStatsDatabase(config_dict) - if options.reconfig_database: + if args.reconfig_database: reconfigMainDatabase(config_dict) - # Now run any hardware specific options: - getattr(station_mod, stationType).runOptions(config_dict, options, args) - def createMainDatabase(config_dict): """Create the main weewx archive database""" # Open up the main database archive @@ -147,4 +134,5 @@ def reconfigMainDatabase(config_dict): newArchiveFilename = oldArchiveFilename + ".new" weewx.archive.reconfig(oldArchiveFilename, newArchiveFilename) -main() +if __name__=="__main__" : + main() diff --git a/bin/config_vp.py b/bin/config_vp.py index 13c6580b..4ff3a61a 100755 --- a/bin/config_vp.py +++ b/bin/config_vp.py @@ -26,34 +26,34 @@ epilog = """Mutating actions will request confirmation before proceeding.""" def main(): # Set defaults for the system logger: - syslog.openlog('vpconfig', syslog.LOG_PID|syslog.LOG_CONS) + syslog.openlog('config_vp', syslog.LOG_PID|syslog.LOG_CONS) # Create a command line parser: parser = argparse.ArgumentParser(description=description, epilog=epilog) # Add the various options: parser.add_argument("config_path", - help="Path to the configuration file (weewx.conf)") + help="Path to the configuration file (Required)") parser.add_argument("--info", action="store_true", dest="info", help="To print configuration, reception, and barometer calibration information about your weather station.") parser.add_argument("--clear", action="store_true", dest="clear", help="To clear the memory of your weather station.") - parser.add_argument("--set_interval", type=int, + parser.add_argument("--set-interval", type=int, dest="set_interval", help="Sets the archive interval to the specified value in seconds. "\ "Valid values are 60, 300, 600, 900, 1800, 3600, or 7200.", metavar="SECONDS") - parser.add_argument("--set_altitude", type=float, + parser.add_argument("--set-altitude", type=float, dest="set_altitude", help="Sets the altitude of the station to the specified number of feet.", metavar="FEET") - parser.add_argument("--set_barometer", type=float, + parser.add_argument("--set-barometer", type=float, dest="set_barometer", help="Sets the barometer reading of the station to a known correct value in inches of mercury. "\ "Specify 0 (zero) to have the console pick a sensible value.", metavar="INHG") - parser.add_argument("--set_bucket", type=int, + parser.add_argument("--set-bucket", type=int, dest="set_bucket", help="Set the type of rain bucket. "\ "Specify '0' for 0.01 inches; '1' for 0.2 MM; '2' for 0.1 MM", metavar="CODE") - parser.add_argument("--set_rain_year_start", type=int, + parser.add_argument("--set-rain-year-start", type=int, dest="set_rain_year_start", choices=[i for i in range(1,13)], help="Set the rain year start (1=Jan, 2=Feb, etc.)", metavar="MM") @@ -66,12 +66,12 @@ def main(): config_dict = configobj.ConfigObj(args.config_path, file_error=True) except IOError: print >>sys.stderr, "Unable to open configuration file ", args.config_path - syslog.syslog(syslog.LOG_CRIT, "main: Unable to open configuration file %s" % args.config_path) - raise + syslog.syslog(syslog.LOG_CRIT, "Unable to open configuration file %s" % args.config_path) + exit(1) except configobj.ConfigObjError: print >>sys.stderr, "Error wile parsing configuration file %s" % args.config_path syslog.syslog(syslog.LOG_CRIT, "Error while parsing configuration file %s" % args.config_path) - raise + exit(1) syslog.syslog(syslog.LOG_INFO, "Using configuration file %s." % args.config_path) @@ -251,4 +251,3 @@ def set_rain_year_start(station, rain_year_start): if __name__=="__main__" : main() - diff --git a/bin/daemon.py b/bin/weeutil/daemon.py similarity index 100% rename from bin/daemon.py rename to bin/weeutil/daemon.py diff --git a/bin/weewx/wxengine.py b/bin/weewx/wxengine.py index 816bed32..7a8113b4 100644 --- a/bin/weewx/wxengine.py +++ b/bin/weewx/wxengine.py @@ -22,7 +22,7 @@ import time # 3rd party imports: import configobj -import daemon +import weeutil.daemon # weewx imports: import weewx.archive @@ -642,7 +642,7 @@ def parseArgs(): sys.exit(weewx.CMD_ERROR) if options.daemon: - daemon.daemonize(pidfile='/var/run/weewx.pid') + weeutil.daemon.daemonize(pidfile='/var/run/weewx.pid') return (options, args) diff --git a/docs/CHANGES.txt b/docs/CHANGES.txt index 9fcb5e80..d2325952 100644 --- a/docs/CHANGES.txt +++ b/docs/CHANGES.txt @@ -6,12 +6,17 @@ For complete documentation, see http://www.weewx.com/docs 2.0.0 XX/YY/12 -Changed the engine architecture so it is more event driven. It now uses callbacks. -This makes it easier to add new events. +Changed the engine architecture so it is more event driven. It now uses +callbacks, making it easier to add new event types. Added utility config_vp, for configuring the VantagePro hardware. -Added utility config_database, for configuring the databases. +Added utility config_database, for configuring the databases. + +Both utilities use argparse (instead of optparse), which is included with Python +2.7. For earlier versions of Python, it will have to be supplied. Installation +is trivial: just download from http://pypi.python.org/pypi/argparse and put +argparse.py in any sys.path directory. When doing DMPAFT, no longer tries to correct for DST. diff --git a/setup.py b/setup.py index 7f6bb1b7..738c39d6 100755 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ # # weewx --- A simple, high-performance weather station server # -# Copyright (c) 2009, 2010, 2011 Tom Keffer +# Copyright (c) 2009, 2010, 2011, 2012 Tom Keffer # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -44,6 +44,8 @@ 6. It backs up any pre-existing bin subdirectory. 7. It conserves the ./bin/user subdirectory. + + 8. Finally, it tries to tidy up things left behind from earlier versions. """ import os @@ -53,8 +55,8 @@ import shutil import sys import tempfile import time -import configobj +import configobj from distutils.core import setup from distutils.command.install_data import install_data from distutils.command.install_lib import install_lib @@ -353,7 +355,7 @@ setup(name='weewx', package_dir = {'' : 'bin'}, packages = ['weewx', 'weeplot', 'weeutil', 'examples', 'user'], py_modules = ['daemon'], - scripts = ['bin/configure.py', 'bin/weewxd.py', 'bin/runreports.py'], + scripts = ['bin/config_database.py', 'bin/config_vp.py', 'bin/weewxd.py', 'bin/runreports.py'], data_files = [('', ['LICENSE.txt', 'README', 'weewx.conf']), ('docs', ['docs/CHANGES.txt', 'docs/customizing.htm', 'docs/daytemp_with_avg.png', 'docs/debian.htm',