Skip to content

Commit

Permalink
refactor: remove usage of cgi
Browse files Browse the repository at this point in the history
fixes #5280
  • Loading branch information
jnoortheen committed Mar 18, 2024
1 parent 18fb513 commit 88830a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion xonsh/webconfig/file_writes.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def config_to_xonsh(


def insert_into_xonshrc(
config,
config: dict,
xonshrc="~/.xonshrc",
prefix="# XONSH WEBCONFIG START",
suffix="# XONSH WEBCONFIG END",
Expand Down
28 changes: 15 additions & 13 deletions xonsh/webconfig/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
import cgi
import contextlib
import json
import os
Expand All @@ -8,6 +7,7 @@
import sys
import typing as tp
from argparse import ArgumentParser
from email.message import EmailMessage
from http import HTTPStatus, server
from pathlib import Path
from pprint import pprint
Expand Down Expand Up @@ -90,24 +90,26 @@ def do_GET(self) -> None:
return super().do_GET()

def _read_form(self):
ctype, pdict = cgi.parse_header(self.headers.get("content-type"))
# if ctype == "multipart/form-data":
# postvars = cgi.parse_multipart(self.rfile, pdict)
if ctype == "application/x-www-form-urlencoded":
return parse.parse_qs(self._read(), keep_blank_values=True)
return {}
msg = EmailMessage()
msg["Content-Type"] = self.headers.get("content-type")
if msg.get_content_type() == "application/x-www-form-urlencoded":
data = parse.parse_qs(self._read(), keep_blank_values=True)
for name, values in data.items():
value = None
if isinstance(name, bytes):
name = name.decode(encoding="utf-8")
if isinstance(values, list) and values:
if isinstance(values[0], bytes):
value = values[0].decode(encoding="utf-8")
yield name, value

def do_POST(self):
"""Reads post request body"""
route = self._get_route("post")
if route is not None:
# redirect after form submission
data = cgi.FieldStorage(
self.rfile,
headers=self.headers,
environ={"REQUEST_METHOD": "POST"},
keep_blank_values=True,
)
data = dict(self._read_form())

new_route = route.post(data) or route
return self._send(redirect=new_route.path)
post_body = self._read()
Expand Down
13 changes: 6 additions & 7 deletions xonsh/webconfig/routes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cgi
import sys
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -220,9 +219,9 @@ def get(self):
# rest
yield from self.get_cols()

def post(self, data: "cgi.FieldStorage"):
if data:
prompt = data.getvalue(self.var_name).replace("\r", "")
def post(self, data: dict[str, str]):
if prompt := data.get(self.var_name):
prompt = prompt.replace("\r", "")
self.env[self.var_name] = prompt
self.update_rc(prompt=prompt)

Expand Down Expand Up @@ -267,10 +266,10 @@ def get(self):
yield t.row()[t.col()[self.xontrib_card(name, data),]]
yield t.br()

def post(self, data: "cgi.FieldStorage"):
if not data.keys():
def post(self, data: dict[str, str]):
if not data:
return
name = data.keys()[0]
name = list(data)[0]
if self.is_loaded(name):
# todo: update rc file
del sys.modules[self.mod_name(name)]
Expand Down

0 comments on commit 88830a4

Please sign in to comment.