Skip to content

Commit

Permalink
#781 typing fix, refactorings for resource path
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Nov 28, 2023
1 parent c6f4b91 commit bf24503
Show file tree
Hide file tree
Showing 29 changed files with 57 additions and 53 deletions.
2 changes: 1 addition & 1 deletion office365/delta_path.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from office365.runtime.paths.entity import EntityPath
from office365.runtime.paths.v4.entity import EntityPath


class DeltaPath(EntityPath):
Expand Down
2 changes: 1 addition & 1 deletion office365/directory/internal/paths/me.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from office365.runtime.paths.entity import EntityPath
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.paths.v4.entity import EntityPath


class MePath(EntityPath):
Expand Down
2 changes: 1 addition & 1 deletion office365/directory/users/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
from office365.runtime.client_value_collection import ClientValueCollection
from office365.runtime.http.http_method import HttpMethod
from office365.runtime.http.request_options import RequestOptions
from office365.runtime.paths.entity import EntityPath
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.paths.v4.entity import EntityPath
from office365.runtime.queries.create_entity import CreateEntityQuery
from office365.runtime.queries.function import FunctionQuery
from office365.runtime.queries.service_operation import ServiceOperationQuery
Expand Down
2 changes: 1 addition & 1 deletion office365/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from typing_extensions import Self

from office365.runtime.client_object import ClientObject
from office365.runtime.paths.entity import EntityPath
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.paths.v4.entity import EntityPath
from office365.runtime.queries.delete_entity import DeleteEntityQuery
from office365.runtime.queries.update_entity import UpdateEntityQuery

Expand Down
2 changes: 1 addition & 1 deletion office365/onedrive/internal/paths/children.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from office365.runtime.paths.entity import EntityPath
from office365.runtime.paths.v4.entity import EntityPath


class ChildrenPath(EntityPath):
Expand Down
2 changes: 1 addition & 1 deletion office365/onedrive/internal/paths/root.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from office365.runtime.paths.entity import EntityPath
from office365.runtime.paths.v4.entity import EntityPath


class RootPath(EntityPath):
Expand Down
2 changes: 1 addition & 1 deletion office365/onedrive/internal/paths/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class SharedPath(ResourcePath):

@property
def segment(self):
return _url_to_shared_token(self.key)
return _url_to_shared_token(self._key)
6 changes: 3 additions & 3 deletions office365/onedrive/internal/paths/site.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from office365.runtime.compat import is_absolute_url, urlparse
from office365.runtime.paths.entity import EntityPath
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.paths.v4.entity import EntityPath


class SitePath(EntityPath):
"""Resource path for addressing Site resource"""

@property
def segment(self):
if is_absolute_url(self.key):
url_result = urlparse(self.key)
if is_absolute_url(self._key):
url_result = urlparse(self._key)
return ":".join([url_result.hostname, url_result.path])
else:
return super(SitePath, self).segment
Expand Down
4 changes: 2 additions & 2 deletions office365/onedrive/internal/paths/url.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from office365.onedrive.internal.paths.root import RootPath
from office365.runtime.paths.entity import EntityPath
from office365.runtime.paths.v4.entity import EntityPath


class UrlPath(EntityPath):
Expand All @@ -11,7 +11,7 @@ def __init__(self, url, parent, collection=None):
:type parent: office365.runtime.paths.resource_path.ResourcePath
"""
if isinstance(parent, UrlPath):
url = "/".join([parent.key, url])
url = "/".join([parent._key, url])
collection = parent.collection
parent = parent.parent
elif isinstance(parent, RootPath):
Expand Down
4 changes: 2 additions & 2 deletions office365/runtime/auth/authentication_context.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
import sys
from typing import Any, Callable, TypedDict
from typing import Any, Callable

from typing_extensions import Required
from typing_extensions import Required, TypedDict

from office365.runtime.auth.client_credential import ClientCredential
from office365.runtime.auth.providers.acs_token_provider import ACSTokenProvider
Expand Down
2 changes: 1 addition & 1 deletion office365/runtime/paths/appid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class AppIdPath(ResourcePath):

@property
def segment(self):
return "(appId='{0}')".format(self.key)
return "(appId='{0}')".format(self._key)

@property
def delimiter(self):
Expand Down
2 changes: 1 addition & 1 deletion office365/runtime/paths/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def parse_url(path_str):
def build_segment(path):
# type: (ServiceOperationPath) -> str
"""Constructs url for path segment"""
url = path.key or ""
url = path.name or ""
if isinstance(path.parameters, ClientValue):
url += "(@v)?@v={0}".format(json.dumps(path.parameters.to_json()))
elif path.parameters is not None:
Expand Down
2 changes: 1 addition & 1 deletion office365/runtime/paths/item.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from office365.runtime.paths.entity import EntityPath
from office365.runtime.paths.v4.entity import EntityPath


class ItemPath(EntityPath):
Expand Down
6 changes: 1 addition & 5 deletions office365/runtime/paths/resource_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ def parent(self):

@property
def segment(self):
return str(self.key)

@property
def key(self):
return self._key
return str(self._key)

@property
def delimiter(self):
Expand Down
4 changes: 4 additions & 0 deletions office365/runtime/paths/service_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def __init__(self, name, parameters=None, parent=None):
def segment(self):
return ODataPathBuilder.build_segment(self)

@property
def name(self):
return self._key

@property
def parameters(self):
return self._parameters
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from office365.runtime.paths.resource_path import ResourcePath


class KeyPath(ResourcePath):
class EntityPath(ResourcePath):
"""Path for addressing a single entity by key"""

@property
def segment(self):
if self.key is None:
if self._key is None:
return "(<key>)"
elif isinstance(self.key, int):
return "({0})".format(self.key)
return "('{0}')".format(self.key)
elif isinstance(self._key, int):
return "({0})".format(self._key)
return "('{0}')".format(self._key)

@property
def delimiter(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def collection(self):

@property
def segment(self):
return str(self.key or "<key>")
return str(self._key or "<key>")

def patch(self, key, inplace=False):
"""
Expand Down
6 changes: 4 additions & 2 deletions office365/sharepoint/files/checked_out_file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from office365.runtime.paths.key import KeyPath
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.paths.v3.entity import EntityPath
from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.sharepoint.entity import Entity
from office365.sharepoint.principal.users.user import User
Expand Down Expand Up @@ -43,5 +43,7 @@ def set_property(self, name, value, persist_changes=True):
super(CheckedOutFile, self).set_property(name, value, persist_changes)
# fallback: create a new resource path
if name == "CheckedOutById":
self._resource_path = KeyPath(value, self.parent_collection.resource_path)
self._resource_path = EntityPath(
value, self.parent_collection.resource_path
)
return self
6 changes: 3 additions & 3 deletions office365/sharepoint/files/versions/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from typing_extensions import Self

from office365.runtime.client_result import ClientResult
from office365.runtime.paths.key import KeyPath
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.paths.v3.entity import EntityPath
from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.sharepoint.entity import Entity

Expand Down Expand Up @@ -106,9 +106,9 @@ def set_property(self, key, value, persist_changes=True):
super(FileVersion, self).set_property(key, value, persist_changes)
if key.lower() == self.property_ref_name.lower():
if self._resource_path is None:
self._resource_path = KeyPath(
self._resource_path = EntityPath(
value, self.parent_collection.resource_path
)
else:
self._resource_path.patch(value, path_type=KeyPath)
self._resource_path.patch(value, path_type=EntityPath)
return self
4 changes: 2 additions & 2 deletions office365/sharepoint/folders/collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from office365.runtime.paths.key import KeyPath
from office365.runtime.paths.service_operation import ServiceOperationPath
from office365.runtime.paths.v3.entity import EntityPath
from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.sharepoint.entity_collection import EntityCollection
from office365.sharepoint.folders.folder import Folder
Expand Down Expand Up @@ -46,7 +46,7 @@ def add(self, name):
"""Adds the folder that is located at the specified URL to the collection.
:param str name: Specifies the Name of the folder.
"""
return_type = Folder(self.context, KeyPath(name, self.resource_path))
return_type = Folder(self.context, EntityPath(name, self.resource_path))
self.add_child(return_type)
qry = ServiceOperationQuery(self, "Add", [name], None, None, return_type)
self.context.add_query(qry)
Expand Down
6 changes: 3 additions & 3 deletions office365/sharepoint/internal/paths/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ def segment(self):

@property
def web_path(self):
if is_absolute_url(self.key):
url_parts = urlparse(self.key)
if is_absolute_url(self._key):
url_parts = urlparse(self._key)
return url_parts.path
else:
return self.key
return self._key

@property
def parent(self):
Expand Down
6 changes: 3 additions & 3 deletions office365/sharepoint/listitems/listitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from office365.runtime.client_result import ClientResult
from office365.runtime.client_value_collection import ClientValueCollection
from office365.runtime.paths.key import KeyPath
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.paths.v3.entity import EntityPath
from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.sharepoint.attachments.collection import AttachmentCollection
from office365.sharepoint.changes.collection import ChangeCollection
Expand Down Expand Up @@ -559,11 +559,11 @@ def set_property(self, name, value, persist_changes=True):
# fallback: create a new resource path
if name == "Id":
if self._resource_path is None and self.parent_collection is not None:
self._resource_path = KeyPath(
self._resource_path = EntityPath(
value, self.parent_collection.resource_path
)
else:
self._resource_path.patch(value, path_type=KeyPath)
self._resource_path.patch(value, path_type=EntityPath)
return self

def _set_taxonomy_field_value(self, name, value):
Expand Down
4 changes: 2 additions & 2 deletions office365/sharepoint/publishing/pages/metadata.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
from typing import TYPE_CHECKING, Optional

from office365.runtime.paths.key import KeyPath
from office365.runtime.paths.v3.entity import EntityPath
from office365.runtime.types.collections import StringCollection
from office365.sharepoint.entity import Entity
from office365.sharepoint.publishing.pages.version_info import SitePageVersionInfo
Expand Down Expand Up @@ -138,5 +138,5 @@ def set_property(self, name, value, persist_changes=True):
value
).resource_path
else:
self._resource_path.patch(value, path_type=KeyPath)
self._resource_path.patch(value, path_type=EntityPath)
return super(SitePageMetadata, self).set_property(name, value, persist_changes)
4 changes: 2 additions & 2 deletions office365/sharepoint/recyclebin/item.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Optional

from office365.runtime.paths.key import KeyPath
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.paths.service_operation import ServiceOperationPath
from office365.runtime.paths.v3.entity import EntityPath
from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.sharepoint.entity import Entity
from office365.sharepoint.principal.users.user import User
Expand Down Expand Up @@ -67,4 +67,4 @@ def set_property(self, name, value, persist_changes=True):
"GetById", [value], self._parent_collection.resource_path
)
else:
self._resource_path.patch(value, path_type=KeyPath)
self._resource_path.patch(value, path_type=EntityPath)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Optional

from office365.onedrive.listitems.list_item import ListItem
from office365.runtime.paths.key import KeyPath
from office365.runtime.paths.service_operation import ServiceOperationPath
from office365.runtime.paths.v3.entity import EntityPath
from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.entity import Entity
Expand Down Expand Up @@ -144,7 +144,7 @@ def _ensure_site_path(self, action):
def _loaded(return_type):
# type: (ListItem) -> None
site_id = return_type.get_property("SiteId")
self._resource_path = KeyPath(
self._resource_path = EntityPath(
site_id, self.parent_collection.resource_path
)
action()
Expand Down
2 changes: 1 addition & 1 deletion office365/teams/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,6 @@ def get_property(self, name, default_value=None):
def set_property(self, name, value, persist_changes=True):
super(Team, self).set_property(name, value, persist_changes)
# fallback: determine whether resource path is resolved
if name == "id" and self._resource_path.key == "team":
if name == "id" and self._resource_path.segment == "team":
self._resource_path = ResourcePath(value, ResourcePath("teams"))
return self
8 changes: 5 additions & 3 deletions tests/directory/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ class TestDirectory(GraphTestCase):

def test2_get_deleted_groups(self):
deleted_groups = self.client.directory.deleted_groups.get().execute_query()
self.assertEqual(deleted_groups.resource_path.key, "microsoft.graph.group")
self.assertEqual(deleted_groups.resource_path.segment, "microsoft.graph.group")

def test3_get_deleted_users(self):
deleted_users = self.client.directory.deleted_users.get().execute_query()
self.assertEqual(deleted_users.resource_path.key, "microsoft.graph.user")
self.assertEqual(deleted_users.resource_path.segment, "microsoft.graph.user")

def test4_get_deleted_applications(self):
deleted_apps = self.client.directory.deleted_applications.get().execute_query()
self.assertEqual(deleted_apps.resource_path.key, "microsoft.graph.application")
self.assertEqual(
deleted_apps.resource_path.segment, "microsoft.graph.application"
)

def test5_get_member_objects(self):
result = self.client.me.get_member_objects().execute_query()
Expand Down
2 changes: 1 addition & 1 deletion tests/sharepoint/test_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test1_ensure_site_pages_library(self):
self.__class__.pages_list = pages_list

def test2_create_wiki_page(self):
page_name = "WelcomeWikiPage.aspx"
page_name = "WelcomeWikiPage123.aspx"
file = self.__class__.pages_list.create_wiki_page(
page_name, "Wiki content"
).execute_query()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_graph_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ def test_16_resolve_entity_type_name(self):
def test_17_build_path_from_url(self):
path_str = "/teams('7f919b9f-c220-4290-a4d8-5ff9300d1296')/operations('dc97f61a-0040-436f-ac09-427cd2456fd8')"
path = ODataPathBuilder.parse_url(path_str)
self.assertIsNotNone(path.key)
self.assertIsNotNone(path.segment)

0 comments on commit bf24503

Please sign in to comment.