Add --delay command line and delay config options
Make the alert and forecast expiration delay user-configurable, both in configuration and at runtime on the command line.
This commit is contained in:
5
weather
5
weather
@@ -2,7 +2,7 @@
|
|||||||
# distributions may wish to edit the above to refer to a specific interpreter
|
# distributions may wish to edit the above to refer to a specific interpreter
|
||||||
# path, such as #!/usr/bin/python
|
# path, such as #!/usr/bin/python
|
||||||
|
|
||||||
# Copyright (c) 2006-2012 Jeremy Stanley <fungi@yuggoth.org>. Permission to
|
# Copyright (c) 2006-2024 Jeremy Stanley <fungi@yuggoth.org>. Permission to
|
||||||
# use, copy, modify, and distribute this software is granted under terms
|
# use, copy, modify, and distribute this software is granted under terms
|
||||||
# provided in the LICENSE file distributed with this software.
|
# provided in the LICENSE file distributed with this software.
|
||||||
|
|
||||||
@@ -101,7 +101,8 @@ else:
|
|||||||
and selections.get_bool("cache_data")
|
and selections.get_bool("cache_data")
|
||||||
),
|
),
|
||||||
cacheage=selections.getint("cacheage"),
|
cacheage=selections.getint("cacheage"),
|
||||||
cachedir=selections.get("cachedir")
|
cachedir=selections.get("cachedir"),
|
||||||
|
delay=selections.getint("delay")
|
||||||
)
|
)
|
||||||
if partial:
|
if partial:
|
||||||
alert_text += "***** %s *****\n%s\n" % (
|
alert_text += "***** %s *****\n%s\n" % (
|
||||||
|
|||||||
19
weather.py
19
weather.py
@@ -320,7 +320,8 @@ def get_alert(
|
|||||||
quiet=False,
|
quiet=False,
|
||||||
cache_data=False,
|
cache_data=False,
|
||||||
cacheage=900,
|
cacheage=900,
|
||||||
cachedir="."
|
cachedir=".",
|
||||||
|
delay=1
|
||||||
):
|
):
|
||||||
"""Return alert notice for the specified URI."""
|
"""Return alert notice for the specified URI."""
|
||||||
if not uri:
|
if not uri:
|
||||||
@@ -343,14 +344,13 @@ def get_alert(
|
|||||||
muted = False
|
muted = False
|
||||||
expirycheck = re.search(r"Expires:([0-9]{12})", alert)
|
expirycheck = re.search(r"Expires:([0-9]{12})", alert)
|
||||||
if expirycheck:
|
if expirycheck:
|
||||||
# only report alerts and forecasts that expired less than
|
# only report alerts and forecasts that expired less than delay
|
||||||
# offset ago
|
# hours ago
|
||||||
import datetime, zoneinfo
|
import datetime, zoneinfo
|
||||||
expiration = datetime.datetime.fromisoformat(
|
expiration = datetime.datetime.fromisoformat(
|
||||||
"%sT%sZ" % (expirycheck[1][:8], expirycheck[1][-4:]))
|
"%sT%sZ" % (expirycheck[1][:8], expirycheck[1][-4:]))
|
||||||
now = datetime.datetime.now(tz=zoneinfo.ZoneInfo("UTC"))
|
now = datetime.datetime.now(tz=zoneinfo.ZoneInfo("UTC"))
|
||||||
# TODO: make this offset configurable
|
if now - expiration > datetime.timedelta(hours=delay):
|
||||||
if now - expiration > datetime.timedelta(hours=1):
|
|
||||||
return ""
|
return ""
|
||||||
lines = alert.split("\n")
|
lines = alert.split("\n")
|
||||||
output = []
|
output = []
|
||||||
@@ -437,6 +437,15 @@ def get_options(config):
|
|||||||
default=default_cachedir,
|
default=default_cachedir,
|
||||||
help="directory for storing cached searches and data")
|
help="directory for storing cached searches and data")
|
||||||
|
|
||||||
|
# the --delay option
|
||||||
|
if config.has_option("default", "delay"):
|
||||||
|
default_delay = config.getint("default", "delay")
|
||||||
|
else: default_delay = 1
|
||||||
|
option_parser.add_option("--delay",
|
||||||
|
dest="delay",
|
||||||
|
default=default_delay,
|
||||||
|
help="hours to delay alert and forecast expiration")
|
||||||
|
|
||||||
# the -f/--forecast option
|
# the -f/--forecast option
|
||||||
if config.has_option("default", "forecast"):
|
if config.has_option("default", "forecast"):
|
||||||
default_forecast = config.getboolean("default", "forecast")
|
default_forecast = config.getboolean("default", "forecast")
|
||||||
|
|||||||
Reference in New Issue
Block a user