Skip to content

Commit 2253066

Browse files
committed
typings enhancements & Folder.copy_to method fix
1 parent 0757e43 commit 2253066

File tree

9 files changed

+65
-66
lines changed

9 files changed

+65
-66
lines changed

examples/sharepoint/files/upload.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Demonstrates how to upload a small files (up to 4MB in size)
33
"""
4+
import os
45

56
from office365.sharepoint.client_context import ClientContext
67
from tests import test_team_site_url, test_user_credentials
@@ -10,7 +11,11 @@
1011
list_title = "Documents"
1112
folder = ctx.web.lists.get_by_title(list_title).root_folder
1213
# path = "../../data/SharePoint User Guide.docx"
13-
path = "../../data/Sample.pdf"
14-
with open(path, "rb") as f:
15-
file = folder.files.upload(f).execute_query()
14+
# path = "../../data/Sample.pdf"
15+
path = "../../data/countries.json"
16+
# with open(path, "rb") as f:
17+
# file = folder.files.upload(f).execute_query()
18+
with open(path, "rb") as content_file:
19+
file_content = content_file.read()
20+
file = folder.upload_file(os.path.basename(path), file_content).execute_query()
1621
print("File has been uploaded into: {0}".format(file.serverRelativeUrl))

examples/sharepoint/folders/copy_folder.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@
88
ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
99

1010
# creates a temporary folder first in a Documents library
11-
folder_from = ctx.web.default_document_library().root_folder.add(
12-
create_unique_name("from")
13-
)
11+
# folder_from = ctx.web.default_document_library().root_folder.add(
12+
# create_unique_name("from")
13+
# )
14+
folder_from = ctx.web.get_folder_by_server_relative_url("Shared Documents/Archive/2001")
15+
1416
# folder_to = ctx.web.default_document_library().root_folder.add(create_unique_name("to"))
15-
folder_to_url = "/sites/team/Shared Documents/Archive/2001/01"
17+
# folder_to_url = "/sites/team/Shared Documents/Archive/2001/01"
18+
folder_to = ctx.web.get_folder_by_server_relative_url("Shared Documents/Archive/2002")
1619

1720
# copies the folder with a new name
18-
folder = folder_from.copy_to(folder_to_url).execute_query()
21+
folder = folder_from.copy_to(folder_to).execute_query()
1922
print(
2023
"Folder has been copied from '{0}' into '{1}'".format(
2124
folder_from.serverRelativeUrl, folder.serverRelativeUrl
2225
)
2326
)
2427

2528
# clean up
26-
folder_from.delete_object().execute_query()
27-
folder.delete_object().execute_query()
29+
# folder_from.delete_object().execute_query()
30+
# folder.delete_object().execute_query()

office365/directory/extensions/open_type.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from office365.directory.extensions.extension import Extension
24

35

@@ -10,8 +12,6 @@ class OpenTypeExtension(Extension):
1012

1113
@property
1214
def extension_name(self):
13-
"""
14-
A unique text identifier for an open type data extension. Optional.
15-
:rtype: str
16-
"""
15+
# type: () -> Optional[str]
16+
"""A unique text identifier for an open type data extension."""
1717
return self.properties.get("extensionName", None)

office365/onedrive/driveitems/driveItem.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ def upload(self, name, content):
246246
:type content: str or bytes or None
247247
"""
248248
return_type = DriveItem(self.context, UrlPath(name, self.resource_path))
249+
self.children.add_child(return_type)
249250
qry = ServiceOperationQuery(
250251
return_type, "content", None, content, None, return_type
251252
)
252-
self.children.add_child(qry.return_type)
253253

254254
def _modify_query(request):
255255
# type: (RequestOptions) -> None
@@ -533,15 +533,13 @@ def get_activities_by_interval(self, start_dt=None, end_dt=None, interval=None):
533533
self.context, ItemActivityStat, self.resource_path
534534
)
535535

536-
def _create_query():
537-
params = {
538-
"startDateTime": start_dt.strftime("%m-%d-%Y") if start_dt else None,
539-
"endDateTime": end_dt.strftime("%m-%d-%Y") if end_dt else None,
540-
"interval": interval,
541-
}
542-
return FunctionQuery(self, "getActivitiesByInterval", params, return_type)
536+
params = {
537+
"startDateTime": start_dt.strftime("%m-%d-%Y") if start_dt else None,
538+
"endDateTime": end_dt.strftime("%m-%d-%Y") if end_dt else None,
539+
"interval": interval,
540+
}
543541

544-
qry = _create_query()
542+
qry = FunctionQuery(self, "getActivitiesByInterval", params, return_type)
545543
self.context.add_query(qry)
546544
return return_type
547545

office365/runtime/client_runtime_context.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ def has_pending_request(self):
3333

3434
def build_request(self, query):
3535
# type: (ClientQuery) -> RequestOptions
36-
"""
37-
Builds a request
38-
"""
36+
"""Builds a request"""
3937
self._current_query = query
4038
request = self.pending_request().build_request(query)
4139
self.pending_request().beforeExecute.notify(request)
@@ -98,8 +96,8 @@ def load(
9896
self.after_query_execute(after_loaded, client_object)
9997
return self
10098

101-
def before_query_execute(self, action, once=True, *args, **kwargs):
102-
# type: (Callable[[RequestOptions, Any, Any], None], bool, Any, Any) -> Self
99+
def before_query_execute(self, action, once=True):
100+
# type: (Callable[[RequestOptions], None], bool) -> Self
103101
"""
104102
Attach an event handler which is triggered before query is submitted to server
105103
@@ -115,13 +113,13 @@ def _prepare_request(request):
115113
if self.current_query.id == query.id:
116114
if once:
117115
self.pending_request().beforeExecute -= _prepare_request
118-
action(request, *args, **kwargs)
116+
action(request)
119117

120118
self.pending_request().beforeExecute += _prepare_request
121119
return self
122120

123-
def before_execute(self, action, once=True, *args, **kwargs):
124-
# type: (Callable[[RequestOptions, ...], None], bool, Optional[Any], Optional[Any]) -> Self
121+
def before_execute(self, action, once=True):
122+
# type: (Callable[[RequestOptions], None], bool) -> Self
125123
"""
126124
Attach an event handler which is triggered before request is submitted to server
127125
:param (office365.runtime.http.request_options.RequestOptions, any) -> None action:
@@ -132,13 +130,13 @@ def _process_request(request):
132130
# type: (RequestOptions) -> None
133131
if once:
134132
self.pending_request().beforeExecute -= _process_request
135-
action(request, *args, **kwargs)
133+
action(request)
136134

137135
self.pending_request().beforeExecute += _process_request
138136
return self
139137

140138
def after_query_execute(self, action, *args, **kwargs):
141-
# type: (Callable[[Any, Any], None], Any, Any) -> Self
139+
# type: (Callable[..., None], Any, Any) -> Self
142140
"""Attach an event handler which is triggered after query is submitted to server"""
143141
if len(self._queries) == 0:
144142
return
@@ -159,15 +157,15 @@ def _process_response(resp):
159157

160158
return self
161159

162-
def after_execute(self, action, once=True, *args, **kwargs):
163-
# type: (Callable[[requests.Response, Any, Any], None], bool, Any, Any) -> Self
160+
def after_execute(self, action, once=True):
161+
# type: (Callable[[requests.Response], None], bool) -> Self
164162
"""Attach an event handler which is triggered after request is submitted to server"""
165163

166164
def _process_response(response):
167165
# type: (requests.Response) -> None
168166
if once:
169167
self.pending_request().afterExecute -= _process_response
170-
action(response, *args, **kwargs)
168+
action(response)
171169

172170
self.pending_request().afterExecute += _process_response
173171
return self

office365/sharepoint/folders/folder.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def copy_to(self, destination, keep_both=False, reset_author_and_created=False):
380380
return_type = Folder(self.context)
381381
self.parent_collection.add_child(return_type)
382382

383-
def _copy_folder(destination_folder):
383+
def _copy_to(destination_folder):
384384
# type: ("Folder") -> None
385385
destination_url = "/".join(
386386
[destination_folder.serverRelativeUrl, self.name]
@@ -396,15 +396,11 @@ def _copy_folder(destination_folder):
396396

397397
def _source_folder_resolved():
398398
if isinstance(destination, Folder):
399-
destination.ensure_property(
400-
"ServerRelativeUrl", _copy_folder, destination
401-
)
399+
destination.ensure_property("ServerRelativeUrl", _copy_to, destination)
402400
else:
403-
self.context.web.ensure_folder_path(destination).after_execute(
404-
_copy_folder
405-
)
401+
self.context.web.ensure_folder_path(destination).after_execute(_copy_to)
406402

407-
self.ensure_property("ServerRelativeUrl", _source_folder_resolved)
403+
self.ensure_properties(["ServerRelativeUrl", "Name"], _source_folder_resolved)
408404
return return_type
409405

410406
def copy_to_using_path(

office365/sharepoint/listitems/listitem.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ def share(
210210
ExternalSharingSiteOption.Edit: "role:1073741827",
211211
}
212212

213-
def _picker_value_resolved(resp, picker_result):
213+
def _picker_value_resolved(picker_result):
214+
# type: (ClientResult) -> None
214215
file_abs_url = self.get_property("EncodedAbsUrl")
215216
picker_value = "[{0}]".format(picker_result.value)
216217
from office365.sharepoint.webs.web import Web
@@ -229,15 +230,12 @@ def _picker_value_resolved(resp, picker_result):
229230
return_type=return_type,
230231
)
231232

232-
def _property_resolved():
233-
picker_result = (
234-
ClientPeoplePickerWebServiceInterface.client_people_picker_resolve_user(
235-
self.context, user_principal_name
236-
)
237-
)
238-
self.context.after_execute(_picker_value_resolved, True, picker_result)
233+
def _url_resolved():
234+
ClientPeoplePickerWebServiceInterface.client_people_picker_resolve_user(
235+
self.context, user_principal_name
236+
).after_execute(_picker_value_resolved)
239237

240-
self.ensure_property("EncodedAbsUrl", _property_resolved)
238+
self.ensure_property("EncodedAbsUrl", _url_resolved)
241239
return return_type
242240

243241
def unshare(self):

office365/sharepoint/portal/sites/manager.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from office365.runtime.client_result import ClientResult
22
from office365.runtime.http.http_method import HttpMethod
3+
from office365.runtime.http.request_options import RequestOptions
34
from office365.runtime.paths.resource_path import ResourcePath
45
from office365.runtime.queries.service_operation import ServiceOperationQuery
56
from office365.sharepoint.entity import Entity
@@ -73,13 +74,13 @@ def get_status(self, site_url):
7374
qry = ServiceOperationQuery(
7475
self, "Status", None, {"url": site_url}, None, response
7576
)
76-
self.context.add_query(qry)
7777

78-
def _construct_status_request(request):
78+
def _construct_request(request):
79+
# type: (RequestOptions) -> None
7980
request.method = HttpMethod.Get
8081
request.url += "?url='{0}'".format(site_url)
8182

82-
self.context.before_execute(_construct_status_request)
83+
self.context.add_query(qry).before_execute(_construct_request)
8384
return response
8485

8586
def get_site_url(self, site_id):

office365/sharepoint/webs/template.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from office365.sharepoint.entity import Entity
24

35

@@ -9,42 +11,41 @@ def __repr__(self):
911

1012
@property
1113
def description(self):
12-
"""Gets a value that specifies the description of the list template.
13-
:rtype: str or None
14-
"""
14+
# type: () -> Optional[str]
15+
"""Gets a value that specifies the description of the list template."""
1516
return self.properties.get("Description", None)
1617

1718
@property
1819
def display_category(self):
20+
# type: () -> Optional[str]
1921
"""
2022
Specifies the display name for the category that this site definition configuration or site template is
2123
a part of.
22-
:rtype: str or None
2324
"""
2425
return self.properties.get("DisplayCategory", None)
2526

2627
@property
2728
def image_url(self):
29+
# type: () -> Optional[str]
2830
"""
2931
Specifies the URL for the image that is associated with the site definition configuration or site template.
30-
:rtype: str or None
3132
"""
3233
return self.properties.get("ImageUrl", None)
3334

3435
@property
3536
def is_hidden(self):
37+
# type: () -> Optional[bool]
3638
"""
3739
Specifies whether the site definition configuration is displayed in the user interface for creating new sites
38-
:rtype: bool or None
3940
"""
4041
return self.properties.get("IsHidden", None)
4142

4243
@property
4344
def is_root_web_only(self):
45+
# type: () -> Optional[bool]
4446
"""
4547
Specifies whether the site definition configuration or site template can only be applied to the top-level site
4648
in the site collection.
47-
:rtype: bool or None
4849
"""
4950
return self.properties.get("IsRootWebOnly", None)
5051

@@ -74,7 +75,6 @@ def name(self):
7475

7576
@property
7677
def title(self):
77-
"""Specifies the display name for the site definition configuration or site template.
78-
:rtype: str or None
79-
"""
78+
# type: () -> Optional[str]
79+
"""Specifies the display name for the site definition configuration or site template."""
8080
return self.properties.get("Title", None)

0 commit comments

Comments
 (0)