Skip to content

Sourcery Starbot ⭐ refactored mfhbam/pyload#1

Open
SourceryAI wants to merge 1 commit into
mfhbam:stablefrom
SourceryAI:stable
Open

Sourcery Starbot ⭐ refactored mfhbam/pyload#1
SourceryAI wants to merge 1 commit into
mfhbam:stablefrom
SourceryAI:stable

Conversation

@SourceryAI
Copy link
Copy Markdown

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 stable branch, then run:

git fetch https://github.com/sourcery-ai-bot/pyload stable
git merge --ff-only FETCH_HEAD
git reset HEAD^

Copy link
Copy Markdown
Author

@SourceryAI SourceryAI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery timed out performing refactorings.

Due to GitHub API limits, only the first 60 comments can be shown.

Comment thread pyLoadCli.py
else:
enc = "utf8"

enc = "cp850" if os.name == "nt" else "utf8"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 37-41 refactored with the following changes:

  • Replace if statement with if expression (assign-if-exp)

Comment thread pyLoadCore.py
self.threadManager.pause = False
return False
elif not self.threadManager.pause:
else:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Core.__init__.toggle_pause refactored with the following changes:

  • Remove redundant conditional (remove-redundant-if)

Comment thread pyLoadCore.py
Comment on lines -188 to +189
f = open(self.pidfile, "wb")
f.write(str(pid))
f.close()
with open(self.pidfile, "wb") as f:
f.write(str(pid))
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Core.__init__.writePidFile refactored with the following changes:

  • Use with when opening file to ensure closure (ensure-file-closed)

Comment thread pyLoadCore.py
pid = f.read().strip()
f.close()
with open(self.pidfile, "rb") as f:
pid = f.read().strip()
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Core.__init__.checkPidFile refactored with the following changes:

  • Use with when opening file to ensure closure (ensure-file-closed)

Comment thread pyLoadGui.py
Comment on lines -322 to +334
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", "")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread module/Api.py
data["queue"],
data["order"],
links=[self._convertPyFile(x) for x in data["links"].itervalues()],
)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Api.statusDownloads.getPackageData refactored with the following changes:

  • Inline variable that is immediately returned (inline-immediately-returned-variable)

Comment thread module/Api.py
Comment on lines -487 to +496

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"]],
)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Api.statusDownloads.getPackageInfo refactored with the following changes:

  • Inline variable that is immediately returned (inline-immediately-returned-variable)

Comment thread module/Api.py
Comment on lines -508 to +509
fdata = self._convertPyFile(info.values()[0])
return fdata
return self._convertPyFile(info.values()[0])
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Api.statusDownloads.getFileData refactored with the following changes:

  • Inline variable that is immediately returned (inline-immediately-returned-variable)

Comment thread module/Api.py
Comment on lines -698 to +699
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))
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Api.statusDownloads.uploadContainer refactored with the following changes:

  • Use with when opening file to ensure closure (ensure-file-closed)

Comment thread module/Api.py
Comment on lines -765 to +763
while pack["order"] in order.keys(): #just in case
while pack["order"] in order: #just in case
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Api.statusDownloads.getPackageOrder refactored with the following changes:

  • Remove unnecessary call to keys() (remove-dict-keys)

Comment thread module/Api.py
Comment on lines -780 to +778
while pyfile["order"] in order.keys(): #just in case
while pyfile["order"] in order: #just in case
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Api.statusDownloads.getFileOrder refactored with the following changes:

  • Remove unnecessary call to keys() (remove-dict-keys)

Comment thread module/Api.py
Comment on lines -794 to +792
return not task is None
return task is not None
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Api.statusDownloads.isCaptchaWaiting refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

Comment thread module/Api.py
Comment on lines -808 to +806
t = CaptchaTask(int(task.id), standard_b64encode(data), type, result)
return t
return CaptchaTask(int(task.id), standard_b64encode(data), type, result)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Api.statusDownloads.getCaptchaTask refactored with the following changes:

  • Inline variable that is immediately returned (inline-immediately-returned-variable)

Comment thread module/Api.py
Comment on lines -914 to +911
return True if self.checkAuth(username, password, remoteip) else False
return bool(self.checkAuth(username, password, remoteip))
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Api.statusDownloads.login refactored with the following changes:

  • Simplify boolean if expression (boolean-if-exp-identity)

Comment thread module/Api.py
Comment on lines -958 to +964
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()
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread module/PyFile.py

def setProgress(self, value):
if not value == self.progress:
if value != self.progress:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PyFile.__init__.notifyChange.setProgress refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

Comment thread module/Scheduler.py
try:
el = heappop(self.queue)
return el
return heappop(self.queue)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PriorityQueue.get refactored with the following changes:

  • Inline variable that is immediately returned (inline-immediately-returned-variable)

Comment thread module/ThreadManager.py
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")):
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread module/ThreadManager.py

ip = ""
for i in range(10):
for _ in range(10):
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ThreadManager.getIP refactored with the following changes:

  • Replace unused for index with underscore (for-index-underscore)

Comment thread module/utils.py
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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function compare_time refactored with the following changes:

  • Remove redundant conditional (remove-redundant-if)

Comment thread module/utils.py
Comment on lines -136 to +138
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)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parseFileSize refactored with the following changes:

  • Swap if/else branches (swap-if-else-branches)
  • Remove unnecessary else after guard condition (remove-unnecessary-else)

Comment thread module/cli/ManageFiles.py
Comment on lines -185 to +203
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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)):
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function APIExerciser.__init__ refactored with the following changes:

  • Replace if statement with if expression (assign-if-exp)

Comment on lines -529 to +535
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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FileHandler.addLinks.deleteLink.updateLink.restartFile.checkPackageFinished refactored with the following changes:

  • Merge nested if conditions (merge-nested-ifs)

Comment on lines -540 to -544
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"]))
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function FileHandler.addLinks.deleteLink.updateLink.restartFile.checkPackageFinished.reCheckPackage refactored with the following changes:

  • Convert for loop into list comprehension (list-comprehension)

Comment on lines -662 to +665
data = {}
for r in self.c:
data[r[0]] = {
return {r[0]: {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment on lines -746 to +745
data = {}
for r in self.c:
data[r[0]] = {
return {r[0]: {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant