Skip to content

Commit

Permalink
#748: download_session fix, refactorings and type hints for event han…
Browse files Browse the repository at this point in the history
…dler
  • Loading branch information
vgrem committed Oct 9, 2023
1 parent 3838b48 commit 6505e23
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/sharepoint/files/download_large.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def print_download_progress(offset):


ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
file_url = "Shared Documents/big_buck_bunny.mp4"
file_url = "Shared Documents/Archive/big_buck_bunny.mp4"
source_file = ctx.web.get_file_by_server_relative_path(file_url)
local_file_name = os.path.join(tempfile.mkdtemp(), os.path.basename(file_url))
with open(local_file_name, "wb") as local_file:
Expand Down
2 changes: 0 additions & 2 deletions office365/runtime/odata/v4/upload_session_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from office365.runtime.client_request import ClientRequest
from office365.runtime.http.http_method import HttpMethod
from office365.runtime.http.request_options import RequestOptions
from office365.runtime.types.event_handler import EventHandler


class UploadSessionRequest(ClientRequest):
Expand All @@ -15,7 +14,6 @@ def __init__(self, file_object, chunk_size, chunk_uploaded=None):
"""
super(UploadSessionRequest, self).__init__()
self._file_object = file_object
self.chunk_uploaded = EventHandler(True)
self._chunk_size = chunk_size
self._chunk_uploaded = chunk_uploaded
self._range_start = 0
Expand Down
29 changes: 27 additions & 2 deletions office365/runtime/types/event_handler.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import types
from typing import Callable

from typing_extensions import Self


class EventHandler:
def __init__(self, once=False):
# type: (bool) -> None
self._listeners = []
self._once = once

def __contains__(self, e):
return e in self._listeners
def __contains__(self, listener):
# type: (Callable[..., None]) -> bool
return listener in self._listeners

def __iter__(self):
return iter(self._listeners)

def __iadd__(self, listener):
# type: (Callable[..., None]) -> Self
self._listeners.append(listener)
return self

def __isub__(self, listener):
# type: (Callable[..., None]) -> Self
self._listeners.remove(listener)
return self

Expand All @@ -25,3 +35,18 @@ def notify(self, *args, **kwargs):
if self._once:
self._listeners.remove(listener)
listener(*args, **kwargs)

@staticmethod
def is_builtin(listener):
# type: (Callable[..., None]) -> bool
from office365.runtime.client_request import ClientRequest
from office365.runtime.client_runtime_context import ClientRuntimeContext

if isinstance(listener, types.MethodType):
return isinstance(listener.__self__, ClientRequest) or isinstance(
listener.__self__, ClientRuntimeContext
)
if isinstance(listener, types.FunctionType):
return listener.__module__ == ClientRuntimeContext.__module__
else:
raise ValueError("Invalid listener type")
6 changes: 4 additions & 2 deletions office365/sharepoint/client_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.queries.delete_entity import DeleteEntityQuery
from office365.runtime.queries.update_entity import UpdateEntityQuery
from office365.runtime.types.event_handler import EventHandler
from office365.sharepoint.portal.sites.status import SiteStatus
from office365.sharepoint.publishing.pages.service import SitePageService
from office365.sharepoint.request_user_context import RequestUserContext
Expand Down Expand Up @@ -201,9 +202,10 @@ def _get_context_web_information(self):
Returns an ContextWebInformation object that specifies metadata about the site
"""
client = ODataRequest(JsonLightFormat())
client.beforeExecute += self._authenticate_request
for e in self.pending_request().beforeExecute:
client.beforeExecute += e
client.beforeExecute -= self._build_modification_query
if not EventHandler.is_builtin(e):
client.beforeExecute += e
request = RequestOptions("{0}/contextInfo".format(self.service_root_url()))
request.method = HttpMethod.Post
response = client.execute_request_direct(request)
Expand Down
6 changes: 0 additions & 6 deletions office365/sharepoint/files/file.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import datetime

from office365.runtime.client_result import ClientResult
from office365.runtime.compat import is_string_type
from office365.runtime.http.http_method import HttpMethod
from office365.runtime.http.request_options import RequestOptions
from office365.runtime.paths.resource_path import ResourcePath
Expand All @@ -22,13 +21,8 @@
from office365.sharepoint.permissions.irm.file_settings import (
InformationRightsManagementFileSettings,
)
from office365.sharepoint.permissions.irm.settings import (
InformationRightsManagementSettings,
)
from office365.sharepoint.principal.users.user import User
from office365.sharepoint.types.resource_path import ResourcePath as SPResPath
from office365.sharepoint.utilities.move_copy_options import MoveCopyOptions
from office365.sharepoint.utilities.move_copy_util import MoveCopyUtil
from office365.sharepoint.utilities.upload_status import UploadStatus
from office365.sharepoint.utilities.wopi_frame_action import SPWOPIFrameAction
from office365.sharepoint.webparts.limited_manager import LimitedWebPartManager
Expand Down

0 comments on commit 6505e23

Please sign in to comment.