Skip to content

Commit

Permalink
folder download method fixes, dev requirements for black
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Oct 4, 2023
1 parent 720c7a1 commit ad4afa8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
7 changes: 2 additions & 5 deletions examples/sharepoint/folders/download_as_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import tempfile

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.utilities.move_copy_util import MoveCopyUtil
from tests import test_client_credentials, test_team_site_url


Expand All @@ -16,8 +15,6 @@ def print_progress(file):
ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
from_folder = ctx.web.lists.get_by_title("Documents").root_folder
zip_path = os.path.join(tempfile.mkdtemp(), "download.zip")
with open(zip_path, "wb") as local_file:
MoveCopyUtil.download_folder_as_zip(
from_folder, local_file, print_progress
).execute_query()
with open(zip_path, "wb") as to_file:
from_folder.download_folder(to_file, print_progress).execute_query()
print("Files has been downloaded: {0}".format(zip_path))
13 changes: 13 additions & 0 deletions office365/sharepoint/folders/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ def from_url(abs_url):
relative_url = abs_url.replace(ctx.base_url, "")
return ctx.web.get_folder_by_server_relative_url(relative_url)

def download_folder(
self, download_file, after_file_downloaded=None, recursive=True
):
"""
Downloads a folder into a zip file
:param typing.IO download_file: A download zip file object
:param (office365.sharepoint.files.file.File)->None after_file_downloaded: A download callback
:param bool recursive: Determines whether to traverse folders recursively
"""
return MoveCopyUtil.download_folder(
self, download_file, after_file_downloaded, recursive
)

def get_files(self, recursive=False):
"""
Retrieves files
Expand Down
12 changes: 6 additions & 6 deletions office365/sharepoint/utilities/move_copy_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ def move_folder_by_path(context, src_path, dest_path, options):
return binding_type

@staticmethod
def download_folder_as_zip(
parent_folder, download_file, after_file_downloaded=None, recursive=True
def download_folder(
remove_folder, download_file, after_file_downloaded=None, recursive=True
):
"""
Downloads a folder into a zip file
:param office365.sharepoint.folders.folder.Folder parent_folder: Parent folder
:param office365.sharepoint.folders.folder.Folder remove_folder: Parent folder
:param typing.IO download_file: A download zip file object
:param (office365.sharepoint.files.file.File)->None after_file_downloaded: A download callback
:param bool recursive: Determines whether to traverse folders recursively
Expand All @@ -153,7 +153,7 @@ def download_folder_as_zip(
def _get_file_name(file):
return os.path.join(
file.parent_folder.serverRelativeUrl.replace(
parent_folder.serverRelativeUrl, ""
remove_folder.serverRelativeUrl, ""
),
file.name,
)
Expand Down Expand Up @@ -184,5 +184,5 @@ def _download_files(rt):

folder.expand(["Files", "Folders"]).get().after_execute(_download_files)

_download_folder(parent_folder)
return parent_folder
_download_folder(remove_folder)
return remove_folder
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ astunparse~=1.6.3
Faker~=4.1.2
adal~=1.2.4
configparser~=5.0.2
black

0 comments on commit ad4afa8

Please sign in to comment.