Skip to content

Commit

Permalink
Add makedirs function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Talmaj committed Nov 7, 2021
1 parent fe5c11e commit 4bb5f67
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions owncloud/owncloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,26 @@ def mkdir(self, path):
path += '/'
return self._make_dav_request('MKCOL', path)

def makedirs(self, path, exist_ok=False):
"""Creates recursively remote directories
:param path: path to the remote directory to create
:param exist_ok: if True, it does not throw an error if a folder already exist
:returns: True if the operation succeeded, False otherwise
:raises: HTTPResponseError in case an HTTP error status was returned
"""
try:
self.mkdir(path)
except HTTPResponseError as e:
if e.status_code == 409:
self.makedirs(os.path.dirname(path))
self.makedirs(path)
elif e.status_code == 405 and exist_ok:
pass
else:
raise e
return True

def delete(self, path):
"""Deletes a remote file or directory
Expand Down
11 changes: 11 additions & 0 deletions owncloud/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ def test_mkdir(self, file_name, content, subdir):
self.assertTrue(self.client.mkdir(self.test_root + subdir))
self.assertIsNotNone(self.client.file_info(self.test_root + subdir))

@data_provider(files_content)
def test_makedirs(self, file_name, content, subdir):
"""Test recursive subdirectory creation with existing folder"""
new_dir = os.path.join(self.test_root, subdir, subdir)
self.assertTrue(self.client.makedirs(new_dir))
self.assertIsNotNone(self.client.file_info(new_dir))

new_dir = os.path.join(new_dir, subdir)
self.assertTrue(self.client.makedirs(new_dir, exist_ok=True))
self.assertIsNotNone(self.client.file_info(new_dir))

@data_provider(files_content)
def test_put_file_contents(self, file_name, content, subdir):
"""Test creating remote file with given contents"""
Expand Down

0 comments on commit 4bb5f67

Please sign in to comment.