-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: stable
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
def _convertConfigFormat(self, c): | ||
sections = {} | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
if offset >= len(lines): | ||
return [] | ||
return lines[offset:] | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
folder = folder.replace("http://", "").replace(":", "").replace("/", "_").replace("\\", "_") | ||
|
||
pid = self.core.files.addPackage(name, folder, dest) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return self.checkOnlineStatus(urls + [th.name]) | ||
|
||
@permission(PERMS.ADD) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
@permission(PERMS.ADD) | ||
def generatePackages(self, links): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@permission(PERMS.ADD) | ||
def generateAndAddPackages(self, links, dest=Destination.Queue): | ||
|
@@ -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()], | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@permission(PERMS.LIST) | ||
def getPackageInfo(self, pid): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@permission(PERMS.LIST) | ||
def getFileData(self, fid): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@permission(PERMS.DELETE) | ||
def deleteFiles(self, fids): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.addPackage(th.name, [th.name], Destination.Queue) | ||
|
||
@permission(PERMS.MODIFY) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
pack["order"] += 1 | ||
order[pack["order"]] = pack["id"] | ||
return order | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
pyfile["order"] += 1 | ||
order[pyfile["order"]] = pyfile["id"] | ||
return order | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@permission(PERMS.STATUS) | ||
def getCaptchaTask(self, exclusive=False): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
else: | ||
return CaptchaTask(-1) | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def checkAuth(self, username, password, remoteip=None): | ||
"""Check authentication and returns details | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
@permission(PERMS.STATUS) | ||
def hasService(self, plugin, func): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def isTextual(self): | ||
""" returns if text is written on the captcha """ | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def timedOut(self): | ||
return time() > self.waitUntil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return value | ||
elif typ in ("str", "file", "folder"): | ||
try: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
plugin = self.pluginMap[plugin] | ||
f = getattr(plugin, func) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def activateHook(self, plugin): | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.type = itype | ||
self.id = iid | ||
self.destination = destination | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.type = itype | ||
self.id = iid | ||
self.destination = destination | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.type = itype | ||
self.id = iid | ||
self.after = after | ||
|
@@ -105,7 +105,7 @@ def toList(self): | |
|
||
class ReloadAllEvent(): | ||
def __init__(self, destination): | ||
assert destination == "queue" or destination == "collector" | ||
assert destination in ["queue", "collector"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.destination = destination | ||
|
||
def toList(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.progress = value | ||
self.notifyChange() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
except IndexError: | ||
return None, None | ||
finally: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.createThread() | ||
|
||
|
||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
try: | ||
sv = choice(services) | ||
ip = getURL(sv[0]) | ||
|
There was a problem hiding this comment.
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-immediately-returned-variable
)