From cb6de0387dd03a3e64a96825e04eefde4581f753 Mon Sep 17 00:00:00 2001 From: ed Date: Wed, 26 Apr 2023 19:56:27 +0000 Subject: [PATCH] a bit faster --- copyparty/authsrv.py | 8 ++++---- copyparty/httpcli.py | 36 +++++++++++++++++++----------------- copyparty/svchub.py | 12 ++++++------ copyparty/up2k.py | 4 ++-- copyparty/util.py | 6 +++--- 5 files changed, 34 insertions(+), 32 deletions(-) diff --git a/copyparty/authsrv.py b/copyparty/authsrv.py index 282cfebe..beffd29c 100644 --- a/copyparty/authsrv.py +++ b/copyparty/authsrv.py @@ -67,9 +67,9 @@ class AXS(object): self.upget: set[str] = set(upget or []) def __repr__(self) -> str: - return "AXS({})".format( + return "AXS(%s)" % ( ", ".join( - "{}={!r}".format(k, self.__dict__[k]) + "%s=%r" % (k, self.__dict__[k]) for k in "uread uwrite umove udel uget upget".split() ) ) @@ -304,9 +304,9 @@ class VFS(object): self.all_vols = {} def __repr__(self) -> str: - return "VFS({})".format( + return "VFS(%s)" % ( ", ".join( - "{}={!r}".format(k, self.__dict__[k]) + "%s=%r" % (k, self.__dict__[k]) for k in "realpath vpath axs flags".split() ) ) diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index ee841dad..fb226913 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -266,7 +266,7 @@ class HttpCli(object): ) self.host = self.headers.get("host") or "" if not self.host: - zs = "{}:{}".format(*list(self.s.getsockname()[:2])) + zs = "%s:%s" % self.s.getsockname()[:2] self.host = zs[7:] if zs.startswith("::ffff:") else zs n = self.args.rproxy @@ -536,7 +536,7 @@ class HttpCli(object): mime: Optional[str] = None, headers: Optional[dict[str, str]] = None, ) -> None: - response = ["{} {} {}".format(self.http_ver, status, HTTPCODE[status])] + response = ["%s %s %s" % (self.http_ver, status, HTTPCODE[status])] if length is not None: response.append("Content-Length: " + unicode(length)) @@ -560,7 +560,7 @@ class HttpCli(object): self.out_headers["Content-Type"] = mime for k, zs in list(self.out_headers.items()) + self.out_headerlist: - response.append("{}: {}".format(k, zs)) + response.append("%s: %s" % (k, zs)) try: # best practice to separate headers and body into different packets @@ -626,7 +626,7 @@ class HttpCli(object): if not kv: return "" - r = ["{}={}".format(k, quotep(zs)) if zs else k for k, zs in kv.items()] + r = ["%s=%s" % (k, quotep(zs)) if zs else k for k, zs in kv.items()] return "?" + "&".join(r) def redirect( @@ -952,8 +952,10 @@ class HttpCli(object): isdir = stat.S_ISDIR(st.st_mode) - t = "/{}{}" - ret += t.format(quotep(rp), "/" if isdir and rp else "") + ret += "/%s%s" % ( + quotep(rp), + "/" if isdir and rp else "", + ) pvs: dict[str, str] = { "displayname": html_escape(rp.split("/")[-1]), @@ -969,13 +971,13 @@ class HttpCli(object): if k not in props: continue elif v: - ret += "{1}".format(k, v) + ret += "%s" % (k, v, k) else: - ret += "".format(k) + ret += "" % (k,) ret += "HTTP/1.1 200 OK" - missing = ["".format(x) for x in props if x not in pvs] + missing = ["" % (x,) for x in props if x not in pvs] if missing and clen: t = "{}HTTP/1.1 404 Not Found" ret += t.format("".join(missing)) @@ -1641,7 +1643,7 @@ class HttpCli(object): spd1 = get_spd(nbytes, self.t0) spd2 = get_spd(self.conn.nbyte, self.conn.t0) - return "{} {} n{}".format(spd1, spd2, self.conn.nreq) + return "%s %s n%s" % (spd1, spd2, self.conn.nreq) def handle_post_multipart(self) -> bool: self.parser = MultipartParser(self.log, self.sr, self.headers) @@ -3053,7 +3055,7 @@ class HttpCli(object): if self.is_vproxied: parents = self.args.R.split("/") for parent in parents[::-1]: - ret = {"k{}".format(parent): ret, "a": []} + ret = {"k%s" % (parent,): ret, "a": []} zs = json.dumps(ret) self.reply(zs.encode("utf-8"), mime="application/json") @@ -3592,12 +3594,12 @@ class HttpCli(object): if self.args.no_zip: margin = "DIR" else: - margin = 'zip'.format( - quotep(href) - ) + margin = 'zip' % (quotep(href),) elif fn in hist: - margin = '#{}'.format( - base, html_escape(hist[fn][2], quot=True, crlf=True), hist[fn][0] + margin = '#%s' % ( + base, + html_escape(hist[fn][2], quot=True, crlf=True), + hist[fn][0], ) else: margin = "-" @@ -3621,7 +3623,7 @@ class HttpCli(object): ext = "%" if add_fk: - href = "{}?k={}".format( + href = "%s?k=%s" % ( quotep(href), self.gen_fk( self.args.fk_salt, fspath, sz, 0 if ANYWIN else inf.st_ino diff --git a/copyparty/svchub.py b/copyparty/svchub.py index dfa654c3..2b04d735 100644 --- a/copyparty/svchub.py +++ b/copyparty/svchub.py @@ -684,20 +684,20 @@ class SvcHub(object): print("\033[36m{}\033[0m\n".format(dt.strftime("%Y-%m-%d")), end="") self._set_next_day() - fmt = "\033[36m{} \033[33m{:21} \033[0m{}\n" + fmt = "\033[36m%s \033[33m%-21s \033[0m%s\n" if not VT100: - fmt = "{} {:21} {}\n" + fmt = "%s %-21s %s\n" if "\033" in msg: msg = ansi_re.sub("", msg) if "\033" in src: src = ansi_re.sub("", src) elif c: if isinstance(c, int): - msg = "\033[3{}m{}\033[0m".format(c, msg) + msg = "\033[3%sm%s\033[0m" % (c, msg) elif "\033" not in c: - msg = "\033[{}m{}\033[0m".format(c, msg) + msg = "\033[%sm%s\033[0m" % (c, msg) else: - msg = "{}{}\033[0m".format(c, msg) + msg = "%s%s\033[0m" % (c, msg) zd = datetime.utcfromtimestamp(now) ts = "%02d:%02d:%02d.%03d" % ( @@ -706,7 +706,7 @@ class SvcHub(object): zd.second, zd.microsecond // 1000, ) - msg = fmt.format(ts, src, msg) + msg = fmt % (ts, src, msg) try: print(msg, end="") except UnicodeEncodeError: diff --git a/copyparty/up2k.py b/copyparty/up2k.py index 5fbd162a..0e6c74a3 100644 --- a/copyparty/up2k.py +++ b/copyparty/up2k.py @@ -380,9 +380,9 @@ class Up2k(object): if rd.startswith("//") or fn.startswith("//"): rd, fn = s3dec(rd, fn) - fvp = "{}/{}".format(rd, fn).strip("/") + fvp = ("%s/%s" % (rd, fn)).strip("/") if vp: - fvp = "{}/{}".format(vp, fvp) + fvp = "%s/%s" % (vp, fvp) self._handle_rm(LEELOO_DALLAS, "", fvp, []) nrm += 1 diff --git a/copyparty/util.py b/copyparty/util.py index fe24c035..486e0ad0 100644 --- a/copyparty/util.py +++ b/copyparty/util.py @@ -1553,7 +1553,7 @@ def rand_name(fdir: str, fn: str, rnd: int) -> str: def gen_filekey(salt: str, fspath: str, fsize: int, inode: int) -> str: return base64.urlsafe_b64encode( hashlib.sha512( - "{} {} {} {}".format(salt, fspath, fsize, inode).encode("utf-8", "replace") + ("%s %s %s %s" % (salt, fspath, fsize, inode)).encode("utf-8", "replace") ).digest() ).decode("ascii") @@ -1662,7 +1662,7 @@ def uncyg(path: str) -> str: if len(path) > 2 and path[2] != "/": return path - return "{}:\\{}".format(path[1], path[3:]) + return "%s:\\%s" % (path[1], path[3:]) def undot(path: str) -> str: @@ -1705,7 +1705,7 @@ def sanitize_fn(fn: str, ok: str, bad: list[str]) -> str: bad = ["con", "prn", "aux", "nul"] for n in range(1, 10): - bad += "com{0} lpt{0}".format(n).split(" ") + bad += ("com%s lpt%s" % (n, n)).split(" ") if fn.lower().split(".")[0] in bad: fn = "_" + fn