def process(uri): """Process this request This should look something like "/onsite/features/2016/11/161125.png" """ if uri is None: send_content_type("text") ssw("ERROR!") return match = PATTERN.match(uri) if match is None: send_content_type("text") ssw("ERROR!") sys.stderr.write("feature content failure: %s\n" % (repr(uri), )) return data = match.groupdict() fn = ("/mesonet/share/features/%(yyyy)s/%(mm)s/" "%(yymmdd)s%(extra)s.%(suffix)s") % data if os.path.isfile(fn): send_content_type(data['suffix']) ssw(open(fn, 'rb').read()) dblog(data['yymmdd']) else: send_content_type('png') from io import BytesIO from pyiem.plot.use_agg import plt (_, ax) = plt.subplots(1, 1) ax.text(0.5, 0.5, "Feature Image was not Found!", transform=ax.transAxes, ha='center') plt.axis('off') ram = BytesIO() plt.savefig(ram, format='png') ram.seek(0) ssw(ram.read())
def error_image(message, fmt): """Create an error image""" plt.close() _, ax = plt.subplots(1, 1) msg = "IEM Autoplot generation resulted in an error\n%s" % (message,) ax.text(0.5, 0.5, msg, transform=ax.transAxes, ha="center", va="center") ram = BytesIO() plt.axis("off") plt.savefig(ram, format=fmt, dpi=100) ram.seek(0) plt.close() return ram.read()
def process(env): """Process this request This should look something like "/onsite/features/2016/11/161125.png" """ uri = env.get('REQUEST_URI') if uri is None: send_content_type("text") ssw("ERROR!") return match = PATTERN.match(uri) if match is None: send_content_type("text") ssw("ERROR!") sys.stderr.write("feature content failure: %s\n" % (repr(uri), )) return data = match.groupdict() fn = ("/mesonet/share/features/%(yyyy)s/%(mm)s/" "%(yymmdd)s%(extra)s.%(suffix)s") % data if os.path.isfile(fn): rng = env.get("HTTP_RANGE", "bytes=0-") tokens = rng.replace("bytes=", "").split("-", 1) resdata = open(fn, 'rb').read() totalsize = len(resdata) stripe = slice( int(tokens[0]), totalsize if tokens[-1] == '' else (int(tokens[-1]) + 1)) send_content_type(data['suffix'], len(resdata), stripe) ssw(resdata[stripe]) dblog(data['yymmdd']) else: send_content_type('png') from io import BytesIO from pyiem.plot.use_agg import plt (_, ax) = plt.subplots(1, 1) ax.text(0.5, 0.5, "Feature Image was not Found!", transform=ax.transAxes, ha='center') plt.axis('off') ram = BytesIO() plt.savefig(ram, format='png') ram.seek(0) ssw(ram.read())
def application(environ, start_response): """Process this request This should look something like "/onsite/features/2016/11/161125.png" """ headers = [("Accept-Ranges", "bytes")] uri = environ.get("REQUEST_URI") # Option 1, no URI is provided. if uri is None: headers.append(get_content_type("text")) start_response("500 Internal Server Error", headers) return [b"ERROR!"] match = PATTERN.match(uri) # Option 2, the URI pattern is unknown. if match is None: headers.append(get_content_type("text")) start_response("500 Internal Server Error", headers) sys.stderr.write("feature content failure: %s\n" % (repr(uri), )) return [b"ERROR!"] data = match.groupdict() fn = ("/mesonet/share/features/%(yyyy)s/%(mm)s/" "%(yymmdd)s%(extra)s.%(suffix)s") % data # Option 3, we have no file. if not os.path.isfile(fn): # lazy import to save the expense of firing this up when this loads # pylint: disable=import-outside-toplevel from pyiem.plot.use_agg import plt headers.append(get_content_type("png")) (_, ax) = plt.subplots(1, 1) ax.text( 0.5, 0.5, "Feature Image was not Found!", transform=ax.transAxes, ha="center", ) plt.axis("off") ram = BytesIO() plt.savefig(ram, format="png") plt.close() ram.seek(0) start_response("404 Not Found", headers) return [ram.read()] # Option 4, we can support this request. headers.append(get_content_type(data["suffix"])) rng = environ.get("HTTP_RANGE", "bytes=0-") tokens = rng.replace("bytes=", "").split("-", 1) resdata = open(fn, "rb").read() totalsize = len(resdata) stripe = slice( int(tokens[0]), totalsize if tokens[-1] == "" else (int(tokens[-1]) + 1), ) status = "200 OK" if totalsize != (stripe.stop - stripe.start): status = "206 Partial Content" headers.append(("Content-Length", "%.0f" % (stripe.stop - stripe.start, ))) if environ.get("HTTP_RANGE") and stripe is not None: secondval = ("" if environ.get("HTTP_RANGE") == "bytes=0-" else (stripe.stop - 1)) headers.append(( "Content-Range", "bytes %s-%s/%s" % (stripe.start, secondval, totalsize), )) dblog(data["yymmdd"]) start_response(status, headers) return [resdata[stripe]]