Skip to content

Commit

Permalink
typings & examples updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Feb 18, 2024
1 parent a1bf24e commit 187d4d2
Show file tree
Hide file tree
Showing 25 changed files with 140 additions and 110 deletions.
13 changes: 10 additions & 3 deletions examples/directory/groups/create_m365.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
https://learn.microsoft.com/en-us/graph/api/group-post-groups?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from tests import create_unique_name
from tests.graph_case import acquire_token_by_username_password
from tests import (
create_unique_name,
test_client_id,
test_password,
test_tenant,
test_username,
)

grp_name = create_unique_name("Group")
client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
group = client.groups.create_m365(grp_name).execute_query()

# clean up resources
Expand Down
5 changes: 2 additions & 3 deletions examples/onedrive/lists/create_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
https://learn.microsoft.com/en-us/graph/api/list-create?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from tests import create_unique_name
from tests.graph_case import acquire_token_by_client_credentials
from tests import create_unique_name, test_client_id, test_client_secret, test_tenant

client = GraphClient(acquire_token_by_client_credentials)
client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret)

print("Creating a custom list...")
custom_list = client.sites.root.lists.add(
Expand Down
15 changes: 15 additions & 0 deletions examples/sharepoint/files/get_download_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
Returns a link for downloading the file without authentication.
"""
from office365.sharepoint.client_context import ClientContext
from tests import test_client_credentials, test_team_site_url

ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
file_url = "Shared Documents/Financial Sample.xlsx"

result = (
ctx.web.get_file_by_server_relative_path(file_url)
.get_pre_authorized_access_url(1)
.execute_query()
)
print(result.value)
3 changes: 1 addition & 2 deletions examples/sharepoint/files/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

list_title = "Documents"
folder = ctx.web.lists.get_by_title(list_title).root_folder
# path = "../../data/Financial Sample.csv"
path = "../../data/report '123.csv"
path = "../../data/Financial Sample.xlsx"
with open(path, "rb") as f:
file = folder.files.upload(f).execute_query()
print("File has been uploaded into: {0}".format(file.serverRelativeUrl))
16 changes: 16 additions & 0 deletions examples/sharepoint/files/upload_with_checksum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Demonstrates how to upload a file
"""

from office365.sharepoint.client_context import ClientContext
from tests import test_team_site_url, test_user_credentials

ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)

list_title = "Documents"
folder = ctx.web.lists.get_by_title(list_title).root_folder
# local_path = "../../data/Financial Sample.xlsx"
local_path = "../../../tests/data/big_buck_bunny.mp4"
with open(local_path, "rb") as f:
file = folder.files.upload_with_checksum(f).execute_query()
print("File has been uploaded into: {0}".format(file.serverRelativeUrl))
2 changes: 1 addition & 1 deletion examples/sharepoint/taxonomy/get_group_by_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
term_group = ctx.taxonomy.term_store.term_groups.get_by_name(
term_group_name
).execute_query()
print(term_group.id)
print(term_group)
2 changes: 1 addition & 1 deletion examples/sharepoint/taxonomy/get_term_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
term_group = ctx.taxonomy.term_store.term_groups.get_by_name("Geography")
term_sets = term_group.get_term_sets_by_name("Countries").execute_query()
for ts in term_sets:
print(ts.id)
print(ts)
5 changes: 3 additions & 2 deletions office365/entity_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from office365.entity import Entity
from office365.runtime.client_object_collection import ClientObjectCollection
from office365.runtime.compat import is_string_type
from office365.runtime.paths.item import ItemPath
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.paths.v4.entity import EntityPath
from office365.runtime.queries.create_entity import CreateEntityQuery
Expand Down Expand Up @@ -43,7 +42,9 @@ def __getitem__(self, key):
def add(self, **kwargs):
# type: (Any) -> T
"""Creates an entity and prepares the query"""
return_type = self.create_typed_object(kwargs, ItemPath(self.resource_path))
return_type = self.create_typed_object(
kwargs, EntityPath(None, self.resource_path)
)
self.add_child(return_type)
qry = CreateEntityQuery(self, return_type, return_type)
self.context.add_query(qry)
Expand Down
5 changes: 5 additions & 0 deletions office365/onedrive/files/hashes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from office365.runtime.client_value import ClientValue


class Hashes(ClientValue):
"""The Hashes resource groups available hashes into a single structure for an item."""
4 changes: 2 additions & 2 deletions office365/onedrive/lists/collection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from office365.entity_collection import EntityCollection
from office365.onedrive.lists.list import List
from office365.runtime.paths.item import ItemPath
from office365.runtime.paths.v4.entity import EntityPath
from office365.runtime.queries.create_entity import CreateEntityQuery


Expand All @@ -17,7 +17,7 @@ def add(self, display_name, list_template="genericList"):
:param str display_name: The displayable title of the list.
:param str list_template: The base list template used in creating the list
"""
return_type = List(self.context, ItemPath(self.resource_path))
return_type = List(self.context, EntityPath(None, self.resource_path))
self.add_child(return_type)
payload = {
"displayName": display_name,
Expand Down
8 changes: 0 additions & 8 deletions office365/runtime/paths/item.py

This file was deleted.

5 changes: 3 additions & 2 deletions office365/sharepoint/files/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@ def upload(self, path_or_file):
def upload_with_checksum(self, file_object, chunk_size=1024):
# type: (IO, int) -> File
""" """
h = hashlib.sha1() # hashlib.md5()
h = hashlib.md5()
file_name = os.path.basename(file_object.name)
upload_id = str(uuid.uuid4())

def _upload_session(return_type):
# type: (File) -> None
content = file_object.read(chunk_size)
h.update(content)

return_type.upload_with_checksum(
upload_id, h.hexdigest(), content
).after_execute(_upload_session)
) # .after_execute(_upload_session)

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

Expand Down
14 changes: 14 additions & 0 deletions office365/sharepoint/files/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ def get_content(self):
self.context.add_query(qry)
return return_type

def get_pre_authorized_access_url(self, expiration_hours):
# type: (int) -> ClientResult[str]
"""Returns a link for downloading the file without authentication.
:param int expiration_hours: The number of hours until the link expires. If the maximum expiration time
defined in the web application is less than the specified expiration time, the maximum expiration time
takes precedence.
"""
return_type = ClientResult(self.context, str())
params = {"expirationHours": expiration_hours}
qry = FunctionQuery(self, "GetPreAuthorizedAccessUrl", params, return_type)
self.context.add_query(qry)
return return_type

def get_absolute_url(self):
# type: () -> ClientResult[str]
"""Gets absolute url of a File"""
Expand Down Expand Up @@ -488,6 +501,7 @@ def get_upload_status(self, upload_id):
return return_type

def upload_with_checksum(self, upload_id, checksum, stream):
# type: (str, str, bytes) -> File
"""
:param str upload_id: The upload session ID.
:param str checksum:
Expand Down
13 changes: 5 additions & 8 deletions office365/sharepoint/navigation/node.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from typing import Optional
from typing import TYPE_CHECKING, Optional

from office365.runtime.paths.resource_path import ResourcePath
from office365.sharepoint.entity import Entity
from office365.sharepoint.translation.user_resource import UserResource

if TYPE_CHECKING:
from office365.sharepoint.navigation.node_collection import NavigationNodeCollection


class NavigationNode(Entity):
"""
Expand All @@ -20,10 +23,6 @@ def __repr__(self):
@property
def children(self):
"""Gets the collection of child nodes of the navigation node."""
from office365.sharepoint.navigation.node_collection import (
NavigationNodeCollection,
)

return self.properties.get(
"Children",
NavigationNodeCollection(
Expand Down Expand Up @@ -67,9 +66,7 @@ def is_external(self):

@property
def parent_collection(self):
"""
:rtype: office365.sharepoint.navigation.node_collection.NavigationNodeCollection
"""
# type: () -> NavigationNodeCollection
return self._parent_collection

@property
Expand Down
13 changes: 3 additions & 10 deletions office365/sharepoint/permissions/irm/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,13 @@ def allow_write_copy(self):
@property
def disable_document_browser_view(self):
# type: () -> Optional[bool]
"""
Specifies whether a user can write in a copy of the downloaded document.
:rtype: bool or None
"""
"""Specifies whether a user can write in a copy of the downloaded document."""
return self.properties.get("DisableDocumentBrowserView", None)

@property
def document_access_expire_days(self):
# type: () -> Optional[int]
"""
Specifies the number of days after which the downloaded document will expire.
"""
"""Specifies the number of days after which the downloaded document will expire."""
return self.properties.get("DocumentAccessExpireDays", None)

@property
Expand All @@ -66,9 +61,7 @@ def enable_document_access_expire(self):
@property
def enable_group_protection(self):
# type: () -> Optional[bool]
"""
Specifies whether the permission of the downloaded document is applicable to a group.
"""
"""Specifies whether the permission of the downloaded document is applicable to a group."""
return self.properties.get("EnableGroupProtection", None)

@property
Expand Down
7 changes: 4 additions & 3 deletions office365/sharepoint/publishing/pages/campaign_publication.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.sharepoint.publishing.highlights_info import HighlightsInfo
from office365.sharepoint.publishing.pages.page import SitePage
Expand All @@ -15,9 +17,8 @@ def get_highlights_info(self):

@property
def email_endpoint(self):
"""
:rtype: str
"""
# type: () -> Optional[str]
""""""
return self.properties.get("EmailEndpoint", None)

@property
Expand Down
7 changes: 4 additions & 3 deletions office365/sharepoint/sitedesigns/run.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import Optional

from office365.sharepoint.entity import Entity


class SiteDesignRun(Entity):
@property
def site_design_id(self):
"""
:rtype: str or None
"""
# type: () -> Optional[str]
""""""
return self.properties.get("SiteDesignID", None)

@property
Expand Down
6 changes: 6 additions & 0 deletions office365/sharepoint/taxonomy/sets/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
class TermSet(TaxonomyItem):
"""Represents a hierarchical or flat set of Term objects known as a 'TermSet'."""

def __repr__(self):
if self.is_property_available("localizedNames"):
return repr(self.localized_names[0])
else:
return self.entity_type_name

@property
def localized_names(self):
return self.properties.get(
Expand Down
5 changes: 3 additions & 2 deletions office365/sharepoint/translation/sync_translator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from office365.runtime.client_result import ClientResult
from office365.runtime.paths.service_operation import ServiceOperationPath
from office365.runtime.queries.service_operation import ServiceOperationQuery
Expand Down Expand Up @@ -45,13 +47,12 @@ def translate(self, input_file, output_file):

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

Expand Down
32 changes: 12 additions & 20 deletions office365/sharepoint/usercustomactions/action.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from office365.sharepoint.entity import Entity


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

@property
def client_side_component_id(self):
"""
:rtype: str or None
"""
# type: () -> Optional[str]
""""""
return self.properties.get("ClientSideComponentId", None)

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

@script_block.setter
def script_block(self, value):
# type: (str) -> None
"""
Sets the value that specifies the ECMAScript to be executed when the custom action is performed.
:type value: str
"""
self.set_property("ScriptBlock", value)

@property
def script_src(self):
"""
Gets a value that specifies the URI of a file which contains the ECMAScript to execute on the page
:rtype: str or None
"""
# type: () -> Optional[str]
"""Gets a value that specifies the URI of a file which contains the ECMAScript to execute on the page"""
return self.properties.get("ScriptSrc", None)

@script_src.setter
def script_src(self, value):
# type: (str) -> None
"""
Sets a value that specifies the URI of a file which contains the ECMAScript to execute on the page
:type value: str
"""
self.set_property("ScriptSrc", value)

@property
def url(self):
"""
Gets or sets the URL, URI, or ECMAScript (JScript, JavaScript) function associated with the action.
:rtype: str or None
"""
# type: () -> Optional[str]
"""Gets or sets the URL, URI, or ECMAScript (JScript, JavaScript) function associated with the action."""
return self.properties.get("Url", None)

0 comments on commit 187d4d2

Please sign in to comment.