Skip to content

Commit

Permalink
refactorings for resource path
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Dec 2, 2023
1 parent bf24503 commit 480f825
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 29 deletions.
5 changes: 3 additions & 2 deletions examples/onedrive/folders/list_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

client = GraphClient(acquire_token_by_username_password)
drive = client.me.drive
file_items = client.me.drive.root.get_files(True).execute_query()
for file_item in file_items:
# items = client.me.drive.root.get_files(True).execute_query()
items = client.sites.root.lists["Documents"].drive.root.get_files(True).execute_query()
for file_item in items:
print(file_item.web_url)
Empty file removed examples/outlook/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion office365/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ def set_property(self, name, value, persist_changes=True):
value, self.parent_collection.resource_path
)
else:
self._resource_path.patch(value, inplace=True)
self._resource_path.patch(value)
return self
11 changes: 10 additions & 1 deletion office365/entity_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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

if TYPE_CHECKING:
Expand Down Expand Up @@ -32,7 +33,7 @@ def __getitem__(self, key):
return super(EntityCollection, self).__getitem__(key)
elif is_string_type(key):
return self.create_typed_object(
resource_path=ResourcePath(key, self.resource_path)
resource_path=EntityPath(key, self.resource_path)
)
else:
raise ValueError(
Expand All @@ -48,6 +49,14 @@ def add(self, **kwargs):
self.context.add_query(qry)
return return_type

def create_typed_object(self, initial_properties=None, resource_path=None):
# type: (Optional[dict], Optional[ResourcePath]) -> T
if resource_path is None:
resource_path = EntityPath(None, self.resource_path)
return super(EntityCollection, self).create_typed_object(
initial_properties, resource_path
)

@property
def context(self):
# type: () -> GraphClient
Expand Down
2 changes: 1 addition & 1 deletion office365/onedrive/driveitems/driveItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_files(self, recursive=False):
"""Retrieves files
:param bool recursive: Determines whether to enumerate folders recursively
"""
return_type = EntityCollection(self.context, DriveItem)
return_type = EntityCollection(self.context, DriveItem, self.resource_path)

def _get_files(parent_drive_item):
# type: (DriveItem) -> None
Expand Down
2 changes: 1 addition & 1 deletion office365/onedrive/internal/paths/children.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ def collection(self):
if isinstance(self.parent, EntityPath):
self._collection = self.parent.collection
else:
self._collection = self.parent.parent
self._collection = self.parent
return self._collection
4 changes: 1 addition & 3 deletions office365/runtime/paths/resource_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ def __init__(self, key=None, parent=None):
self._key = key
self._parent = parent

def patch(self, key, inplace=False, path_type=None):
def patch(self, key):
if self._key is None:
self._key = key
if path_type:
self.__class__ = path_type
return self

def __iter__(self):
Expand Down
22 changes: 12 additions & 10 deletions office365/runtime/paths/v4/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,25 @@ def __init__(self, key=None, parent=None, collection=None):

@property
def collection(self):
from office365.onedrive.internal.paths.children import ChildrenPath

if self._collection is None:
if isinstance(self.parent, ChildrenPath):
self._collection = self.parent.collection
else:
self._collection = self.parent
return self._collection

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

def patch(self, key, inplace=False):
def patch(self, key):
"""
Patches path
:type key: str or None
:type inplace: bool
"""
if inplace:
self._key = key
self._parent = self.collection
self.__class__ = ResourcePath
return self
else:
return ResourcePath(key, self.collection)
self._key = key
self._parent = self.collection
self.__class__ = EntityPath
return self
9 changes: 9 additions & 0 deletions office365/sharepoint/entity_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from office365.runtime.client_object_collection import ClientObjectCollection
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.paths.v3.entity import EntityPath
from office365.sharepoint.entity import Entity

if TYPE_CHECKING:
Expand All @@ -21,6 +22,14 @@ def __init__(self, context, item_type, resource_path=None, parent=None):
context, item_type, resource_path, parent
)

def create_typed_object(self, initial_properties=None, resource_path=None):
# type: (Optional[dict], Optional[ResourcePath]) -> T
if resource_path is None:
resource_path = EntityPath(None, self.resource_path)
return super(EntityCollection, self).create_typed_object(
initial_properties, resource_path
)

@property
def context(self):
# type: () -> ClientContext
Expand Down
2 changes: 1 addition & 1 deletion office365/sharepoint/files/versions/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,5 @@ def set_property(self, key, value, persist_changes=True):
value, self.parent_collection.resource_path
)
else:
self._resource_path.patch(value, path_type=EntityPath)
self._resource_path.patch(value)
return self
2 changes: 1 addition & 1 deletion office365/sharepoint/listitems/listitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ def set_property(self, name, value, persist_changes=True):
value, self.parent_collection.resource_path
)
else:
self._resource_path.patch(value, path_type=EntityPath)
self._resource_path.patch(value)
return self

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

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 +137,5 @@ def set_property(self, name, value, persist_changes=True):
value
).resource_path
else:
self._resource_path.patch(value, path_type=EntityPath)
self._resource_path.patch(value)
return super(SitePageMetadata, self).set_property(name, value, persist_changes)
2 changes: 1 addition & 1 deletion office365/sharepoint/recyclebin/item.py
Original file line number Diff line number Diff line change
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=EntityPath)
self._resource_path.patch(value)
8 changes: 8 additions & 0 deletions office365/sharepoint/webs/collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.sharepoint.entity_collection import EntityCollection
from office365.sharepoint.internal.paths.web import WebPath
from office365.sharepoint.webs.web import Web


Expand All @@ -25,6 +26,13 @@ def add(self, web_creation_information):
self.context.add_query(qry)
return return_type

def create_typed_object(self, initial_properties=None, resource_path=None):
if resource_path is None:
resource_path = WebPath(self.resource_path)
return super(EntityCollection, self).create_typed_object(
initial_properties, resource_path
)

@property
def resource_url(self):
val = super(WebCollection, self).resource_url
Expand Down
4 changes: 2 additions & 2 deletions office365/sharepoint/webs/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def __init__(self, context, resource_path=None):
:type context: office365.sharepoint.client_context.ClientContext
"""
if resource_path is None:
resource_path = ResourcePath("Web")
resource_path = WebPath("Web")
super(Web, self).__init__(context, resource_path)
self._web_url = None

Expand Down Expand Up @@ -2452,7 +2452,7 @@ def set_property(self, name, value, persist_changes=True):
super(Web, self).set_property(name, value, persist_changes)
if name == "Url":
self._web_url = value
self._resource_path.patch(value, path_type=WebPath)
self._resource_path.patch(value)
return self

@property
Expand Down
6 changes: 3 additions & 3 deletions tests/test_graph_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ def test5_resolve_drive_url_path(self):
parent_path = self.client.me.drive.root.resource_path
path = UrlPath("Sample.docx", UrlPath("2018", UrlPath("archive", parent_path)))
item_id = uuid.uuid4().hex
path.patch(item_id, inplace=True)
path.patch(item_id)
self.assertEqual(f"/me/drive/items/{item_id}", str(path))

def test6_resolve_drive_children_path(self):
path = self.client.me.drive.root.children.resource_path
item_id = uuid.uuid4().hex
path.patch(item_id, inplace=True)
path.patch(item_id)
self.assertEqual(f"/me/drive/items/{item_id}", str(path))

def test7_build_drive_children_path(self):
Expand All @@ -66,7 +66,7 @@ def test8_resolve_site_url_path(self):
def test9_resolve_drive_root_path(self):
path = self.client.me.drive.root.resource_path
item_id = uuid.uuid4().hex
path.patch(item_id, inplace=True)
path.patch(item_id)
self.assertEqual(f"/me/drive/items/{item_id}", str(path))

def test_10_build_site_root_path(self):
Expand Down

0 comments on commit 480f825

Please sign in to comment.