Skip to content

Commit

Permalink
[#50739] rpath: Load files eagerly
Browse files Browse the repository at this point in the history
Signed-off-by: Mikolaj Klikowicz <[email protected]>
  • Loading branch information
mikolaj-klikowicz committed Oct 31, 2023
1 parent cb06423 commit 2b624b3
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/pyrenode3/rpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,45 @@ class RPath:
"""A class used for fetching files and converting paths."""

def __init__(self, location: "Union[str, Path]"):
self.__location = location
self.__result = self.__fetch(location)

@property
def path(self) -> str:
return str(self.__fetch())
return str(self.__result)

@property
def read_file_path(self) -> "ReadFilePath":
return ReadFilePath(str(self.__fetch()))
return ReadFilePath(str(self.__result))

@classmethod
def in_root(cls):
return RenodeLoader().in_root()

def __fetch(self):
if isinstance(self.__location, Path):
return self.__fetch_local()

scheme = urlparse(self.__location).scheme
if scheme in ["http", "https"]:
return self.__fetch_http()

return self.__fetch_local()

def __fetch_http(self):
@classmethod
def __fetch_http(cls, uri: str):
fetcher = wrappers.Emulation().FileFetcher
res, filename = fetcher.TryFetchFromUri(Uri(self.__location))
res, filename = fetcher.TryFetchFromUri(Uri(uri))
if not res:
msg = f"Downloading from '{self.__location}' failed."
raise ValueError(msg)
msg = f"Downloading from '{uri}' failed."
raise FileNotFoundError(msg)

return Path(filename)

def __fetch_local(self):
res = Path(self.__location)
@classmethod
def __fetch_local(cls, path: "Union[str, Path]"):
res = Path(path)
if not res.exists():
msg = f"'{self.__location}' doesn't exist."
raise ValueError(msg)
msg = f"'{path}' doesn't exist."
raise FileNotFoundError(msg)

return res

def __fetch(self, location: "Union[str, Path]") -> "Path":
if isinstance(location, Path):
return self.__fetch_local(location)

scheme = urlparse(location).scheme
if scheme in ["http", "https"]:
return self.__fetch_http(location)

return self.__fetch_local(location)

0 comments on commit 2b624b3

Please sign in to comment.