diff options
author | Walter Purcaro <vuolter@gmail.com> | 2014-11-09 03:08:19 +0100 |
---|---|---|
committer | Walter Purcaro <vuolter@gmail.com> | 2014-11-09 03:08:19 +0100 |
commit | bd8259220ab4d56ab419b7b32045b08cc9b0a7c8 (patch) | |
tree | 92f1c69d4280f8f57021083dbf878e62033eb502 /module/plugins/hooks/HotFolder.py | |
parent | Cookie support for getURL method (diff) | |
download | pyload-bd8259220ab4d56ab419b7b32045b08cc9b0a7c8.tar.xz |
Use with statement instead open method when accessing fod + handle i/o error
Diffstat (limited to 'module/plugins/hooks/HotFolder.py')
-rw-r--r-- | module/plugins/hooks/HotFolder.py | 67 |
1 files changed, 33 insertions, 34 deletions
diff --git a/module/plugins/hooks/HotFolder.py b/module/plugins/hooks/HotFolder.py index d7e8093b3..5e9dd9547 100644 --- a/module/plugins/hooks/HotFolder.py +++ b/module/plugins/hooks/HotFolder.py @@ -7,6 +7,7 @@ from os.path import exists, isfile, join from shutil import move from module.plugins.Hook import Hook +from module.utils import fs_encode, save_join class HotFolder(Hook): @@ -29,37 +30,35 @@ class HotFolder(Hook): def periodical(self): - if not exists(join(self.getConfig("folder"), "finished")): - makedirs(join(self.getConfig("folder"), "finished")) - - if self.getConfig("watch_file"): - - if not exists(self.getConfig("file")): - f = open(self.getConfig("file"), "wb") - f.close() - - f = open(self.getConfig("file"), "rb") - content = f.read().strip() - f.close() - f = open(self.getConfig("file"), "wb") - f.close() - if content: - name = "%s_%s.txt" % (self.getConfig("file"), time.strftime("%H-%M-%S_%d%b%Y")) - - f = open(join(self.getConfig("folder"), "finished", name), "wb") - f.write(content) - f.close() - - self.core.api.addPackage(f.name, [f.name], 1) - - for f in listdir(self.getConfig("folder")): - path = join(self.getConfig("folder"), f) - - if not isfile(path) or f.endswith("~") or f.startswith("#") or f.startswith("."): - continue - - newpath = join(self.getConfig("folder"), "finished", f if self.getConfig("keep") else "tmp_" + f) - move(path, newpath) - - self.logInfo(_("Added %s from HotFolder") % f) - self.core.api.addPackage(f, [newpath], 1) + folder = fs_encode(self.getConfig("folder")) + + try: + if not exists(join(folder, "finished")): + makedirs(join(folder, "finished")) + + if self.getConfig("watch_file"): + with open(fs_encode(self.getConfig("file")), "a+") as f: + content = f.read().strip() + + if content: + name = "%s_%s.txt" % (self.getConfig("file"), time.strftime("%H-%M-%S_%d%b%Y")) + + with open(save_join(folder, "finished", name), "wb") as f: + f.write(content) + + self.core.api.addPackage(f.name, [f.name], 1) + + for f in listdir(folder): + path = join(folder, f) + + if not isfile(path) or f.endswith("~") or f.startswith("#") or f.startswith("."): + continue + + newpath = join(folder, "finished", f if self.getConfig("keep") else "tmp_" + f) + move(path, newpath) + + self.logInfo(_("Added %s from HotFolder") % f) + self.core.api.addPackage(f, [newpath], 1) + + except IOError, e: + self.logError(str(e)) |