cardinal_pythonlib.rnc_web


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Support for web scripts.

cardinal_pythonlib.rnc_web.bold_if_not_blank(x: Optional[str]) → str[source]

HTML-emboldens content, unless blank.

cardinal_pythonlib.rnc_web.cgi_method_is_post(environ: Dict[str, str]) → bool[source]

Determines if the CGI method was POST, given the CGI environment.

cardinal_pythonlib.rnc_web.cgi_parameter_exists(form: cgi.FieldStorage, key: str) → bool[source]

Does a CGI form contain the key?

cardinal_pythonlib.rnc_web.checkbox_checked(b: Any) → str[source]

Returns ' checked="checked"' if b is true; otherwise ''.

Use this code to fill the {} in e.g.:

<label>
    <input type="checkbox" name="myfield" value="1"{}>
    This will be pre-ticked if you insert " checked" where the braces
    are. The newer, more stringent requirement is ' checked="checked"'.
</label>
cardinal_pythonlib.rnc_web.debug_form_contents(form: cgi.FieldStorage, to_stderr: bool = True, to_logger: bool = False) → None[source]

Writes the keys and values of a CGI form to stderr.

cardinal_pythonlib.rnc_web.get_cgi_fieldstorage_from_wsgi_env(env: Dict[str, str], include_query_string: bool = True) → cgi.FieldStorage[source]

Returns a cgi.FieldStorage object from the WSGI environment.

cardinal_pythonlib.rnc_web.get_cgi_parameter_bool(form: cgi.FieldStorage, key: str) → bool[source]

Extracts a boolean parameter from a CGI form, on the assumption that "1" is True and everything else is False.

cardinal_pythonlib.rnc_web.get_cgi_parameter_bool_or_default(form: cgi.FieldStorage, key: str, default: bool = None) → Optional[bool][source]

Extracts a boolean parameter from a CGI form ("1" = True, other string = False, absent/zero-length string = default value).

cardinal_pythonlib.rnc_web.get_cgi_parameter_bool_or_none(form: cgi.FieldStorage, key: str) → Optional[bool][source]

Extracts a boolean parameter from a CGI form ("1" = True, other string = False, absent/zero-length string = None).

cardinal_pythonlib.rnc_web.get_cgi_parameter_datetime(form: cgi.FieldStorage, key: str) → Optional[datetime.datetime][source]

Extracts a date/time parameter from a CGI form. Applies the LOCAL timezone if none specified.

cardinal_pythonlib.rnc_web.get_cgi_parameter_file(form: cgi.FieldStorage, key: str) → Optional[bytes][source]

Extracts a file’s contents from a “file” input in a CGI form, or None if no such file was uploaded.

cardinal_pythonlib.rnc_web.get_cgi_parameter_filename_and_file(form: cgi.FieldStorage, key: str) → Tuple[Optional[str], Optional[bytes]][source]

Extracts a file’s name and contents from a “file” input in a CGI form. Returns (name, contents), or (None, None) if no such file was uploaded.

cardinal_pythonlib.rnc_web.get_cgi_parameter_float(form: cgi.FieldStorage, key: str) → Optional[float][source]

Extracts a float parameter from a CGI form, or None if the key is absent or the string value is not convertible to float.

cardinal_pythonlib.rnc_web.get_cgi_parameter_int(form: cgi.FieldStorage, key: str) → Optional[int][source]

Extracts an integer parameter from a CGI form, or None if the key is absent or the string value is not convertible to int.

cardinal_pythonlib.rnc_web.get_cgi_parameter_list(form: cgi.FieldStorage, key: str) → List[str][source]

Extracts a list of values, all with the same key, from a CGI form.

cardinal_pythonlib.rnc_web.get_cgi_parameter_str(form: cgi.FieldStorage, key: str, default: str = None) → str[source]

Extracts a string parameter from a CGI form. Note: key is CASE-SENSITIVE.

cardinal_pythonlib.rnc_web.get_cgi_parameter_str_or_none(form: cgi.FieldStorage, key: str) → Optional[str][source]

Extracts a string parameter from a CGI form, or None if the key doesn’t exist or the string is zero-length.

cardinal_pythonlib.rnc_web.get_float_or_none(s: str) → Optional[float][source]

Returns the float value of a string, or None if it’s not convertible to a float.

cardinal_pythonlib.rnc_web.get_int_or_none(s: str) → Optional[int][source]

Returns the integer value of a string, or None if it’s not convertible to an int.

cardinal_pythonlib.rnc_web.get_png_data_url(blob: Optional[bytes]) → str[source]

Converts a PNG blob into a local URL encapsulating the PNG.

cardinal_pythonlib.rnc_web.get_png_img_html(blob: Union[bytes, memoryview], extra_html_class: str = None) → str[source]

Converts a PNG blob to an HTML IMG tag with embedded data.

cardinal_pythonlib.rnc_web.getconfigvar_escaped(config: configparser.ConfigParser, section: str, key: str) → Optional[str][source]

Returns a CGI-escaped version of the value read from an INI file using ConfigParser, or None.

cardinal_pythonlib.rnc_web.getenv_escaped(key: str, default: str = None) → Optional[str][source]

Returns an environment variable’s value, CGI-escaped, or None.

cardinal_pythonlib.rnc_web.html_result(html: str, extraheaders: List[Tuple[str, str]] = None) → Tuple[str, List[Tuple[str, str]], bytes][source]

Returns (contenttype, extraheaders, data) tuple for UTF-8 HTML.

cardinal_pythonlib.rnc_web.html_table_from_query(rows: Iterable[Iterable[Optional[str]]], descriptions: Iterable[Optional[str]]) → str[source]

Converts rows from an SQL query result to an HTML table. Suitable for processing output from the defunct function rnc_db.fetchall_with_fieldnames(sql).

cardinal_pythonlib.rnc_web.is_1(s: str) → bool[source]

True if the input is the string literal "1", otherwise False.

cardinal_pythonlib.rnc_web.is_valid_png(blob: Optional[bytes]) → bool[source]

Does a blob have a valid PNG signature?

Adds hyperlinks to text that appears to contain URLs.

See

cardinal_pythonlib.rnc_web.number_to_dp(number: Optional[float], dp: int, default: Optional[str] = '', en_dash_for_minus: bool = True) → str[source]

Format number to dp decimal places, optionally using a UTF-8 en dash for minus signs.

cardinal_pythonlib.rnc_web.option_selected(variable: Any, testvalue: Any) → str[source]

Returns ' selected="selected"' if variable == testvalue else ''; for use with HTML select options.

cardinal_pythonlib.rnc_web.pdf_result(pdf_binary: bytes, extraheaders: List[Tuple[str, str]] = None, filename: str = None) → Tuple[str, List[Tuple[str, str]], bytes][source]

Returns (contenttype, extraheaders, data) tuple for a PDF.

cardinal_pythonlib.rnc_web.print_result_for_plain_cgi_script(contenttype: str, headers: List[Tuple[str, str]], content: bytes, status: str = '200 OK') → None[source]

Writes HTTP request result to stdout.

cardinal_pythonlib.rnc_web.print_result_for_plain_cgi_script_from_tuple(contenttype_headers_content: Tuple[str, List[Tuple[str, str]], bytes], status: str = '200 OK') → None[source]

Writes HTTP result to stdout.

Parameters:
  • contenttype_headers_content – the tuple (contenttype, extraheaders, data)
  • status – HTTP status message (default "200 OK)
cardinal_pythonlib.rnc_web.print_utf8(s: str) → None[source]

Writes a Unicode string to sys.stdout in UTF-8 encoding.

cardinal_pythonlib.rnc_web.replace_nl_with_html_br(string: str) → str[source]

Replaces newlines with <br>.

cardinal_pythonlib.rnc_web.text_result(text: str, extraheaders: List[Tuple[str, str]] = None, filename: str = None) → Tuple[str, List[Tuple[str, str]], bytes][source]

Returns (contenttype, extraheaders, data) tuple for UTF-8 text.

cardinal_pythonlib.rnc_web.tsv_result(text: str, extraheaders: List[Tuple[str, str]] = None, filename: str = None) → Tuple[str, List[Tuple[str, str]], bytes][source]

Returns (contenttype, extraheaders, data) tuple for UTF-8 TSV.

cardinal_pythonlib.rnc_web.webify(v: Any, preserve_newlines: bool = True) → str[source]

Converts a value into an HTML-safe str (formerly, in Python 2: unicode).

Converts value v to a string; escapes it to be safe in HTML format (escaping ampersands, replacing newlines with <br>, etc.). Returns "" for blank input.

cardinal_pythonlib.rnc_web.websafe(value: str) → str[source]

Makes a string safe for inclusion in ASCII-encoded HTML.

cardinal_pythonlib.rnc_web.wsgi_simple_responder(result: Union[str, bytes], handler: Callable[[Union[str, bytes]], Tuple[str, List[Tuple[str, str]], bytes]], start_response: Callable[[str, List[Tuple[str, str]], Optional[Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[traceback]]]], Callable[[str], None]], status: str = '200 OK', extraheaders: List[Tuple[str, str]] = None) → Iterable[bytes][source]

Simple WSGI app.

Parameters:
  • result – the data to be processed by handler
  • handler – a function returning a (contenttype, extraheaders, data) tuple, e.g. text_result, html_result
  • start_response – standard WSGI start_response function
  • status – status code (default "200 OK")
  • extraheaders – optional extra HTTP headers
Returns:

WSGI application result

cardinal_pythonlib.rnc_web.xml_result(xml: str, extraheaders: List[Tuple[str, str]] = None) → Tuple[str, List[Tuple[str, str]], bytes][source]

Returns (contenttype, extraheaders, data) tuple for UTF-8 XML.

cardinal_pythonlib.rnc_web.zip_result(zip_binary: bytes, extraheaders: List[Tuple[str, str]] = None, filename: str = None) → Tuple[str, List[Tuple[str, str]], bytes][source]

Returns (contenttype, extraheaders, data) tuple for a ZIP.