#!/usr/bin/env python # # Copyright (c) 2009-2019 Tom Keffer # # See the file LICENSE.txt for your rights. # """Executable that can run all reports.""" from __future__ import absolute_import from __future__ import print_function import optparse import socket # Although this import is not used, it's important to execute any user extensions before starting. import user.extensions import weecfg import weeutil.logger import weewx.engine import weewx.manager import weewx.reportengine import weewx.station from weeutil.weeutil import timestamp_to_string, to_int description = """Run all reports defined in the specified configuration file. Use this utility to run reports immediately instead of waiting for the end of an archive interval.""" usage = """%prog: [config_file] [timestamp] [--config=CONFIG_FILE] [--help]""" class DummyConsole(object): """A fake console, used for reporting.""" def __init__(self, config_dict): try: self.archive_interval = to_int(config_dict['StdArchive']['archive_interval']) except KeyError: self.archive_interval = 300 class DummyEngine(weewx.engine.StdEngine): """A fake engine, used for reporting.""" def setupStation(self, config_dict): self.console = DummyConsole(config_dict) def disable_timing(section, key): """Function to effectively disable report_timing option""" if key == 'report_timing': section['report_timing'] = "* * * * *" def main(): # Set defaults for logging: weeutil.logger.setup('wee_reports', {}) # Create a command line parser: parser = optparse.OptionParser(description=description, usage=usage) # Add the various options: parser.add_option("--config", dest="config_path", type=str, metavar="CONFIG_FILE", help="Use the configuration file CONFIG_FILE") # Now we are ready to parse the command line: options, args = parser.parse_args() config_path, config_dict = weecfg.read_config(options.config_path, args) print("Using configuration file %s" % config_path) # For wee_reports we want to generate all reports irrespective of any # report_timing settings that may exist. The easiest way to do this is walk # the config dict resetting any report_timing settings found. config_dict.walk(disable_timing) # Look for the debug flag. If set, ask for extra logging weewx.debug = int(config_dict.get('debug', 0)) # Set final configuration for logging: weeutil.logger.setup('wee_reports', config_dict) socket.setdefaulttimeout(10) # Instantiate the dummy engine. This will cause services to get loaded, which will make # the type extensions (xtypes) system available. engine = DummyEngine(config_dict) stn_info = weewx.station.StationInfo(**config_dict['Station']) # If the user specified a time, retrieve it. Otherwise, set to None gen_ts = int(args[0]) if args else None if gen_ts is None: print("Generating for all time") else: print("Generating for requested time %s" % timestamp_to_string(gen_ts)) try: binding = config_dict['StdArchive']['data_binding'] except KeyError: binding = 'wx_binding' # Retrieve the appropriate record from the database with weewx.manager.DBBinder(config_dict) as db_binder: db_manager = db_binder.get_manager(binding) if gen_ts: ts = gen_ts else: ts = db_manager.lastGoodStamp() record = db_manager.getRecord(ts) t = weewx.reportengine.StdReportEngine(config_dict, stn_info, record=record, gen_ts=gen_ts) # Although the report engine inherits from Thread, we can just run it in the main thread: t.run() if __name__ == "__main__": main()