diff --git a/copyparty/__main__.py b/copyparty/__main__.py index 461a57e7..f7e93b24 100644 --- a/copyparty/__main__.py +++ b/copyparty/__main__.py @@ -53,13 +53,13 @@ from .util import ( PYFTPD_VER, RAM_AVAIL, RAM_TOTAL, + RE_ANSI, SQLITE_VER, UNPLICATIONS, URL_BUG, URL_PRJ, Daemon, align_tab, - ansi_re, b64enc, dedent, has_resource, @@ -167,7 +167,7 @@ def lprint(*a: Any, **ka: Any) -> None: txt: str = " ".join(unicode(x) for x in a) + eol printed.append(txt) if not VT100: - txt = ansi_re.sub("", txt) + txt = RE_ANSI.sub("", txt) print(txt, end="", **ka) diff --git a/copyparty/svchub.py b/copyparty/svchub.py index 751fd566..73855bf1 100644 --- a/copyparty/svchub.py +++ b/copyparty/svchub.py @@ -51,6 +51,7 @@ from .util import ( HAVE_PSUTIL, HAVE_SQLITE3, HAVE_ZMQ, + RE_ANSI, URL_BUG, UTC, VERSIONS, @@ -60,7 +61,6 @@ from .util import ( HMaccas, ODict, alltrace, - ansi_re, build_netmap, expat_ver, gzip, @@ -1409,9 +1409,9 @@ class SvcHub(object): if self.no_ansi: fmt = "%s %-21s %s\n" if "\033" in msg: - msg = ansi_re.sub("", msg) + msg = RE_ANSI.sub("", msg) if "\033" in src: - src = ansi_re.sub("", src) + src = RE_ANSI.sub("", src) elif c: if isinstance(c, int): msg = "\033[3%sm%s\033[0m" % (c, msg) diff --git a/copyparty/util.py b/copyparty/util.py index 58a883d5..ae968777 100644 --- a/copyparty/util.py +++ b/copyparty/util.py @@ -243,7 +243,17 @@ except: BITNESS = struct.calcsize("P") * 8 -ansi_re = re.compile("\033\\[[^mK]*[mK]") +RE_ANSI = re.compile("\033\\[[^mK]*[mK]") +RE_CTYPE = re.compile(r"^content-type: *([^; ]+)", re.IGNORECASE) +RE_CDISP = re.compile(r"^content-disposition: *([^; ]+)", re.IGNORECASE) +RE_CDISP_FIELD = re.compile( + r'^content-disposition:(?: *|.*; *)name="([^"]+)"', re.IGNORECASE +) +RE_CDISP_FILE = re.compile( + r'^content-disposition:(?: *|.*; *)filename="(.*)"', re.IGNORECASE +) +RE_MEMTOTAL = re.compile("^MemTotal:.* kB") +RE_MEMAVAIL = re.compile("^MemAvailable:.* kB") BOS_SEP = ("%s" % (os.sep,)).encode("ascii") @@ -488,11 +498,11 @@ def read_ram() -> tuple[float, float]: with open("/proc/meminfo", "rb", 0x10000) as f: zsl = f.read(0x10000).decode("ascii", "replace").split("\n") - p = re.compile("^MemTotal:.* kB") + p = RE_MEMTOTAL zs = next((x for x in zsl if p.match(x))) a = int((int(zs.split()[1]) / 0x100000) * 100) / 100 - p = re.compile("^MemAvailable:.* kB") + p = RE_MEMAVAIL zs = next((x for x in zsl if p.match(x))) b = int((int(zs.split()[1]) / 0x100000) * 100) / 100 except: @@ -1698,14 +1708,10 @@ class MultipartParser(object): self.args = args self.headers = http_headers - self.re_ctype = re.compile(r"^content-type: *([^; ]+)", re.IGNORECASE) - self.re_cdisp = re.compile(r"^content-disposition: *([^; ]+)", re.IGNORECASE) - self.re_cdisp_field = re.compile( - r'^content-disposition:(?: *|.*; *)name="([^"]+)"', re.IGNORECASE - ) - self.re_cdisp_file = re.compile( - r'^content-disposition:(?: *|.*; *)filename="(.*)"', re.IGNORECASE - ) + self.re_ctype = RE_CTYPE + self.re_cdisp = RE_CDISP + self.re_cdisp_field = RE_CDISP_FIELD + self.re_cdisp_file = RE_CDISP_FILE self.boundary = b"" self.gen: Optional[