Skip to content

Commit

Permalink
mnt: use ensure_list helper
Browse files Browse the repository at this point in the history
  • Loading branch information
akrherz committed Oct 19, 2023
1 parent cbed156 commit d1f4e55
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 46 deletions.
15 changes: 5 additions & 10 deletions cgi-bin/request/asos1min.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pyiem.exceptions import IncompleteWebRequest
from pyiem.util import get_dbconnc
from pyiem.webutil import iemapp
from pyiem.webutil import ensure_list, iemapp

SAMPLING = {
"1min": 1,
Expand Down Expand Up @@ -74,23 +74,18 @@ def compute_prefixes(sio, form, delim, stations, tz) -> dict:
@iemapp()
def application(environ, start_response):
"""Handle mod_wsgi request."""
stations = environ.get("station", [])
stations = ensure_list(environ, "station")
if not stations: # legacy php
stations = environ.get("station[]", [])
# Ensure stations is a list
if isinstance(stations, str):
stations = [stations]
stations = ensure_list(environ, "station[]")
if not stations:
raise IncompleteWebRequest("No station= was specified in request.")
delim = DELIM[environ.get("delim", "comma")]
sample = SAMPLING[environ.get("sample", "1min")]
what = environ.get("what", "dl")
tz = environ.get("tz", "UTC")
varnames = environ.get("vars", [])
varnames = ensure_list(environ, "vars")
if not varnames: # legacy php
varnames = environ.get("vars[]", [])
if isinstance(varnames, str):
varnames = [varnames]
varnames = ensure_list(environ, "vars[]")
if not varnames:
raise IncompleteWebRequest("No vars= was specified in request.")
pgconn, cursor = get_dbconnc("asos1min")
Expand Down
14 changes: 4 additions & 10 deletions cgi-bin/request/daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pyiem.exceptions import IncompleteWebRequest
from pyiem.network import Table as NetworkTable
from pyiem.util import get_dbconn, get_sqlalchemy_conn
from pyiem.webutil import iemapp
from pyiem.webutil import ensure_list, iemapp
from sqlalchemy import text

EXL = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Expand Down Expand Up @@ -138,20 +138,14 @@ def application(environ, start_response):
return [b"ERROR: server over capacity, please try later"]

fmt = environ.get("format", "csv")
stations = environ.get("stations", [])
if isinstance(stations, str):
stations = [stations]
stations = ensure_list(environ, "stations")
if not stations:
stations = environ.get("station", [])
if isinstance(stations, str):
stations = [stations]
stations = ensure_list(environ, "station")
if not stations:
start_response("200 OK", [("Content-type", "text/plain")])
return [b"ERROR: No stations specified for request"]
network = environ.get("network")[:12]
cols = environ.get("var", [])
if isinstance(cols, str):
cols = [cols]
cols = ensure_list(environ, "var")
na = environ.get("na", "None")
if na not in ["M", "None", "blank"]:
start_response("200 OK", [("Content-type", "text/plain")])
Expand Down
10 changes: 3 additions & 7 deletions cgi-bin/request/gis/lsr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import shapefile
from pyiem.exceptions import IncompleteWebRequest
from pyiem.util import get_dbconn, get_sqlalchemy_conn, utc
from pyiem.webutil import iemapp
from pyiem.webutil import ensure_list, iemapp

fiona.supported_drivers["KML"] = "rw"
EXL = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Expand Down Expand Up @@ -114,17 +114,13 @@ def application(environ, start_response):
statelimiter = ""
for opt in ["state", "states", "states[]"]:
if opt in environ:
aStates = environ.get(opt, [])
if isinstance(aStates, str):
aStates = [aStates]
aStates = ensure_list(environ, opt)
aStates.append("XX")
if "_ALL" not in aStates:
statelimiter = f" and l.state in {tuple(aStates)} "
wfoLimiter = ""
if "wfo[]" in environ:
aWFO = environ.get("wfo[]", [])
if isinstance(aWFO, str):
aWFO = [aWFO]
aWFO = ensure_list(environ, "wfo[]")
aWFO.append("XXX") # Hack to make next section work
if "ALL" not in aWFO:
wfoLimiter = f" and l.wfo in {tuple(aWFO)} "
Expand Down
14 changes: 4 additions & 10 deletions cgi-bin/request/gis/watchwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pandas as pd
from pyiem.exceptions import IncompleteWebRequest
from pyiem.util import get_dbconnc, get_sqlalchemy_conn, utc
from pyiem.webutil import iemapp
from pyiem.webutil import ensure_list, iemapp
from shapely.geometry import mapping
from shapely.wkb import loads

Expand All @@ -34,17 +34,13 @@ def parse_wfo_location_group(form):
"""Parse wfoLimiter"""
limiter = ""
if "wfo[]" in form:
wfos = form.get("wfo[]", [])
if not isinstance(wfos, list):
wfos = [wfos]
wfos = ensure_list(form, "wfo[]")
wfos.append("XXX") # Hack to make next section work
if "ALL" not in wfos:
limiter = f" and w.wfo in {tuple(char3(wfos))} "

if "wfos[]" in form:
wfos = form.get("wfos[]", [])
if not isinstance(wfos, list):
wfos = [wfos]
wfos = ensure_list(form, "wfos[]")
wfos.append("XXX") # Hack to make next section work
if "ALL" not in wfos:
limiter = f" and w.wfo in {tuple(char3(wfos))} "
Expand All @@ -59,9 +55,7 @@ def build_sql(environ):
location_group = environ.get("location_group", "wfo")
if location_group == "states":
if "states[]" in environ:
arstates = environ.get("states[]", [])
if not isinstance(arstates, list):
arstates = [arstates]
arstates = ensure_list(environ, "states[]")
states = [x[:2].upper() for x in arstates]
states.append("XX") # Hack for 1 length
wfo_limiter = (
Expand Down
12 changes: 3 additions & 9 deletions cgi-bin/request/talltowers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,9 @@ def application(environ, start_response):
fmt = environ.get("format")
# Build out our variable list
tokens = []
zz = environ.get("z", [])
if isinstance(zz, str):
zz = [zz]
varnames = environ.get("var", [])
if isinstance(varnames, str):
varnames = [varnames]
aggs = environ.get("agg", [])
if isinstance(aggs, str):
aggs = [aggs]
zz = ensure_list(environ, "z")
varnames = ensure_list(environ, "var")
aggs = ensure_list(environ, "agg")
for z in zz:
for v in varnames:
v1 = v
Expand Down

0 comments on commit d1f4e55

Please sign in to comment.