From 9f17a039a4d82f0d3ebdf40abafa3676a6df94f5 Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Sat, 28 Sep 2019 00:17:21 +0200 Subject: [PATCH] Avoid encoding issues with Python 3 --- etherpump/commands/common.py | 2 -- etherpump/commands/creatediffhtml.py | 2 +- etherpump/commands/deletepad.py | 5 ++--- etherpump/commands/dumpcsv.py | 16 +++++----------- etherpump/commands/gethtml.py | 2 +- etherpump/commands/index.py | 2 -- etherpump/commands/listauthors.py | 4 ++-- etherpump/commands/publication.py | 2 -- etherpump/commands/pull.py | 13 ++++--------- etherpump/commands/revisionscount.py | 2 +- etherpump/commands/showmeta.py | 2 +- 11 files changed, 17 insertions(+), 35 deletions(-) diff --git a/etherpump/commands/common.py b/etherpump/commands/common.py index 83b2ca6..436d903 100644 --- a/etherpump/commands/common.py +++ b/etherpump/commands/common.py @@ -45,8 +45,6 @@ def padpath(padid, pub_path="", group_path="", normalize=False): def padpath2id(path): - if type(path) == str: - path = path.encode("utf-8") dd, p = os.path.split(path) gname = dd.split("/")[-1] p = unquote_plus(p) diff --git a/etherpump/commands/creatediffhtml.py b/etherpump/commands/creatediffhtml.py index 766a15b..bdf8d10 100644 --- a/etherpump/commands/creatediffhtml.py +++ b/etherpump/commands/creatediffhtml.py @@ -45,6 +45,6 @@ def main(args): if args.format == "json": print(json.dumps(results)) else: - print(results['html'].encode("utf-8")) + print(results['html']) except HTTPError as e: pass diff --git a/etherpump/commands/deletepad.py b/etherpump/commands/deletepad.py index aa77fbb..c2a2c7e 100644 --- a/etherpump/commands/deletepad.py +++ b/etherpump/commands/deletepad.py @@ -24,10 +24,9 @@ def main(args): with open(args.padinfo) as f: info = json.load(f) apiurl = info.get("apiurl") - # apiurl = "{0[protocol]}://{0[hostname]}:{0[port]}{0[apiurl]}{0[apiversion]}/".format(info) data = {} data['apikey'] = info['apikey'] - data['padID'] = args.padid # is utf-8 encoded + data['padID'] = args.padid requesturl = apiurl + 'deletePad?' + urlencode(data) if args.showurl: print(requesturl) @@ -37,4 +36,4 @@ def main(args): print(json.dumps(results)) else: if results['data']: - print(results['data']['text'].encode("utf-8")) + print(results['data']['text']) diff --git a/etherpump/commands/dumpcsv.py b/etherpump/commands/dumpcsv.py index a62c85e..7ce2690 100644 --- a/etherpump/commands/dumpcsv.py +++ b/etherpump/commands/dumpcsv.py @@ -17,7 +17,7 @@ padid, groupid, revisions, lastedited, author_ids groupid is without (g. $) revisions is an integral number of edits lastedited is ISO8601 formatted - author_ids is a space delimited list of internal author IDs + author_ids is a space delimited list of internal author IDs """ groupnamepat = re.compile(r"^g\.(\w+)\$") @@ -69,7 +69,7 @@ def main(args): if len(msg) > maxmsglen: maxmsglen = len(msg) sys.stderr.write("\r{0}".format(" " * maxmsglen)) - sys.stderr.write(msg.encode("utf-8")) + sys.stderr.write(msg) sys.stderr.flush() m = groupnamepat.match(padid) if m: @@ -79,7 +79,7 @@ def main(args): groupname = "" padidnogroup = padid - data['padID'] = padid.encode("utf-8") + data['padID'] = padid revisions = jsonload(apiurl + 'getRevisionsCount?' + urlencode(data))[ 'data' ]['revisions'] @@ -95,15 +95,9 @@ def main(args): author_ids = jsonload(apiurl + 'listAuthorsOfPad?' + urlencode(data))[ 'data' ]['authorIDs'] - author_ids = " ".join(author_ids).encode("utf-8") + author_ids = " ".join(author_ids) out.writerow( - ( - padidnogroup.encode("utf-8"), - groupname.encode("utf-8"), - revisions, - lastedited_iso, - author_ids, - ) + (padidnogroup, groupname, revisions, lastedited_iso, author_ids) ) count += 1 diff --git a/etherpump/commands/gethtml.py b/etherpump/commands/gethtml.py index 8646c3e..3154feb 100644 --- a/etherpump/commands/gethtml.py +++ b/etherpump/commands/gethtml.py @@ -41,4 +41,4 @@ def main(args): if args.format == "json": print(json.dumps(results)) else: - print(results['html'].encode("utf-8")) + print(results['html']) diff --git a/etherpump/commands/index.py b/etherpump/commands/index.py index 3608c14..e811320 100644 --- a/etherpump/commands/index.py +++ b/etherpump/commands/index.py @@ -340,8 +340,6 @@ def main(args): args.timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") padurlbase = re.sub(r"api/1.2.9/$", "p/", info["apiurl"]) - # if type(padurlbase) == unicode: - # padurlbase = padurlbase.encode("utf-8") args.siteurl = args.siteurl or padurlbase args.utcnow = datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S +0000") diff --git a/etherpump/commands/listauthors.py b/etherpump/commands/listauthors.py index 9f78223..d530162 100644 --- a/etherpump/commands/listauthors.py +++ b/etherpump/commands/listauthors.py @@ -26,7 +26,7 @@ def main(args): apiurl = info.get("apiurl") data = {} data['apikey'] = info['apikey'] - data['padID'] = args.padid.encode("utf-8") + data['padID'] = args.padid requesturl = apiurl + 'listAuthorsOfPad?' + urlencode(data) if args.showurl: print(requesturl) @@ -36,4 +36,4 @@ def main(args): print(json.dumps(results)) else: for r in results: - print(r.encode("utf-8")) + print(r) diff --git a/etherpump/commands/publication.py b/etherpump/commands/publication.py index 65c3d4e..c060d6a 100644 --- a/etherpump/commands/publication.py +++ b/etherpump/commands/publication.py @@ -341,8 +341,6 @@ def main(args): args.timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") padurlbase = re.sub(r"api/1.2.9/$", "p/", info["apiurl"]) - # if type(padurlbase) == unicode: - # padurlbase = padurlbase.encode("utf-8") args.siteurl = args.siteurl or padurlbase args.utcnow = datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S +0000") diff --git a/etherpump/commands/pull.py b/etherpump/commands/pull.py index 875667b..8c38ea0 100644 --- a/etherpump/commands/pull.py +++ b/etherpump/commands/pull.py @@ -15,8 +15,6 @@ import html5lib from etherpump.commands.common import * from etherpump.commands.html5tidy import html5tidy -# debugging -# import ElementTree as ET """ pull(meta): @@ -199,10 +197,10 @@ def main(args): continue progressbar(i, numpads, padid) - data['padID'] = padid.encode("utf-8") + data['padID'] = padid p = padpath(padid, args.pub, args.group, args.fix_names) if args.folder: - p = os.path.join(p, padid.encode("utf-8")) + p = os.path.join(p, padid) metapath = p + ".meta.json" revisions = None @@ -210,8 +208,7 @@ def main(args): skip = False padurlbase = re.sub(r"api/1.2.9/$", "p/", info["apiurl"]) meta = {} - # if type(padurlbase) == unicode: - # padurlbase = padurlbase.encode("utf-8") + while True: try: if os.path.exists(metapath): @@ -226,7 +223,7 @@ def main(args): skip = True break - meta['padid'] = padid # .encode("utf-8") + meta['padid'] = padid versions = meta["versions"] = [] versions.append( { @@ -421,7 +418,6 @@ def main(args): links=links, ) with open(ver["path"], "w") as f: - # f.write(html.encode("utf-8")) print( ET.tostring(doc, method="html", encoding="unicode"), file=f, @@ -453,7 +449,6 @@ def main(args): links=links, ) with open(ver["path"], "w") as f: - # f.write(html.encode("utf-8")) print( ET.tostring(doc, method="html", encoding="unicode"), file=f, diff --git a/etherpump/commands/revisionscount.py b/etherpump/commands/revisionscount.py index cee0e0e..ea374af 100644 --- a/etherpump/commands/revisionscount.py +++ b/etherpump/commands/revisionscount.py @@ -21,7 +21,7 @@ def main(args): apiurl = info.get("apiurl") data = {} data['apikey'] = info['apikey'] - data['padID'] = args.padid.encode("utf-8") + data['padID'] = args.padid requesturl = apiurl + 'getRevisionsCount?' + urlencode(data) if args.showurl: print(requesturl) diff --git a/etherpump/commands/showmeta.py b/etherpump/commands/showmeta.py index db44205..d58d6ef 100644 --- a/etherpump/commands/showmeta.py +++ b/etherpump/commands/showmeta.py @@ -35,4 +35,4 @@ def main(args): formatstr = args.format.decode("utf-8") formatstr = re.sub(r"{(\w+)}", r"{0[\1]}", formatstr) - print(formatstr.format(meta).encode("utf-8")) + print(formatstr.format(meta))