File cgi.py of Package failed_python-tableauserverclient
"""
Compatibility shim for the removed 'cgi' stdlib module on Python 3.13+.
This minimal shim implements a small subset of the original cgi API that
the package expects at import time:
- parse_header(value) -> (main, params_dict)
- escape(s, quote=True) -> html.escape(s, quote=quote)
- FieldStorage placeholder to raise a helpful error if actually used
If your code needs more functionality from the original cgi module,
extend this file accordingly. This shim is intentionally small to avoid
pulling in the full legacy implementation.
"""
from __future__ import annotations
from html import escape as _html_escape
from typing import Tuple, Dict, Any
def escape(s: str, quote: bool = True) -> str:
"""
Replacement for cgi.escape (deprecated). Uses html.escape under the hood.
"""
return _html_escape(s, quote=quote)
def parse_header(line: str) -> Tuple[str, Dict[str, str]]:
"""
Parse a Content-Type (or similar) header into a main value and a dict
of parameters.
Simplified implementation: splits on ';' and parses key=value pairs,
stripping quotes around values.
Example:
>>> parse_header('text/plain; charset="utf-8"')
('text/plain', {'charset': 'utf-8'})
"""
if line is None:
return "", {}
parts = [p.strip() for p in line.split(';')]
main = parts[0] if parts else ''
params: Dict[str, str] = {}
for param in parts[1:]:
if not param:
continue
if '=' in param:
key, val = param.split('=', 1)
key = key.strip().lower()
val = val.strip()
# strip surrounding quotes if present
if len(val) >= 2 and ((val[0] == '"' and val[-1] == '"') or (val[0] == "'" and val[-1] == "'")):
val = val[1:-1]
params[key] = val
else:
# parameters without '=' are treated as flags; set to empty string
params[param.lower()] = ''
return main, params
class FieldStorage:
"""
Minimal placeholder for cgi.FieldStorage.
The original FieldStorage is a bulky class for parsing multipart/form-data
and form fields. In this build environment the tests previously passed
under Python 3.11/3.12; the immediate failure was due to the 'cgi'
module import being removed in Python 3.13. If you actually need to use
FieldStorage functionality, replace this placeholder with an adapter
or import an alternative implementation.
Attempting to instantiate this class will raise a clear error.
"""
def __init__(self, *args: Any, **kwargs: Any) -> None: # pragma: no cover - shim
raise RuntimeError(
"cgi.FieldStorage is not available in this shim. "
"If your code requires parsing multipart/form-data or similar, "
"please add a proper parser or extend this compatibility shim."
)
# Provide a small __all__ to mirror the original module's exported names
__all__ = ["escape", "parse_header", "FieldStorage"]