Sourcery Starbot ⭐ refactored mfhbam/pyload#1
Conversation
SourceryAI
left a comment
There was a problem hiding this comment.
Sourcery timed out performing refactorings.
Due to GitHub API limits, only the first 60 comments can be shown.
| else: | ||
| enc = "utf8" | ||
|
|
||
| enc = "cp850" if os.name == "nt" else "utf8" |
There was a problem hiding this comment.
Lines 37-41 refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| self.threadManager.pause = False | ||
| return False | ||
| elif not self.threadManager.pause: | ||
| else: |
There was a problem hiding this comment.
Function Core.__init__.toggle_pause refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if)
| f = open(self.pidfile, "wb") | ||
| f.write(str(pid)) | ||
| f.close() | ||
| with open(self.pidfile, "wb") as f: | ||
| f.write(str(pid)) |
There was a problem hiding this comment.
Function Core.__init__.writePidFile refactored with the following changes:
- Use
withwhen opening file to ensure closure (ensure-file-closed)
| pid = f.read().strip() | ||
| f.close() | ||
| with open(self.pidfile, "rb") as f: | ||
| pid = f.read().strip() |
There was a problem hiding this comment.
Function Core.__init__.checkPidFile refactored with the following changes:
- Use
withwhen opening file to ensure closure (ensure-file-closed)
| data = {} | ||
| data["type"] = conn.attribute("type", "remote") | ||
| data = {"type": conn.attribute("type", "remote")} | ||
| data["default"] = conn.attribute("default", "False") | ||
| data["id"] = conn.attribute("id", uuid().hex) | ||
| if data["default"] == "True": | ||
| data["default"] = True | ||
| else: | ||
| data["default"] = False | ||
| data["default"] = data["default"] == "True" | ||
| subs = self.parser.parseNode(conn, "dict") | ||
| if not subs.has_key("name"): | ||
| data["name"] = _("Unnamed") | ||
| else: | ||
| data["name"] = subs["name"].text() | ||
| data["name"] = subs["name"].text() if subs.has_key("name") else _("Unnamed") | ||
| if data["type"] == "remote": | ||
| if not subs.has_key("server"): | ||
| continue | ||
| else: | ||
| data["host"] = subs["server"].text() | ||
| data["user"] = subs["server"].attribute("user", "admin") | ||
| data["port"] = int(subs["server"].attribute("port", "7227")) | ||
| data["password"] = subs["server"].attribute("password", "") | ||
| data["host"] = subs["server"].text() | ||
| data["user"] = subs["server"].attribute("user", "admin") | ||
| data["port"] = int(subs["server"].attribute("port", "7227")) | ||
| data["password"] = subs["server"].attribute("password", "") |
There was a problem hiding this comment.
Function main.getConnections refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else) - Replace if statement with if expression (
assign-if-exp) - Simplify boolean if expression (
boolean-if-exp-identity) - Merge dictionary assignment with declaration (
merge-dict-assign) - Swap if/else branches (
swap-if-else-branches)
| data["queue"], | ||
| data["order"], | ||
| links=[self._convertPyFile(x) for x in data["links"].itervalues()], | ||
| ) |
There was a problem hiding this comment.
Function Api.statusDownloads.getPackageData refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
|
|
||
| if not data: | ||
| raise PackageDoesNotExists(pid) | ||
|
|
||
| pdata = PackageData(data["id"], data["name"], data["folder"], data["site"], data["password"], | ||
| data["queue"], data["order"], | ||
| fids=[int(x) for x in data["links"]]) | ||
|
|
||
| return pdata | ||
| return PackageData( | ||
| data["id"], | ||
| data["name"], | ||
| data["folder"], | ||
| data["site"], | ||
| data["password"], | ||
| data["queue"], | ||
| data["order"], | ||
| fids=[int(x) for x in data["links"]], | ||
| ) |
There was a problem hiding this comment.
Function Api.statusDownloads.getPackageInfo refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| fdata = self._convertPyFile(info.values()[0]) | ||
| return fdata | ||
| return self._convertPyFile(info.values()[0]) |
There was a problem hiding this comment.
Function Api.statusDownloads.getFileData refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| th = open(join(self.core.config["general"]["download_folder"], "tmp_" + filename), "wb") | ||
| th.write(str(data)) | ||
| th.close() | ||
|
|
||
| with open(join(self.core.config["general"]["download_folder"], "tmp_" + filename), "wb") as th: | ||
| th.write(str(data)) |
There was a problem hiding this comment.
Function Api.statusDownloads.uploadContainer refactored with the following changes:
- Use
withwhen opening file to ensure closure (ensure-file-closed)
| while pack["order"] in order.keys(): #just in case | ||
| while pack["order"] in order: #just in case |
There was a problem hiding this comment.
Function Api.statusDownloads.getPackageOrder refactored with the following changes:
- Remove unnecessary call to keys() (
remove-dict-keys)
| while pyfile["order"] in order.keys(): #just in case | ||
| while pyfile["order"] in order: #just in case |
There was a problem hiding this comment.
Function Api.statusDownloads.getFileOrder refactored with the following changes:
- Remove unnecessary call to keys() (
remove-dict-keys)
| return not task is None | ||
| return task is not None |
There was a problem hiding this comment.
Function Api.statusDownloads.isCaptchaWaiting refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan)
| t = CaptchaTask(int(task.id), standard_b64encode(data), type, result) | ||
| return t | ||
| return CaptchaTask(int(task.id), standard_b64encode(data), type, result) |
There was a problem hiding this comment.
Function Api.statusDownloads.getCaptchaTask refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| return True if self.checkAuth(username, password, remoteip) else False | ||
| return bool(self.checkAuth(username, password, remoteip)) |
There was a problem hiding this comment.
Function Api.statusDownloads.login refactored with the following changes:
- Simplify boolean if expression (
boolean-if-exp-identity)
| res = {} | ||
| for user, data in self.core.db.getAllUserData().iteritems(): | ||
| res[user] = UserData(user, data["email"], data["role"], data["permission"], data["template"]) | ||
|
|
||
| return res | ||
| return { | ||
| user: UserData( | ||
| user, | ||
| data["email"], | ||
| data["role"], | ||
| data["permission"], | ||
| data["template"], | ||
| ) | ||
| for user, data in self.core.db.getAllUserData().iteritems() | ||
| } |
There was a problem hiding this comment.
Function Api.statusDownloads.getAllUserData refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
|
|
||
| def setProgress(self, value): | ||
| if not value == self.progress: | ||
| if value != self.progress: |
There was a problem hiding this comment.
Function PyFile.__init__.notifyChange.setProgress refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan)
| try: | ||
| el = heappop(self.queue) | ||
| return el | ||
| return heappop(self.queue) |
There was a problem hiding this comment.
Function PriorityQueue.get refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| pycurl.global_init(pycurl.GLOBAL_DEFAULT) | ||
|
|
||
| for i in range(0, self.core.config.get("download", "max_downloads")): | ||
| for _ in range(self.core.config.get("download", "max_downloads")): |
There was a problem hiding this comment.
Function ThreadManager.__init__ refactored with the following changes:
- Replace range(0, x) with range(x) (
remove-zero-from-range) - Replace unused for index with underscore (
for-index-underscore)
|
|
||
| ip = "" | ||
| for i in range(10): | ||
| for _ in range(10): |
There was a problem hiding this comment.
Function ThreadManager.getIP refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| now = list(time.localtime()[3:5]) | ||
| if start < now < end: return True | ||
| elif start > end and (now > start or now < end): return True | ||
| elif start < now > end < start: return True |
There was a problem hiding this comment.
Function compare_time refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if)
| if m: | ||
| traffic = float(m.group(1).replace(",", ".")) | ||
| unit = m.group(2) | ||
| else: | ||
| if not m: | ||
| return 0 | ||
| traffic = float(m.group(1).replace(",", ".")) | ||
| unit = m.group(2) |
There was a problem hiding this comment.
Function parseFileSize refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| if "-" in inp: | ||
| l, n, h = inp.partition("-") | ||
| l = int(l) | ||
| h = int(h) | ||
| r = range(l, h + 1) | ||
|
|
||
| ret = [] | ||
| if package: | ||
| for p in self.cache: | ||
| if p.pid in r: | ||
| ret.append(p.pid) | ||
| else: | ||
| for l in self.links.links: | ||
| if l.lid in r: | ||
| ret.append(l.lid) | ||
|
|
||
| return ret | ||
| if "-" not in inp: | ||
| return [int(x) for x in inp.split(",")] | ||
|
|
||
| l, n, h = inp.partition("-") | ||
| l = int(l) | ||
| h = int(h) | ||
| r = range(l, h + 1) | ||
|
|
||
| ret = [] | ||
| if package: | ||
| for p in self.cache: | ||
| if p.pid in r: | ||
| ret.append(p.pid) | ||
| else: | ||
| return [int(x) for x in inp.split(",")] | ||
| for l in self.links.links: | ||
| if l.lid in r: | ||
| ret.append(l.lid) | ||
|
|
||
| return ret |
There was a problem hiding this comment.
Function ManageFiles.init.onBackSpace.onEnter.renderBody.getLinks.parseInput refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| """ create some urls, some may fail """ | ||
| urls = [] | ||
| for x in range(0, randint(20, 100)): | ||
| for _ in range(randint(20, 100)): |
There was a problem hiding this comment.
Function createURLs refactored with the following changes:
- Replace range(0, x) with range(x) (
remove-zero-from-range) - Replace unused for index with underscore (
for-index-underscore)
|
|
||
| def startApiExerciser(core, n): | ||
| for i in range(n): | ||
| for _ in range(n): |
There was a problem hiding this comment.
Function startApiExerciser refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| self.api = core.api | ||
|
|
||
|
|
||
| self.api = ThriftClient(user=user, password=pw) if thrift else core.api |
There was a problem hiding this comment.
Function APIExerciser.__init__ refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| if not ids or (pyfile.id in ids and len(ids) == 1): | ||
| if not pyfile.package().setFinished: | ||
| self.core.log.info(_("Package finished: %s") % pyfile.package().name) | ||
| self.core.hookManager.packageFinished(pyfile.package()) | ||
| pyfile.package().setFinished = True | ||
| if ( | ||
| not ids or (pyfile.id in ids and len(ids) == 1) | ||
| ) and not pyfile.package().setFinished: | ||
| self.core.log.info(_("Package finished: %s") % pyfile.package().name) | ||
| self.core.hookManager.packageFinished(pyfile.package()) | ||
| pyfile.package().setFinished = True |
There was a problem hiding this comment.
Function FileHandler.addLinks.deleteLink.updateLink.restartFile.checkPackageFinished refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs)
| urls = [] | ||
| urls = [ | ||
| (pyfile["url"], pyfile["plugin"]) | ||
| for pyfile in data.itervalues() | ||
| if pyfile["status"] not in (0, 12, 13) | ||
| ] | ||
|
|
||
| for pyfile in data.itervalues(): | ||
| if pyfile["status"] not in (0, 12, 13): | ||
| urls.append((pyfile["url"], pyfile["plugin"])) |
There was a problem hiding this comment.
Function FileHandler.addLinks.deleteLink.updateLink.restartFile.checkPackageFinished.reCheckPackage refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension)
| data = {} | ||
| for r in self.c: | ||
| data[r[0]] = { | ||
| return {r[0]: { |
There was a problem hiding this comment.
Function FileHandler.addLinks.deleteLink.updateLink.restartFile.checkPackageFinished.FileMethods._nextFileOrder.addLink.addLinks.getAllLinks refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| data = {} | ||
| for r in self.c: | ||
| data[r[0]] = { | ||
| return {r[0]: { |
There was a problem hiding this comment.
Function FileHandler.addLinks.deleteLink.updateLink.restartFile.checkPackageFinished.FileMethods._nextFileOrder.addLink.addLinks.getPackageData refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
| for r in self.c: | ||
| ids.append(int(r[0])) | ||
| return ids | ||
| return [int(r[0]) for r in self.c] |
There was a problem hiding this comment.
Function FileHandler.addLinks.deleteLink.updateLink.restartFile.checkPackageFinished.FileMethods._nextFileOrder.addLink.addLinks.updateLinkInfo refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨
Here's your pull request refactoring your most popular Python repo.
If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.
Review changes via command line
To manually merge these changes, make sure you're on the
stablebranch, then run: