mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-19 22:25:44 +08:00
Changing filepaths to Path obj (#303)
changing from using strings to paths
This commit is contained in:
1 parent
2d1ba13e12
commit
8f42ce4b34
6 files changed
+70
-92
No files matched your search
+16
-39
@@ -19,51 +19,52 @@ import json
|
||||
import logging
|
||||
import os
|
||||
import pickle
|
||||
from pathlib import Path
|
||||
from typing import Any, Tuple
|
||||
|
||||
|
||||
def load_from_json(filename: str):
|
||||
def load_from_json(filename: Path):
|
||||
"""Load a dictionary from a JSON filename.
|
||||
|
||||
Args:
|
||||
filename: The filename to load from.
|
||||
"""
|
||||
assert filename.endswith(".json")
|
||||
assert filename.suffix == ".json"
|
||||
with open(filename, encoding="UTF-8") as file:
|
||||
return json.load(file)
|
||||
|
||||
|
||||
def write_to_json(filename: str, content: dict):
|
||||
def write_to_json(filename: Path, content: dict):
|
||||
"""Write data to a JSON file.
|
||||
|
||||
Args:
|
||||
filename: The filename to write to.
|
||||
content: The dictionary data to write.
|
||||
"""
|
||||
assert filename.endswith(".json")
|
||||
assert filename.suffix == ".json"
|
||||
with open(filename, "w", encoding="UTF-8") as file:
|
||||
json.dump(content, file)
|
||||
|
||||
|
||||
def load_from_pkl(filename: str):
|
||||
def load_from_pkl(filename: Path):
|
||||
"""Load from a pickle file.
|
||||
|
||||
Args:
|
||||
filename: The filename to load from.
|
||||
"""
|
||||
assert filename.endswith(".pkl")
|
||||
assert filename.suffix == ".pkl"
|
||||
with open(filename, "rb") as f:
|
||||
return pickle.load(f)
|
||||
|
||||
|
||||
def write_to_pkl(filename: str, content: Any):
|
||||
def write_to_pkl(filename: Path, content: Any):
|
||||
"""Write to a pickle file.
|
||||
|
||||
Args:
|
||||
filename (str): The filename to write to.
|
||||
content (Any): The data to write.
|
||||
"""
|
||||
assert filename.endswith(".pkl")
|
||||
assert filename.suffix == ".pkl"
|
||||
with open(filename, "wb") as f:
|
||||
pickle.dump(content, f)
|
||||
|
||||
@@ -89,7 +90,7 @@ def get_git_root(path: str, dirs: Tuple[str] = (".git",), default: str = "") ->
|
||||
return default
|
||||
|
||||
|
||||
def get_project_root(path: str) -> str:
|
||||
def get_project_root(path: str) -> Path:
|
||||
"""Return the project root directory from an environment variable.
|
||||
# TODO: handle this better and report user error
|
||||
|
||||
@@ -106,40 +107,16 @@ def get_project_root(path: str) -> str:
|
||||
"Going to try calling get_git_root(path) instead."
|
||||
)
|
||||
project_root = get_git_root(path)
|
||||
return project_root
|
||||
return Path(project_root)
|
||||
|
||||
|
||||
def get_absolute_path(path, proj_root_func=get_project_root):
|
||||
def get_absolute_path(path, proj_root_func=get_project_root) -> Path:
|
||||
"""
|
||||
Returns the full, absolute path.
|
||||
Relative paths are assumed to start at the repo directory.
|
||||
"""
|
||||
if path == "":
|
||||
return ""
|
||||
absolute_path = path
|
||||
if absolute_path[0] != "/":
|
||||
absolute_path = os.path.join(proj_root_func(path), absolute_path)
|
||||
str_absolute_path = str(path)
|
||||
if str_absolute_path == "" or str_absolute_path[0] == "/":
|
||||
return path
|
||||
absolute_path = proj_root_func(str_absolute_path) / path
|
||||
return absolute_path
|
||||
|
||||
|
||||
def make_dir(filename_or_folder: str) -> str:
|
||||
"""Make the directory for either the filename or folder.
|
||||
Note that filename_or_folder currently needs to end in / for it to be recognized as a folder.
|
||||
|
||||
Args:
|
||||
filename_or_folder (str): The filename or folder to make.
|
||||
|
||||
Returns:
|
||||
The filename_or_folder, which is the input.
|
||||
"""
|
||||
if filename_or_folder[-1] != "/" and filename_or_folder.find(".") < 0:
|
||||
folder = os.path.dirname(filename_or_folder + "/")
|
||||
else:
|
||||
folder = os.path.dirname(filename_or_folder)
|
||||
if not os.path.exists(folder):
|
||||
try:
|
||||
os.makedirs(folder)
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
print(f"Couldn't create folder: {folder}. Maybe due to a parallel process?")
|
||||
print(e)
|
||||
return filename_or_folder
|
||||
Reference in new issue
Block a user