Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery Starbot ⭐ refactored mfhbam/pyload #1

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 54 additions & 53 deletions module/Api.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ def __init__(self, core):
self.core = core

def _convertPyFile(self, p):
f = FileData(p["id"], p["url"], p["name"], p["plugin"], p["size"],
return FileData(p["id"], p["url"], p["name"], p["plugin"], p["size"],
p["format_size"], p["status"], p["statusmsg"],
p["package"], p["error"], p["order"])
return f
Comment on lines -99 to -102
Copy link
Author

Choose a reason for hiding this comment

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

Function Api._convertPyFile refactored with the following changes:

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


def _convertConfigFormat(self, c):
sections = {}
Expand Down Expand Up @@ -259,9 +258,8 @@ def getLog(self, offset=0):
"""
filename = join(self.core.config['log']['log_folder'], 'log.txt')
try:
fh = open(filename, "r")
lines = fh.readlines()
fh.close()
with open(filename, "r") as fh:
lines = fh.readlines()
Copy link
Author

Choose a reason for hiding this comment

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

Function Api.getLog refactored with the following changes:

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

if offset >= len(lines):
return []
return lines[offset:]
Expand Down Expand Up @@ -316,11 +314,7 @@ def addPackage(self, name, links, dest=Destination.Queue):
:param dest: `Destination`
:return: package id of the new package
"""
if self.core.config['general']['folder_per_package']:
folder = name
else:
folder = ""

folder = name if self.core.config['general']['folder_per_package'] else ""
Comment on lines -319 to +317
Copy link
Author

Choose a reason for hiding this comment

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

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

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

folder = folder.replace("http://", "").replace(":", "").replace("/", "_").replace("\\", "_")

pid = self.core.files.addPackage(name, folder, dest)
Expand Down Expand Up @@ -402,10 +396,8 @@ def checkOnlineStatusContainer(self, urls, container, data):
:param data: file content
:return: online check
"""
th = open(join(self.core.config["general"]["download_folder"], "tmp_" + container), "wb")
th.write(str(data))
th.close()

with open(join(self.core.config["general"]["download_folder"], "tmp_" + container), "wb") as th:
th.write(str(data))
Comment on lines -405 to +400
Copy link
Author

Choose a reason for hiding this comment

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

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

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

return self.checkOnlineStatus(urls + [th.name])

@permission(PERMS.ADD)
Expand All @@ -417,12 +409,12 @@ def pollResults(self, rid):
"""
result = self.core.threadManager.getInfoResult(rid)

if "ALL_INFO_FETCHED" in result:
del result["ALL_INFO_FETCHED"]
return OnlineCheck(-1, result)
else:
if "ALL_INFO_FETCHED" not in result:
return OnlineCheck(rid, result)

del result["ALL_INFO_FETCHED"]
return OnlineCheck(-1, result)
Comment on lines -420 to +416
Copy link
Author

Choose a reason for hiding this comment

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

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

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



@permission(PERMS.ADD)
def generatePackages(self, links):
Expand All @@ -431,8 +423,7 @@ def generatePackages(self, links):
:param links: list of urls
:return: package names mapped to urls
"""
result = parseNames((x, x) for x in links)
return result
return parseNames((x, x) for x in links)
Comment on lines -434 to +426
Copy link
Author

Choose a reason for hiding this comment

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

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

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


@permission(PERMS.ADD)
def generateAndAddPackages(self, links, dest=Destination.Queue):
Expand Down Expand Up @@ -470,11 +461,16 @@ def getPackageData(self, pid):
if not data:
raise PackageDoesNotExists(pid)

pdata = PackageData(data["id"], data["name"], data["folder"], data["site"], data["password"],
data["queue"], data["order"],
links=[self._convertPyFile(x) for x in data["links"].itervalues()])

return pdata
return PackageData(
data["id"],
data["name"],
data["folder"],
data["site"],
data["password"],
data["queue"],
data["order"],
links=[self._convertPyFile(x) for x in data["links"].itervalues()],
)
Copy link
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)


@permission(PERMS.LIST)
def getPackageInfo(self, pid):
Expand All @@ -484,15 +480,20 @@ def getPackageInfo(self, pid):
:return: `PackageData` with .fid attribute
"""
data = self.core.files.getPackageData(int(pid))

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"]],
)
Comment on lines -487 to +496
Copy link
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)


@permission(PERMS.LIST)
def getFileData(self, fid):
Expand All @@ -505,8 +506,7 @@ def getFileData(self, fid):
if not info:
raise FileDoesNotExists(fid)

fdata = self._convertPyFile(info.values()[0])
return fdata
return self._convertPyFile(info.values()[0])
Comment on lines -508 to +509
Copy link
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)


@permission(PERMS.DELETE)
def deleteFiles(self, fids):
Expand Down Expand Up @@ -695,10 +695,8 @@ def uploadContainer(self, filename, data):
:param filename: filename, extension is important so it can correctly decrypted
:param data: file content
"""
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))
Comment on lines -698 to +699
Copy link
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)

self.addPackage(th.name, [th.name], Destination.Queue)

@permission(PERMS.MODIFY)
Expand Down Expand Up @@ -762,7 +760,7 @@ def getPackageOrder(self, destination):

for pid in packs:
pack = self.core.files.getPackageData(int(pid))
while pack["order"] in order.keys(): #just in case
while pack["order"] in order: #just in case
Comment on lines -765 to +763
Copy link
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)

pack["order"] += 1
order[pack["order"]] = pack["id"]
return order
Expand All @@ -777,7 +775,7 @@ def getFileOrder(self, pid):
rawData = self.core.files.getPackageData(int(pid))
order = {}
for id, pyfile in rawData["links"].iteritems():
while pyfile["order"] in order.keys(): #just in case
while pyfile["order"] in order: #just in case
Comment on lines -780 to +778
Copy link
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)

pyfile["order"] += 1
order[pyfile["order"]] = pyfile["id"]
return order
Expand All @@ -791,7 +789,7 @@ def isCaptchaWaiting(self):
"""
self.core.lastClientConnected = time()
task = self.core.captchaManager.getTask()
return not task is None
return task is not None
Comment on lines -794 to +792
Copy link
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)


@permission(PERMS.STATUS)
def getCaptchaTask(self, exclusive=False):
Expand All @@ -805,8 +803,7 @@ def getCaptchaTask(self, exclusive=False):
if task:
task.setWatingForUser(exclusive=exclusive)
data, type, result = task.getCaptcha()
t = CaptchaTask(int(task.id), standard_b64encode(data), type, result)
return t
return CaptchaTask(int(task.id), standard_b64encode(data), type, result)
Comment on lines -808 to +806
Copy link
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)

else:
return CaptchaTask(-1)

Expand Down Expand Up @@ -911,7 +908,7 @@ def login(self, username, password, remoteip=None):
:param remoteip: Omit this argument, its only used internal
:return: bool indicating login was successful
"""
return True if self.checkAuth(username, password, remoteip) else False
return bool(self.checkAuth(username, password, remoteip))
Comment on lines -914 to +911
Copy link
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)


def checkAuth(self, username, password, remoteip=None):
"""Check authentication and returns details
Expand Down Expand Up @@ -955,23 +952,27 @@ def getUserData(self, username, password):

def getAllUserData(self):
"""returns all known user and info"""
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()
}
Comment on lines -958 to +964
Copy link
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)


@permission(PERMS.STATUS)
def getServices(self):
""" A dict of available services, these can be defined by hook plugins.

:return: dict with this style: {"plugin": {"method": "description"}}
"""
data = {}
for plugin, funcs in self.core.hookManager.methods.iteritems():
data[plugin] = funcs

return data
return {
plugin: funcs
for plugin, funcs in self.core.hookManager.methods.iteritems()
}
Comment on lines -970 to +975
Copy link
Author

Choose a reason for hiding this comment

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

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

  • Convert for loop into dictionary comprehension (dict-comprehension)
  • Inline variable that is immediately returned (inline-immediately-returned-variable)


@permission(PERMS.STATUS)
def hasService(self, plugin, func):
Expand Down
10 changes: 2 additions & 8 deletions module/CaptchaManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ def setWaiting(self, sec):
self.status = "waiting"

def isWaiting(self):
if self.result or self.error or time() > self.waitUntil:
return False

return True
return not self.result and not self.error and time() <= self.waitUntil
Copy link
Author

Choose a reason for hiding this comment

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

Function CaptchaTask.isWaiting refactored with the following changes:

  • Simplify conditional into return statement (return-identity)


def isTextual(self):
""" returns if text is written on the captcha """
Expand All @@ -139,10 +136,7 @@ def isPositional(self):
return self.captchaResultType == 'positional'

def setWatingForUser(self, exclusive):
if exclusive:
self.status = "user"
else:
self.status = "shared-user"
self.status = "user" if exclusive else "shared-user"
Comment on lines -142 to +139
Copy link
Author

Choose a reason for hiding this comment

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

Function CaptchaTask.setWatingForUser refactored with the following changes:

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


def timedOut(self):
return time() > self.waitUntil
Expand Down
4 changes: 2 additions & 2 deletions module/ConfigParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ def cast(self, typ, value):
elif typ == "int":
return int(value)
elif typ == "bool":
return True if value.lower() in ("1", "true", "on", "an", "yes") else False
return value.lower() in ("1", "true", "on", "an", "yes")
elif typ == "time":
if not value: value = "0:00"
if not ":" in value: value += ":00"
if ":" not in value: value += ":00"
Comment on lines -249 to +252
Copy link
Author

Choose a reason for hiding this comment

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

Function ConfigParser.cast refactored with the following changes:

  • Simplify boolean if expression (boolean-if-exp-identity)
  • Simplify logical expression using De Morgan identities (de-morgan)

return value
elif typ in ("str", "file", "folder"):
try:
Expand Down
11 changes: 6 additions & 5 deletions module/HookManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def addRPC(self, plugin, func, doc):
def callRPC(self, plugin, func, args, parse):
if not args: args = tuple()
if parse:
args = tuple([literal_eval(x) for x in args])
args = tuple(literal_eval(x) for x in args)
Copy link
Author

Choose a reason for hiding this comment

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

Function HookManager.callRPC refactored with the following changes:

  • Replace unneeded comprehension with generator (comprehension-to-generator)


plugin = self.pluginMap[plugin]
f = getattr(plugin, func)
Expand Down Expand Up @@ -147,10 +147,11 @@ def createIndex(self):
self.plugins = plugins

def manageHooks(self, plugin, name, value):
if name == "activated" and value:
self.activateHook(plugin)
elif name == "activated" and not value:
self.deactivateHook(plugin)
if name == "activated":
if value:
self.activateHook(plugin)
else:
self.deactivateHook(plugin)
Comment on lines -150 to +154
Copy link
Author

Choose a reason for hiding this comment

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

Function HookManager.manageHooks refactored with the following changes:

  • Remove redundant conditional (remove-redundant-if)
  • Lift repeated conditional into its own if statement (lift-duplicated-conditional)


def activateHook(self, plugin):

Expand Down
14 changes: 7 additions & 7 deletions module/PullEvents.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def addEvent(self, event):

class UpdateEvent():
def __init__(self, itype, iid, destination):
assert itype == "pack" or itype == "file"
assert destination == "queue" or destination == "collector"
assert itype in ["pack", "file"]
assert destination in ["queue", "collector"]
Comment on lines -74 to +75
Copy link
Author

Choose a reason for hiding this comment

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

Function UpdateEvent.__init__ refactored with the following changes:

  • Replace multiple comparisons of same variable with in operator (merge-comparisons)

self.type = itype
self.id = iid
self.destination = destination
Expand All @@ -82,8 +82,8 @@ def toList(self):

class RemoveEvent():
def __init__(self, itype, iid, destination):
assert itype == "pack" or itype == "file"
assert destination == "queue" or destination == "collector"
assert itype in ["pack", "file"]
assert destination in ["queue", "collector"]
Comment on lines -85 to +86
Copy link
Author

Choose a reason for hiding this comment

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

Function RemoveEvent.__init__ refactored with the following changes:

  • Replace multiple comparisons of same variable with in operator (merge-comparisons)

self.type = itype
self.id = iid
self.destination = destination
Expand All @@ -93,8 +93,8 @@ def toList(self):

class InsertEvent():
def __init__(self, itype, iid, after, destination):
assert itype == "pack" or itype == "file"
assert destination == "queue" or destination == "collector"
assert itype in ["pack", "file"]
assert destination in ["queue", "collector"]
Comment on lines -96 to +97
Copy link
Author

Choose a reason for hiding this comment

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

Function InsertEvent.__init__ refactored with the following changes:

  • Replace multiple comparisons of same variable with in operator (merge-comparisons)

self.type = itype
self.id = iid
self.after = after
Expand All @@ -105,7 +105,7 @@ def toList(self):

class ReloadAllEvent():
def __init__(self, destination):
assert destination == "queue" or destination == "collector"
assert destination in ["queue", "collector"]
Copy link
Author

Choose a reason for hiding this comment

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

Function ReloadAllEvent.__init__ refactored with the following changes:

  • Replace multiple comparisons of same variable with in operator (merge-comparisons)

self.destination = destination

def toList(self):
Expand Down
2 changes: 1 addition & 1 deletion module/PyFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,6 @@ def notifyChange(self):
self.m.core.pullManager.addEvent(e)

def setProgress(self, value):
if not value == self.progress:
if value != self.progress:
Copy link
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)

self.progress = value
self.notifyChange()
3 changes: 1 addition & 2 deletions module/Scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ def get(self):
""" return element or None """
self.lock.acquire()
try:
el = heappop(self.queue)
return el
return heappop(self.queue)
Copy link
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)

except IndexError:
return None, None
finally:
Expand Down
4 changes: 2 additions & 2 deletions module/ThreadManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(self, core):

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
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)

self.createThread()


Expand Down Expand Up @@ -215,7 +215,7 @@ def getIP(self):
("http://checkip.dyndns.org/",".*Current IP Address: (\S+)</body>.*")]

ip = ""
for i in range(10):
for _ in range(10):
Copy link
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)

try:
sv = choice(services)
ip = getURL(sv[0])
Expand Down
Loading