From b014d9564ea0067a12f99caf601aa539a10b40b6 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Wed, 3 Jan 2024 12:37:24 +1100 Subject: [PATCH] Make Resouce implement os.PathLike --- rope/base/resources.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/rope/base/resources.py b/rope/base/resources.py index bc8c42724..cb975bde1 100644 --- a/rope/base/resources.py +++ b/rope/base/resources.py @@ -34,7 +34,7 @@ from rope.base import change, exceptions, fscommands -class Resource: +class Resource(os.PathLike): """Represents files and folders in a project""" def __init__(self, project, path): @@ -49,6 +49,10 @@ def __repr__(self): hex(id(self)), ) + def __fspath__(self) -> str: + """Return the file system path of this resource""" + return self.project._get_resource_path(self.path) + def move(self, new_location): """Move resource to `new_location`""" self._perform_change( @@ -67,7 +71,7 @@ def create(self): """Create this resource""" def exists(self): - return os.path.exists(self.real_path) + return os.path.exists(self) @property def parent(self): @@ -90,13 +94,12 @@ def name(self) -> str: @property def real_path(self) -> str: - """Return the file system path of this resource""" - return self.project._get_resource_path(self.path) + return os.fspath(self) @property def pathlib(self) -> Path: """Return the file as a pathlib path.""" - return Path(self.real_path) + return Path(self) def __eq__(self, obj): return self.__class__ == obj.__class__ and self.path == obj.path @@ -135,7 +138,7 @@ def read_bytes(self): DeprecationWarning, stacklevel=2, ) - with open(self.real_path, "rb") as handle: + with open(self, "rb") as handle: return handle.read() return self.project.fscommands.read(self.real_path) @@ -165,7 +168,7 @@ def is_folder(self): def get_children(self): """Return the children of this folder""" try: - children = os.listdir(self.real_path) + children = os.listdir(self) except OSError: return [] result = []