From 3dcc82f41670b68d3c3ed2224cabe30989a66f40 Mon Sep 17 00:00:00 2001 From: Prodesire Date: Fri, 1 Dec 2017 08:09:39 +0800 Subject: [PATCH 1/3] modify file.makedirs logical --- pydu/file.py | 4 ++-- tests/test_file.py | 15 +++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pydu/file.py b/pydu/file.py index d169d97..699537d 100644 --- a/pydu/file.py +++ b/pydu/file.py @@ -5,12 +5,12 @@ import shutil # todo tests and docs def makedirs(path, mode=0o755, ignore_errors=False, exist_ok=True): + if exist_ok and os.path.exists(path): + return try: os.makedirs(path, mode) except Exception: if not ignore_errors: - if not exist_ok: - raise OSError('{} is exist'.format(path)) raise OSError('Create dir: {} error.') diff --git a/tests/test_file.py b/tests/test_file.py index 3f33417..a4260a7 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -4,16 +4,19 @@ import pytest class Testmakedirs(): - def test_makedirs(self,tmpdir): + def test_makedirs(self, tmpdir): path = str(tmpdir.join('test')) makedirs(path) assert os.path.exists(path) - def test_makedirs_with_exists_path(self,tmpdir): + def test_makedirs_with_exists_path(self, tmpdir): path = str(tmpdir.join('test')) makedirs(path) - with pytest.raises(Exception) as e_info: - makedirs(path, exist_ok=True) + + makedirs(path, exist_ok=True) + + with pytest.raises(Exception): + makedirs(path, exist_ok=False) def test_makedirs_with_ignore_error(self, tmpdir): path = str(tmpdir.join('test')) @@ -23,8 +26,8 @@ class Testmakedirs(): def test_makedirs_without_ignore_error(self, tmpdir): path = str(tmpdir.join('test')) makedirs(path) - with pytest.raises(Exception) as e_info: - makedirs(path, ignore_errors=False) + with pytest.raises(Exception): + makedirs(path, ignore_errors=False, exist_ok=False) def test_makedirs_with_mutl_dirs(self, tmpdir): path = str(tmpdir.join('test/test')) From b8cab1e944fe538dd6f383a442737be1a380f6e3 Mon Sep 17 00:00:00 2001 From: Prodesire Date: Fri, 1 Dec 2017 08:13:33 +0800 Subject: [PATCH 2/3] file.makedirs's exist_ok default to False --- pydu/file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydu/file.py b/pydu/file.py index 699537d..4bae1e8 100644 --- a/pydu/file.py +++ b/pydu/file.py @@ -4,7 +4,7 @@ import shutil # todo tests and docs -def makedirs(path, mode=0o755, ignore_errors=False, exist_ok=True): +def makedirs(path, mode=0o755, ignore_errors=False, exist_ok=False): if exist_ok and os.path.exists(path): return try: From fb429508a36b9e9e3e54926c2e6963d5dd42e6cb Mon Sep 17 00:00:00 2001 From: Prodesire Date: Fri, 1 Dec 2017 08:30:50 +0800 Subject: [PATCH 3/3] add docstring for makedirs, remove, removes and open_files --- pydu/file.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pydu/file.py b/pydu/file.py index 4bae1e8..4c8bf9e 100644 --- a/pydu/file.py +++ b/pydu/file.py @@ -5,6 +5,12 @@ import shutil # todo tests and docs def makedirs(path, mode=0o755, ignore_errors=False, exist_ok=False): + """ + Create a leaf directory and all intermediate ones. + + Based on os.makedirs, but also supports ignore_errors which will + ignore all errors raised by os.makedirs. + """ if exist_ok and os.path.exists(path): return try: @@ -15,6 +21,16 @@ def makedirs(path, mode=0o755, ignore_errors=False, exist_ok=False): def remove(path, ignore_errors=False, onerror=None): + """ + Remove a file or directory. + + If ignore_errors is set, errors are ignored; otherwise, if onerror + is set, it is called to handle the error with arguments (func, + path, exc_info) where func is platform and implementation dependent; + path is the argument to that function that caused it to fail; and + exc_info is a tuple returned by sys.exc_info(). If ignore_errors + is False and onerror is None, an exception is raised. + """ if ignore_errors: def onerror(*args): pass @@ -32,11 +48,27 @@ def remove(path, ignore_errors=False, onerror=None): def removes(paths, ignore_errors=False, onerror=None): + """ + Remove a list of file and/or directory. + + If ignore_errors is set, errors are ignored; otherwise, if onerror + is set, it is called to handle the error with arguments (func, + path, exc_info) where func is platform and implementation dependent; + path is the argument to that function that caused it to fail; and + exc_info is a tuple returned by sys.exc_info(). If ignore_errors + is False and onerror is None, an exception is raised. + """ for path in paths: remove(path, ignore_errors=ignore_errors, onerror=onerror) def open_file(path, mode='wb+', buffer_size=-1, ignore_errors=False): + """ + Open a file, defualt mode 'wb+'. + + If path not exists, it will be created automatically. + If ignore_errors is set, errors are ignored. + """ f = None try: if path and not os.path.isdir(path):