diff options
author | Walter Purcaro <vuolter@users.noreply.github.com> | 2015-04-19 16:37:00 +0200 |
---|---|---|
committer | Walter Purcaro <vuolter@users.noreply.github.com> | 2015-04-19 16:37:00 +0200 |
commit | f162ae0de0f71391c56957389cc3c8babc8022e1 (patch) | |
tree | c85c8117da6e20fe49f91c933d8c7e57eb808cb8 | |
parent | Merge pull request #8 from ardi69/0.4.10 (diff) | |
download | pyload-f162ae0de0f71391c56957389cc3c8babc8022e1.tar.xz |
Use with statement
-rw-r--r-- | locale/pavement.py | 28 | ||||
-rwxr-xr-x | pyload/Core.py | 40 | ||||
-rw-r--r-- | pyload/api/__init__.py | 17 | ||||
-rw-r--r-- | pyload/cli/Cli.py | 5 | ||||
-rw-r--r-- | pyload/config/Parser.py | 27 | ||||
-rw-r--r-- | pyload/config/default.conf | 2 | ||||
-rw-r--r-- | pyload/database/Backend.py | 34 | ||||
-rw-r--r-- | pyload/manager/thread/Plugin.py | 7 | ||||
-rw-r--r-- | pyload/network/HTTPDownload.py | 41 | ||||
-rw-r--r-- | pyload/remote/socketbackend/create_ttypes.py | 9 | ||||
-rw-r--r-- | pyload/webui/app/cnl.py | 8 | ||||
-rw-r--r-- | pyload/webui/app/json.py | 7 | ||||
-rw-r--r-- | tests/APIExerciser.py | 54 |
13 files changed, 135 insertions, 144 deletions
diff --git a/locale/pavement.py b/locale/pavement.py index 06a4f9775..cb2c6c01e 100644 --- a/locale/pavement.py +++ b/locale/pavement.py @@ -270,11 +270,11 @@ def upload_translations(options): shutil.copy(f, tmp / 'pyLoad') config = tmp / 'crowdin.yaml' - content = open(config, 'rb').read() + with open(config, 'rb') as f: + content = f.read() content = content.format(key=options.key, tmp=tmp) - f = open(config, 'wb') - f.write(content) - f.close() + with open(config, 'wb') as f: + f.write(content) call(['crowdin-cli', '-c', config, 'upload', 'source']) @@ -300,11 +300,11 @@ def download_translations(options): shutil.copy(f, tmp / 'pyLoad') config = tmp / 'crowdin.yaml' - content = open(config, 'rb').read() + with open(config, 'rb') as f: + content = f.read() content = content.format(key=options.key, tmp=tmp) - f = open(config, 'wb') - f.write(content) - f.close() + with open(config, 'wb') as f: + f.write(content) call(['crowdin-cli', '-c', config, 'download']) @@ -395,14 +395,12 @@ def walk_trans(path, EXCLUDE, endings=[".py"]): def makepot(domain, p, excludes=[], includes="", endings=[".py"], xxargs=[]): print "Generate %s.pot" % domain - f = open("includes.txt", "wb") - if includes: - f.write(includes) + with open("includes.txt", "wb") as f: + if includes: + f.write(includes) - if p: - f.write(walk_trans(path(p), excludes, endings)) - - f.close() + if p: + f.write(walk_trans(path(p), excludes, endings)) call(["xgettext", "--files-from=includes.txt", "--default-domain=%s" % domain] + xargs + xxargs) diff --git a/pyload/Core.py b/pyload/Core.py index f723d8366..481026a68 100755 --- a/pyload/Core.py +++ b/pyload/Core.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- -# @author: RaNaN, mkaay, sebnapi, spoob +# @author: RaNaN, mkaay, sebnapi, spoob, vuolter # @version: v0.4.10 -CURRENT_VERSION = '0.4.10' +from __future__ import with_statement +import pyload import __builtin__ from getopt import getopt, GetoptError @@ -41,11 +42,10 @@ from codecs import getwriter enc = get_console_encoding(sys.stdout.encoding) sys.stdout = getwriter(enc)(sys.stdout, errors="replace") + # TODO List # - configurable auth system ldap/mysql # - cron job like sheduler - - class Core(object): """pyLoad Core, one tool to rule them all... (the filehosters) :D""" @@ -68,7 +68,7 @@ class Core(object): for option, argument in options: if option in ("-v", "--version"): - print "pyLoad", CURRENT_VERSION + print "pyLoad", pyload.__version__ exit() elif option in ("-p", "--pidfile"): self.pidfile = argument @@ -127,7 +127,7 @@ class Core(object): def print_help(self): print - print "pyLoad v%s 2008-2015 the pyLoad Team" % CURRENT_VERSION + print "pyLoad v%s 2008-2015 the pyLoad Team" % pyload.__version__ print if sys.argv[0].endswith(".py"): print "Usage: python pyload.py [options]" @@ -171,9 +171,8 @@ class Core(object): def writePidFile(self): self.deletePidFile() pid = os.getpid() - f = open(self.pidfile, "wb") - f.write(str(pid)) - f.close() + with open(self.pidfile, "wb") as f: + f.write(str(pid)) def deletePidFile(self): @@ -185,9 +184,8 @@ class Core(object): def checkPidFile(self): """ return pid as int or 0""" if os.path.isfile(self.pidfile): - f = open(self.pidfile, "rb") - pid = f.read().strip() - f.close() + with open(self.pidfile, "rb") as f: + pid = f.read().strip() if pid: pid = int(pid) return pid @@ -253,7 +251,7 @@ class Core(object): def start(self, rpc=True, web=True): """ starts the fun :D """ - self.version = CURRENT_VERSION + self.version = pyload.__version__ if not exists("pyload.conf"): from pyload.config.Setup import SetupAssistant as Setup @@ -330,7 +328,7 @@ class Core(object): self.do_restart = False self.shuttedDown = False - self.log.info(_("Starting") + " pyLoad %s" % CURRENT_VERSION) + self.log.info(_("Starting") + " pyLoad %s" % pyload.__version__) self.log.info(_("Using home directory: %s") % getcwd()) self.writePidFile() @@ -409,17 +407,15 @@ class Core(object): link_file = join(pypath, "links.txt") if exists(link_file): - f = open(link_file, "rb") - if f.read().strip(): - self.api.addPackage("links.txt", [link_file], 1) - f.close() + with open(link_file, "rb") as f: + if f.read().strip(): + self.api.addPackage("links.txt", [link_file], 1) link_file = "links.txt" if exists(link_file): - f = open(link_file, "rb") - if f.read().strip(): - self.api.addPackage("links.txt", [link_file], 1) - f.close() + with open(link_file, "rb") as f: + if f.read().strip(): + self.api.addPackage("links.txt", [link_file], 1) #self.scheduler.addJob(0, self.accountManager.getAccountInfos) self.log.info(_("Activating Accounts...")) diff --git a/pyload/api/__init__.py b/pyload/api/__init__.py index b5c1dfbf4..ab717525d 100644 --- a/pyload/api/__init__.py +++ b/pyload/api/__init__.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- # @author: RaNaN +from __future__ import with_statement + from base64 import standard_b64encode from os.path import join from time import time @@ -265,9 +267,8 @@ class Api(Iface): """ filename = join(self.core.config.get("log", "log_folder"), 'log.txt') try: - fh = open(filename, "r") - lines = fh.readlines() - fh.close() + with open(filename, "r") as fh: + lines = fh.readlines() if offset >= len(lines): return [] return lines[offset:] @@ -409,9 +410,8 @@ class Api(Iface): :param data: file content :return: online check """ - th = open(join(self.core.config.get("general", "download_folder"), "tmp_" + container), "wb") - th.write(str(data)) - th.close() + with open(join(self.core.config.get("general", "download_folder"), "tmp_" + container), "wb") as th: + th.write(str(data)) return self.checkOnlineStatus(urls + [th.name]) @@ -707,9 +707,8 @@ class Api(Iface): :param filename: filename, extension is important so it can correctly decrypted :param data: file content """ - th = open(join(self.core.config.get("general", "download_folder"), "tmp_" + filename), "wb") - th.write(str(data)) - th.close() + with open(join(self.core.config.get("general", "download_folder"), "tmp_" + filename), "wb") as th: + th.write(str(data)) self.addPackage(th.name, [th.name], Destination.Queue) diff --git a/pyload/cli/Cli.py b/pyload/cli/Cli.py index 84725b625..a1f192bb3 100644 --- a/pyload/cli/Cli.py +++ b/pyload/cli/Cli.py @@ -321,9 +321,8 @@ class Cli(object): print _("File does not exists.") return - f = open(join(owd, path), "rb") - content = f.read() - f.close() + with open(join(owd, path), "rb") as f: + content = f.read() rid = self.client.checkOnlineStatusContainer([], basename(f.name), content).rid self.printOnlineCheck(self.client, rid) diff --git a/pyload/config/Parser.py b/pyload/config/Parser.py index b26af6202..a5be1444e 100644 --- a/pyload/config/Parser.py +++ b/pyload/config/Parser.py @@ -52,29 +52,26 @@ class ConfigParser(object): copy(join(pypath, "pyload", "config", "default.conf"), "pyload.conf") if not exists("plugin.conf"): - f = open("plugin.conf", "wb") - f.write("version: " + str(CONF_VERSION)) - f.close() + with open("plugin.conf", "wb") as f: + f.write("version: " + str(CONF_VERSION)) - f = open("pyload.conf", "rb") - v = f.readline() - f.close() + with open("pyload.conf", "rb") as f: + v = f.readline() v = v[v.find(":") + 1:].strip() if not v or int(v) < CONF_VERSION: copy(join(pypath, "pyload", "config", "default.conf"), "pyload.conf") print "Old version of config was replaced" - f = open("plugin.conf", "rb") - v = f.readline() - f.close() + with open("plugin.conf", "rb") as f: + v = f.readline() v = v[v.find(":") + 1:].strip() if not v or int(v) < CONF_VERSION: - f = open("plugin.conf", "wb") - f.write("version: " + str(CONF_VERSION)) - f.close() + with open("plugin.conf", "wb") as f: + f.write("version: " + str(CONF_VERSION)) print "Old version of plugin-config replaced" + except Exception: if n >= 3: raise @@ -104,9 +101,8 @@ class ConfigParser(object): def parseConfig(self, config): """parses a given configfile""" - f = open(config) - - config = f.read() + with open(config) as f: + config = f.read() config = config.splitlines()[1:] @@ -181,7 +177,6 @@ class ConfigParser(object): print "Config Warning" print_exc() - f.close() return conf diff --git a/pyload/config/default.conf b/pyload/config/default.conf index e07b92f68..b36ed6c9c 100644 --- a/pyload/config/default.conf +++ b/pyload/config/default.conf @@ -1,4 +1,4 @@ -version: 2 +version: 1 remote - "Remote": bool activated : "Activated" = True diff --git a/pyload/database/Backend.py b/pyload/database/Backend.py index b0e94711e..b105723cc 100644 --- a/pyload/database/Backend.py +++ b/pyload/database/Backend.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- # @author: RaNaN, mkaay +from __future__ import with_statement + from threading import Event, Thread from os import remove from os.path import exists @@ -167,26 +169,22 @@ class DatabaseBackend(Thread): def _checkVersion(self): """ check db version and delete it if needed""" if not exists("files.version"): - f = open("files.version", "wb") - f.write(str(DB_VERSION)) - f.close() + with open("files.version", "wb") as f: + f.write(str(DB_VERSION)) return - f = open("files.version", "rb") - v = int(f.read().strip()) - f.close() - if v < DB_VERSION: - if v < 2: - try: - self.manager.core.log.warning(_("Filedatabase was deleted due to incompatible version.")) - except Exception: - print "Filedatabase was deleted due to incompatible version." - remove("files.version") - move("files.db", "files.backup.db") - f = open("files.version", "wb") - f.write(str(DB_VERSION)) - f.close() - return v + with open("files.version", "wb+") as f: + v = int(f.read().strip()) + if v < DB_VERSION: + if v < 2: + try: + self.manager.core.log.warning(_("Filedatabase was deleted due to incompatible version.")) + except Exception: + print "Filedatabase was deleted due to incompatible version." + remove("files.version") + move("files.db", "files.backup.db") + f.write(str(DB_VERSION)) + return v def _convertDB(self, v): diff --git a/pyload/manager/thread/Plugin.py b/pyload/manager/thread/Plugin.py index 08a2664da..348f005a5 100644 --- a/pyload/manager/thread/Plugin.py +++ b/pyload/manager/thread/Plugin.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- # @author: RaNaN +from __future__ import with_statement + from Queue import Queue from threading import Thread from os import listdir, stat @@ -64,9 +66,8 @@ class PluginThread(Thread): self.m.log.debug("Error creating zip file: %s" % e) dump_name = dump_name.replace(".zip", ".txt") - f = open(dump_name, "wb") - f.write(dump) - f.close() + with open(dump_name, "wb") as f: + f.write(dump) self.m.core.log.info("Debug Report written to %s" % dump_name) diff --git a/pyload/network/HTTPDownload.py b/pyload/network/HTTPDownload.py index 13666195a..77f2ea657 100644 --- a/pyload/network/HTTPDownload.py +++ b/pyload/network/HTTPDownload.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- # @author: RaNaN +from __future__ import with_statement + from os import remove, fsync from os.path import dirname from time import sleep, time @@ -81,27 +83,24 @@ class HTTPDownload(object): init = fs_encode(self.info.getChunkName(0)) #: initial chunk name if self.info.getCount() > 1: - fo = open(init, "rb+") #: first chunkfile - for i in range(1, self.info.getCount()): - #input file - fo.seek( - self.info.getChunkRange(i - 1)[1] + 1) #: seek to beginning of chunk, to get rid of overlapping chunks - fname = fs_encode("%s.chunk%d" % (self.filename, i)) - fi = open(fname, "rb") - buf = 32 * 1024 - while True: #: copy in chunks, consumes less memory - data = fi.read(buf) - if not data: - break - fo.write(data) - fi.close() - if fo.tell() < self.info.getChunkRange(i)[1]: - fo.close() - remove(init) - self.info.remove() #: there are probably invalid chunks - raise Exception("Downloaded content was smaller than expected. Try to reduce download connections.") - remove(fname) #: remove chunk - fo.close() + with open(init, "rb+") as fo: #: first chunkfile + for i in range(1, self.info.getCount()): + #input file + fo.seek( + self.info.getChunkRange(i - 1)[1] + 1) #: seek to beginning of chunk, to get rid of overlapping chunks + fname = fs_encode("%s.chunk%d" % (self.filename, i)) + with open(fname, "rb") as fi: + buf = 32 * 1024 + while True: #: copy in chunks, consumes less memory + data = fi.read(buf) + if not data: + break + fo.write(data) + if fo.tell() < self.info.getChunkRange(i)[1]: + remove(init) + self.info.remove() #: there are probably invalid chunks + raise Exception("Downloaded content was smaller than expected. Try to reduce download connections.") + remove(fname) #: remove chunk if self.nameDisposition and self.disposition: self.filename = fs_join(dirname(self.filename), self.nameDisposition) diff --git a/pyload/remote/socketbackend/create_ttypes.py b/pyload/remote/socketbackend/create_ttypes.py index 9b001f1bf..26dd5d06b 100644 --- a/pyload/remote/socketbackend/create_ttypes.py +++ b/pyload/remote/socketbackend/create_ttypes.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +from __future__ import with_statement + import inspect import os import platform @@ -30,10 +32,9 @@ def main(): enums.append(klass) - f = open(os.path.join(pypath, "pyload", "api", "types.py"), "wb") - - f.write( - """# -*- coding: utf-8 -*- + with open(os.path.join(pypath, "pyload", "api", "types.py"), "wb") as f: + f.write( +"""# -*- coding: utf-8 -*- # Autogenerated by pyload # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING diff --git a/pyload/webui/app/cnl.py b/pyload/webui/app/cnl.py index 73087ad2d..b6cbd6b55 100644 --- a/pyload/webui/app/cnl.py +++ b/pyload/webui/app/cnl.py @@ -1,4 +1,7 @@ # -*- coding: utf-8 -*- + +from __future__ import with_statement + from os.path import join import re from urllib import unquote @@ -57,9 +60,8 @@ def addcrypted(): dlc = request.forms['crypted'].replace(" ", "+") dlc_path = join(DL_ROOT, package.replace("/", "").replace("\\", "").replace(":", "") + ".dlc") - dlc_file = open(dlc_path, "wb") - dlc_file.write(dlc) - dlc_file.close() + with open(dlc_path, "wb") as dlc_file: + dlc_file.write(dlc) try: PYLOAD.addPackage(package, [dlc_path], 0) diff --git a/pyload/webui/app/json.py b/pyload/webui/app/json.py index 12dce2484..d4af40dee 100644 --- a/pyload/webui/app/json.py +++ b/pyload/webui/app/json.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +from __future__ import with_statement + from os.path import join from traceback import print_exc from shutil import copyfileobj @@ -166,9 +168,8 @@ def add_package(): name = f.name fpath = join(PYLOAD.getConfigValue("general", "download_folder"), "tmp_" + f.filename) - destination = open(fpath, 'wb') - copyfileobj(f.file, destination) - destination.close() + with open(fpath, 'wb') as destination: + copyfileobj(f.file, destination) links.insert(0, fpath) except Exception: pass diff --git a/tests/APIExerciser.py b/tests/APIExerciser.py index d17f81ae2..d7300b1a5 100644 --- a/tests/APIExerciser.py +++ b/tests/APIExerciser.py @@ -1,6 +1,8 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from __future__ import with_statement + import string from threading import Thread from random import choice, sample, randint @@ -60,32 +62,32 @@ class APIExerciser(Thread): self.core.log.info("API Excerciser started %d" % self.id) - out = open("error.log", "ab") - # core errors are not logged of course - out.write("\n" + "Starting\n") - out.flush() - - while True: - try: - self.testAPI() - except Exception: - self.core.log.error("Excerciser %d throw an execption" % self.id) - print_exc() - out.write(format_exc() + 2 * "\n") - out.flush() - - if not self.count % 100: - self.core.log.info("Exerciser %d tested %d api calls" % (self.id, self.count)) - if not self.count % 1000: - out.flush() - - if not sumCalled % 1000: #: not thread safe - self.core.log.info("Exercisers tested %d api calls" % sumCalled) - persec = sumCalled / (time() - self.time) - self.core.log.info("Approx. %.2f calls per second." % persec) - self.core.log.info("Approx. %.2f ms per call." % (1000 / persec)) - self.core.log.info("Collected garbage: %d" % gc.collect()) - # sleep(random() / 500) + with open("error.log", "ab") as out: + # core errors are not logged of course + out.write("\n" + "Starting\n") + out.flush() + + while True: + try: + self.testAPI() + except Exception: + self.core.log.error("Excerciser %d throw an execption" % self.id) + print_exc() + out.write(format_exc() + 2 * "\n") + out.flush() + + if not self.count % 100: + self.core.log.info("Exerciser %d tested %d api calls" % (self.id, self.count)) + if not self.count % 1000: + out.flush() + + if not sumCalled % 1000: #: not thread safe + self.core.log.info("Exercisers tested %d api calls" % sumCalled) + persec = sumCalled / (time() - self.time) + self.core.log.info("Approx. %.2f calls per second." % persec) + self.core.log.info("Approx. %.2f ms per call." % (1000 / persec)) + self.core.log.info("Collected garbage: %d" % gc.collect()) + # sleep(random() / 500) def testAPI(self): |