Skip to content

Commit 187d4d2

Browse files
committed
typings & examples updates
1 parent a1bf24e commit 187d4d2

File tree

25 files changed

+140
-110
lines changed

25 files changed

+140
-110
lines changed

examples/directory/groups/create_m365.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77
https://learn.microsoft.com/en-us/graph/api/group-post-groups?view=graph-rest-1.0
88
"""
99
from office365.graph_client import GraphClient
10-
from tests import create_unique_name
11-
from tests.graph_case import acquire_token_by_username_password
10+
from tests import (
11+
create_unique_name,
12+
test_client_id,
13+
test_password,
14+
test_tenant,
15+
test_username,
16+
)
1217

1318
grp_name = create_unique_name("Group")
14-
client = GraphClient(acquire_token_by_username_password)
19+
client = GraphClient.with_username_and_password(
20+
test_tenant, test_client_id, test_username, test_password
21+
)
1522
group = client.groups.create_m365(grp_name).execute_query()
1623

1724
# clean up resources

examples/onedrive/lists/create_list.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
https://learn.microsoft.com/en-us/graph/api/list-create?view=graph-rest-1.0
77
"""
88
from office365.graph_client import GraphClient
9-
from tests import create_unique_name
10-
from tests.graph_case import acquire_token_by_client_credentials
9+
from tests import create_unique_name, test_client_id, test_client_secret, test_tenant
1110

12-
client = GraphClient(acquire_token_by_client_credentials)
11+
client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret)
1312

1413
print("Creating a custom list...")
1514
custom_list = client.sites.root.lists.add(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Returns a link for downloading the file without authentication.
3+
"""
4+
from office365.sharepoint.client_context import ClientContext
5+
from tests import test_client_credentials, test_team_site_url
6+
7+
ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
8+
file_url = "Shared Documents/Financial Sample.xlsx"
9+
10+
result = (
11+
ctx.web.get_file_by_server_relative_path(file_url)
12+
.get_pre_authorized_access_url(1)
13+
.execute_query()
14+
)
15+
print(result.value)

examples/sharepoint/files/upload.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
list_title = "Documents"
1111
folder = ctx.web.lists.get_by_title(list_title).root_folder
12-
# path = "../../data/Financial Sample.csv"
13-
path = "../../data/report '123.csv"
12+
path = "../../data/Financial Sample.xlsx"
1413
with open(path, "rb") as f:
1514
file = folder.files.upload(f).execute_query()
1615
print("File has been uploaded into: {0}".format(file.serverRelativeUrl))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Demonstrates how to upload a file
3+
"""
4+
5+
from office365.sharepoint.client_context import ClientContext
6+
from tests import test_team_site_url, test_user_credentials
7+
8+
ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
9+
10+
list_title = "Documents"
11+
folder = ctx.web.lists.get_by_title(list_title).root_folder
12+
# local_path = "../../data/Financial Sample.xlsx"
13+
local_path = "../../../tests/data/big_buck_bunny.mp4"
14+
with open(local_path, "rb") as f:
15+
file = folder.files.upload_with_checksum(f).execute_query()
16+
print("File has been uploaded into: {0}".format(file.serverRelativeUrl))

examples/sharepoint/taxonomy/get_group_by_name.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
term_group = ctx.taxonomy.term_store.term_groups.get_by_name(
88
term_group_name
99
).execute_query()
10-
print(term_group.id)
10+
print(term_group)

examples/sharepoint/taxonomy/get_term_set.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
term_group = ctx.taxonomy.term_store.term_groups.get_by_name("Geography")
1010
term_sets = term_group.get_term_sets_by_name("Countries").execute_query()
1111
for ts in term_sets:
12-
print(ts.id)
12+
print(ts)

office365/entity_collection.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from office365.entity import Entity
44
from office365.runtime.client_object_collection import ClientObjectCollection
55
from office365.runtime.compat import is_string_type
6-
from office365.runtime.paths.item import ItemPath
76
from office365.runtime.paths.resource_path import ResourcePath
87
from office365.runtime.paths.v4.entity import EntityPath
98
from office365.runtime.queries.create_entity import CreateEntityQuery
@@ -43,7 +42,9 @@ def __getitem__(self, key):
4342
def add(self, **kwargs):
4443
# type: (Any) -> T
4544
"""Creates an entity and prepares the query"""
46-
return_type = self.create_typed_object(kwargs, ItemPath(self.resource_path))
45+
return_type = self.create_typed_object(
46+
kwargs, EntityPath(None, self.resource_path)
47+
)
4748
self.add_child(return_type)
4849
qry = CreateEntityQuery(self, return_type, return_type)
4950
self.context.add_query(qry)

office365/onedrive/files/hashes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from office365.runtime.client_value import ClientValue
2+
3+
4+
class Hashes(ClientValue):
5+
"""The Hashes resource groups available hashes into a single structure for an item."""

office365/onedrive/lists/collection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from office365.entity_collection import EntityCollection
22
from office365.onedrive.lists.list import List
3-
from office365.runtime.paths.item import ItemPath
3+
from office365.runtime.paths.v4.entity import EntityPath
44
from office365.runtime.queries.create_entity import CreateEntityQuery
55

66

@@ -17,7 +17,7 @@ def add(self, display_name, list_template="genericList"):
1717
:param str display_name: The displayable title of the list.
1818
:param str list_template: The base list template used in creating the list
1919
"""
20-
return_type = List(self.context, ItemPath(self.resource_path))
20+
return_type = List(self.context, EntityPath(None, self.resource_path))
2121
self.add_child(return_type)
2222
payload = {
2323
"displayName": display_name,

office365/runtime/paths/item.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

office365/sharepoint/files/collection.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,18 @@ def upload(self, path_or_file):
4545
def upload_with_checksum(self, file_object, chunk_size=1024):
4646
# type: (IO, int) -> File
4747
""" """
48-
h = hashlib.sha1() # hashlib.md5()
48+
h = hashlib.md5()
4949
file_name = os.path.basename(file_object.name)
5050
upload_id = str(uuid.uuid4())
5151

5252
def _upload_session(return_type):
5353
# type: (File) -> None
5454
content = file_object.read(chunk_size)
5555
h.update(content)
56+
5657
return_type.upload_with_checksum(
5758
upload_id, h.hexdigest(), content
58-
).after_execute(_upload_session)
59+
) # .after_execute(_upload_session)
5960

6061
return self.add(file_name, None, True).after_execute(_upload_session)
6162

office365/sharepoint/files/file.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,19 @@ def get_content(self):
129129
self.context.add_query(qry)
130130
return return_type
131131

132+
def get_pre_authorized_access_url(self, expiration_hours):
133+
# type: (int) -> ClientResult[str]
134+
"""Returns a link for downloading the file without authentication.
135+
:param int expiration_hours: The number of hours until the link expires. If the maximum expiration time
136+
defined in the web application is less than the specified expiration time, the maximum expiration time
137+
takes precedence.
138+
"""
139+
return_type = ClientResult(self.context, str())
140+
params = {"expirationHours": expiration_hours}
141+
qry = FunctionQuery(self, "GetPreAuthorizedAccessUrl", params, return_type)
142+
self.context.add_query(qry)
143+
return return_type
144+
132145
def get_absolute_url(self):
133146
# type: () -> ClientResult[str]
134147
"""Gets absolute url of a File"""
@@ -488,6 +501,7 @@ def get_upload_status(self, upload_id):
488501
return return_type
489502

490503
def upload_with_checksum(self, upload_id, checksum, stream):
504+
# type: (str, str, bytes) -> File
491505
"""
492506
:param str upload_id: The upload session ID.
493507
:param str checksum:

office365/sharepoint/navigation/node.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
from typing import Optional
1+
from typing import TYPE_CHECKING, Optional
22

33
from office365.runtime.paths.resource_path import ResourcePath
44
from office365.sharepoint.entity import Entity
55
from office365.sharepoint.translation.user_resource import UserResource
66

7+
if TYPE_CHECKING:
8+
from office365.sharepoint.navigation.node_collection import NavigationNodeCollection
9+
710

811
class NavigationNode(Entity):
912
"""
@@ -20,10 +23,6 @@ def __repr__(self):
2023
@property
2124
def children(self):
2225
"""Gets the collection of child nodes of the navigation node."""
23-
from office365.sharepoint.navigation.node_collection import (
24-
NavigationNodeCollection,
25-
)
26-
2726
return self.properties.get(
2827
"Children",
2928
NavigationNodeCollection(
@@ -67,9 +66,7 @@ def is_external(self):
6766

6867
@property
6968
def parent_collection(self):
70-
"""
71-
:rtype: office365.sharepoint.navigation.node_collection.NavigationNodeCollection
72-
"""
69+
# type: () -> NavigationNodeCollection
7370
return self._parent_collection
7471

7572
@property

office365/sharepoint/permissions/irm/settings.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,13 @@ def allow_write_copy(self):
3232
@property
3333
def disable_document_browser_view(self):
3434
# type: () -> Optional[bool]
35-
"""
36-
Specifies whether a user can write in a copy of the downloaded document.
37-
:rtype: bool or None
38-
"""
35+
"""Specifies whether a user can write in a copy of the downloaded document."""
3936
return self.properties.get("DisableDocumentBrowserView", None)
4037

4138
@property
4239
def document_access_expire_days(self):
4340
# type: () -> Optional[int]
44-
"""
45-
Specifies the number of days after which the downloaded document will expire.
46-
"""
41+
"""Specifies the number of days after which the downloaded document will expire."""
4742
return self.properties.get("DocumentAccessExpireDays", None)
4843

4944
@property
@@ -66,9 +61,7 @@ def enable_document_access_expire(self):
6661
@property
6762
def enable_group_protection(self):
6863
# type: () -> Optional[bool]
69-
"""
70-
Specifies whether the permission of the downloaded document is applicable to a group.
71-
"""
64+
"""Specifies whether the permission of the downloaded document is applicable to a group."""
7265
return self.properties.get("EnableGroupProtection", None)
7366

7467
@property

office365/sharepoint/publishing/pages/campaign_publication.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from office365.runtime.queries.service_operation import ServiceOperationQuery
24
from office365.sharepoint.publishing.highlights_info import HighlightsInfo
35
from office365.sharepoint.publishing.pages.page import SitePage
@@ -15,9 +17,8 @@ def get_highlights_info(self):
1517

1618
@property
1719
def email_endpoint(self):
18-
"""
19-
:rtype: str
20-
"""
20+
# type: () -> Optional[str]
21+
""""""
2122
return self.properties.get("EmailEndpoint", None)
2223

2324
@property

office365/sharepoint/sitedesigns/run.py

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

35

46
class SiteDesignRun(Entity):
57
@property
68
def site_design_id(self):
7-
"""
8-
:rtype: str or None
9-
"""
9+
# type: () -> Optional[str]
10+
""""""
1011
return self.properties.get("SiteDesignID", None)
1112

1213
@property

office365/sharepoint/taxonomy/sets/set.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
class TermSet(TaxonomyItem):
1010
"""Represents a hierarchical or flat set of Term objects known as a 'TermSet'."""
1111

12+
def __repr__(self):
13+
if self.is_property_available("localizedNames"):
14+
return repr(self.localized_names[0])
15+
else:
16+
return self.entity_type_name
17+
1218
@property
1319
def localized_names(self):
1420
return self.properties.get(

office365/sharepoint/translation/sync_translator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from office365.runtime.client_result import ClientResult
24
from office365.runtime.paths.service_operation import ServiceOperationPath
35
from office365.runtime.queries.service_operation import ServiceOperationQuery
@@ -45,13 +47,12 @@ def translate(self, input_file, output_file):
4547

4648
@property
4749
def output_save_behavior(self):
50+
# type: () -> Optional[int]
4851
"""
4952
The protocol client sets this property to determine the behavior of the protocol server in the case that
5053
the output file already exists when a translation occurs.
5154
5255
If the protocol client does not set this property, the AppendIfPossible (section 3.1.5.2.1.1) behavior is used.
53-
54-
:rtype: int or None
5556
"""
5657
return self.properties.get("OutputSaveBehavior", None)
5758

Lines changed: 12 additions & 20 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

@@ -6,50 +8,40 @@ class UserCustomAction(Entity):
68

79
@property
810
def client_side_component_id(self):
9-
"""
10-
:rtype: str or None
11-
"""
11+
# type: () -> Optional[str]
12+
""""""
1213
return self.properties.get("ClientSideComponentId", None)
1314

1415
@property
1516
def script_block(self):
16-
"""
17-
Gets the value that specifies the ECMAScript to be executed when the custom action is performed.
18-
:rtype: str or None
19-
"""
17+
# type: () -> Optional[str]
18+
"""Gets the value that specifies the ECMAScript to be executed when the custom action is performed."""
2019
return self.properties.get("ScriptBlock", None)
2120

2221
@script_block.setter
2322
def script_block(self, value):
23+
# type: (str) -> None
2424
"""
2525
Sets the value that specifies the ECMAScript to be executed when the custom action is performed.
26-
27-
:type value: str
2826
"""
2927
self.set_property("ScriptBlock", value)
3028

3129
@property
3230
def script_src(self):
33-
"""
34-
Gets a value that specifies the URI of a file which contains the ECMAScript to execute on the page
35-
:rtype: str or None
36-
"""
31+
# type: () -> Optional[str]
32+
"""Gets a value that specifies the URI of a file which contains the ECMAScript to execute on the page"""
3733
return self.properties.get("ScriptSrc", None)
3834

3935
@script_src.setter
4036
def script_src(self, value):
37+
# type: (str) -> None
4138
"""
4239
Sets a value that specifies the URI of a file which contains the ECMAScript to execute on the page
43-
44-
:type value: str
4540
"""
4641
self.set_property("ScriptSrc", value)
4742

4843
@property
4944
def url(self):
50-
"""
51-
Gets or sets the URL, URI, or ECMAScript (JScript, JavaScript) function associated with the action.
52-
53-
:rtype: str or None
54-
"""
45+
# type: () -> Optional[str]
46+
"""Gets or sets the URL, URI, or ECMAScript (JScript, JavaScript) function associated with the action."""
5547
return self.properties.get("Url", None)

0 commit comments

Comments
 (0)