add remove_file() and remove_directory() helpers

these functions call os.unlink() or os.rmdir()
while catching and suppressing potential OSErrors
pull/586/head
Mike Fährmann 5 years ago
parent b2d542ad40
commit 760b9b4db4
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

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

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

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

Loading…
Cancel
Save