Skip to content

Commit

Permalink
Sourcery refactored develop branch
Browse files Browse the repository at this point in the history
  • Loading branch information
elsiehupp authored Aug 29, 2023
2 parents e14678e + 9cffdcb commit 9443ccf
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 268 deletions.
69 changes: 24 additions & 45 deletions wikitools3/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(self, wiki, data, write=False, multipart=False):
self.iswrite = write
if wiki.assertval is not None and self.iswrite:
self.data["assert"] = wiki.assertval
if not "maxlag" in self.data and not wiki.maxlag < 0:
if "maxlag" not in self.data and wiki.maxlag >= 0:
self.data["maxlag"] = wiki.maxlag
self.multipart = multipart
if self.multipart:
Expand All @@ -96,7 +96,7 @@ def __init__(self, wiki, data, write=False, multipart=False):
self.response = False
if wiki.auth:
self.headers["Authorization"] = "Basic {0}".format(
base64.encodestring(wiki.auth + ":" + wiki.httppass)
base64.encodestring(f"{wiki.auth}:{wiki.httppass}")
).replace("\n", "")
if hasattr(wiki, "passman"):
self.opener = urllib.request.build_opener(
Expand Down Expand Up @@ -204,10 +204,9 @@ def queryGen(self):
yield data
if "continue" not in data:
break
else:
self.request = copy.deepcopy(reqcopy)
for param in data["continue"]:
self.changeParam(param, data["continue"][param])
self.request = copy.deepcopy(reqcopy)
for param in data["continue"]:
self.changeParam(param, data["continue"][param])

def __longQuery(self, initialdata):
"""For queries that require multiple requests"""
Expand Down Expand Up @@ -261,10 +260,7 @@ def __longQuery(self, initialdata):
res = req.query(False)
for type in possiblecontinues:
total = resultCombine(type, total, res)
if "query-continue" in res:
numkeys = len(res["query-continue"].keys())
else:
numkeys = 0
numkeys = len(res["query-continue"].keys()) if "query-continue" in res else 0
return total

def __getRaw(self):
Expand Down Expand Up @@ -324,9 +320,8 @@ def __parseJSON(self, data):
1
)
)
if lagtime > self.wiki.maxwaittime:
lagtime = self.wiki.maxwaittime
print("Server lag, sleeping for " + str(lagtime) + " seconds")
lagtime = min(lagtime, self.wiki.maxwaittime)
print(f"Server lag, sleeping for {str(lagtime)} seconds")
maxlag = True
time.sleep(int(lagtime) + 0.5)
return False
Expand Down Expand Up @@ -363,34 +358,19 @@ def resultCombine(type, old, new):
ret["query"][type].extend(new["query"][type])
else: # Else its some sort of prop=thing and/or a generator query
for key in new["query"]["pages"].keys(): # Go through each page
if not key in old["query"]["pages"]: # if it only exists in the new one
if key not in old["query"]["pages"]: # if it only exists in the new one
ret["query"]["pages"][key] = new["query"]["pages"][
key
] # add it to the list
else:
if not type in new["query"]["pages"][key]:
continue
elif (
type in new["query"]["pages"][key]
and not type in ret["query"]["pages"][key]
): # if only the new one does, just add it to the return
ret["query"]["pages"][key][type] = new["query"]["pages"][key][type]
continue
else: # Need to check for possible duplicates for some, this is faster than just iterating over new and checking for dups in ret
retset = set(
[
tuple(entry.items())
for entry in ret["query"]["pages"][key][type]
]
)
newset = set(
[
tuple(entry.items())
for entry in new["query"]["pages"][key][type]
]
)
retset.update(newset)
ret["query"]["pages"][key][type] = [dict(entry) for entry in retset]
elif type not in new["query"]["pages"][key]:
continue
elif type not in ret["query"]["pages"][key]: # if only the new one does, just add it to the return
ret["query"]["pages"][key][type] = new["query"]["pages"][key][type]
else: # Need to check for possible duplicates for some, this is faster than just iterating over new and checking for dups in ret
retset = {tuple(entry.items()) for entry in ret["query"]["pages"][key][type]}
newset = {tuple(entry.items()) for entry in new["query"]["pages"][key][type]}
retset.update(newset)
ret["query"]["pages"][key][type] = [dict(entry) for entry in retset]
return ret


Expand Down Expand Up @@ -425,33 +405,32 @@ def urlencode(query, doseq=0):
for k, v in query:
k = quote_plus(str(k))
v = quote_plus(str(v))
l.append(k + "=" + v)
l.append(f"{k}={v}")
else:
for k, v in query:
k = quote_plus(str(k))
if isinstance(v, str):
v = quote_plus(v)
l.append(k + "=" + v)
l.append(f"{k}={v}")
elif isinstance(v, (int, float)):
v = quote_plus(str(v))
l.append(k + "=" + v)
l.append(f"{k}={v}")
elif v.type(str): # TODO: .type() broken for python 3

# is there a reasonable way to convert to ASCII?
# encode generates a string, but "replace" or "ignore"
# lose information and "strict" can raise UnicodeError
v = quote_plus(v.encode("utf8", "replace"))
l.append(k + "=" + v)
l.append(f"{k}={v}")
else:
try:
# is this a sufficient test for sequence-ness?
x = len(v)
except TypeError:
# not a sequence
v = quote_plus(str(v))
l.append(k + "=" + v)
l.append(f"{k}={v}")
else:
# loop over the sequence
for elt in v:
l.append(k + "=" + quote_plus(str(elt)))
l.extend(f"{k}={quote_plus(str(elt))}" for elt in v)
return "&".join(l)
Loading

0 comments on commit 9443ccf

Please sign in to comment.