From 760b9b4db4b6f9ae15f2125c53f2658835e2ceea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Fri, 17 Jan 2020 23:51:07 +0100 Subject: [PATCH] add remove_file() and remove_directory() helpers these functions call os.unlink() or os.rmdir() while catching and suppressing potential OSErrors --- gallery_dl/downloader/http.py | 10 +++------- gallery_dl/postprocessor/zip.py | 21 +++++++-------------- gallery_dl/util.py | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/gallery_dl/downloader/http.py b/gallery_dl/downloader/http.py index fab96ba2..9cd2aa68 100644 --- a/gallery_dl/downloader/http.py +++ b/gallery_dl/downloader/http.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2014-2019 Mike Fährmann +# Copyright 2014-2020 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -8,12 +8,11 @@ """Downloader module for http:// and https:// URLs""" -import os import time import mimetypes from requests.exceptions import RequestException, ConnectionError, Timeout from .common import DownloaderBase -from .. import text +from .. import text, util from ssl import SSLError try: @@ -57,10 +56,7 @@ class HttpDownloader(DownloaderBase): finally: # remove file from incomplete downloads if self.downloading and not self.part: - try: - os.unlink(pathfmt.temppath) - except (OSError, AttributeError): - pass + util.remove_file(pathfmt.temppath) def _download_impl(self, url, pathfmt): response = None diff --git a/gallery_dl/postprocessor/zip.py b/gallery_dl/postprocessor/zip.py index 42f76082..a43c43a3 100644 --- a/gallery_dl/postprocessor/zip.py +++ b/gallery_dl/postprocessor/zip.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2018-2019 Mike Fährmann +# Copyright 2018-2020 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -9,17 +9,17 @@ """Store files in ZIP archives""" from .common import PostProcessor +from .. import util import zipfile -import os class ZipPP(PostProcessor): COMPRESSION_ALGORITHMS = { "store": zipfile.ZIP_STORED, - "zip": zipfile.ZIP_DEFLATED, + "zip" : zipfile.ZIP_DEFLATED, "bzip2": zipfile.ZIP_BZIP2, - "lzma": zipfile.ZIP_LZMA, + "lzma" : zipfile.ZIP_LZMA, } def __init__(self, pathfmt, options): @@ -64,18 +64,11 @@ class ZipPP(PostProcessor): self.zfile.close() if self.delete: - try: - # remove target directory - os.rmdir(self.path) - except OSError: - pass + util.remove_directory(self.path) if self.zfile and not self.zfile.NameToInfo: - try: - # delete empty zip archive - os.unlink(self.zfile.filename) - except OSError: - pass + # remove empty zip archive + util.remove_file(self.zfile.filename) __postprocessor__ = ZipPP diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 994ce0f1..13bf80ed 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -121,6 +121,20 @@ def expand_path(path): return os.path.expandvars(os.path.expanduser(path)) +def remove_file(path): + try: + os.unlink(path) + except OSError: + pass + + +def remove_directory(path): + try: + os.rmdir(path) + except OSError: + pass + + def code_to_language(code, default=None): """Map an ISO 639-1 language code to its actual name""" return CODES.get((code or "").lower(), default)