Skip to content

Commit

Permalink
Make Resouce implement os.PathLike
Browse files Browse the repository at this point in the history
  • Loading branch information
lieryan committed Jan 3, 2024
1 parent 104ef35 commit b014d95
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions rope/base/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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(
Expand All @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 = []
Expand Down

0 comments on commit b014d95

Please sign in to comment.