Skip to content

Commit 827a887

Browse files
committed
save now just has one filename parameter
1 parent cb5e6b6 commit 827a887

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

RATapi/project.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -837,18 +837,17 @@ def classlist_script(name, classlist):
837837
+ "\n)"
838838
)
839839

840-
def save(self, filepath: Union[str, Path], filename: str = "project"):
840+
def save(self, filepath: Union[str, Path] = "./project.json"):
841841
"""Save a project to a JSON file.
842842
843843
Parameters
844844
----------
845845
filepath : str or Path
846-
The path in which the project will be written.
847-
filename : str
848-
The name of the generated project file.
846+
The path to where the project file will be written.
849847
850848
"""
851-
filepath = Path(filepath)
849+
filepath = Path(filepath).with_suffix(".json")
850+
852851
json_dict = {}
853852
for field in self.model_fields:
854853
attr = getattr(self, field)
@@ -882,8 +881,7 @@ def make_custom_file_dict(item):
882881
else:
883882
json_dict[field] = attr
884883

885-
file = Path(filepath, f"{filename.removesuffix('.json')}.json")
886-
file.write_text(json.dumps(json_dict))
884+
filepath.write_text(json.dumps(json_dict))
887885

888886
@classmethod
889887
def load(cls, path: Union[str, Path]) -> "Project":
@@ -979,17 +977,23 @@ def try_relative_to(path: Path, relative_to: Path) -> Path:
979977
try:
980978
relative_path = abs_path.relative_to(abs_base)
981979
except ValueError as err:
982-
warnings.warn("Could not save a custom file path as relative to the project directory. "
983-
"This may mean the project may not open on other devices. "
984-
f"Error message: {err}", stacklevel=2)
985-
return abs_path
980+
warnings.warn(
981+
"Could not save a custom file path as relative to the project directory. "
982+
"This may mean the project may not open on other devices. "
983+
f"Error message: {err}",
984+
stacklevel=2,
985+
)
986+
return abs_path
986987
else:
987988
try:
988-
relative_path = abs_path.relative_to(abs_base, walk_up = True)
989+
relative_path = abs_path.relative_to(abs_base, walk_up=True)
989990
except ValueError as err:
990-
warnings.warn("Could not save a custom file path as relative to the project directory. "
991-
"This may mean the project may not open on other devices. "
992-
f"Error message: {err}", stacklevel=2)
993-
return abs_path
991+
warnings.warn(
992+
"Could not save a custom file path as relative to the project directory. "
993+
"This may mean the project may not open on other devices. "
994+
f"Error message: {err}",
995+
stacklevel=2,
996+
)
997+
return abs_path
994998

995999
return relative_path

tests/test_project.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,10 +1559,11 @@ def test_save_load(project, request):
15591559

15601560
with tempfile.TemporaryDirectory() as tmp:
15611561
# ignore relative path warnings
1562+
path = Path(tmp, "project.json")
15621563
with warnings.catch_warnings():
15631564
warnings.simplefilter("ignore")
1564-
original_project.save(tmp)
1565-
converted_project = RATapi.Project.load(Path(tmp, "project.json"))
1565+
original_project.save(path)
1566+
converted_project = RATapi.Project.load(path)
15661567

15671568
# resolve custom files in case the original project had unresolvable relative paths
15681569
for file in original_project.custom_files:
@@ -1583,16 +1584,18 @@ def test_relative_paths():
15831584

15841585
def test_relative_paths_version():
15851586
"""Test that we only walk up paths on Python 3.12 or greater."""
1586-
1587+
15871588
data_path = "/tmp/project/data/mydata.dat"
15881589
relative_path = "/tmp/project/project_path/myproj.dat"
15891590

15901591
if version_info.minor >= 12:
15911592
with warnings.catch_warnings():
15921593
warnings.simplefilter("error")
15931594
assert RATapi.project.try_relative_to(data_path, relative_path) == Path("../../data/mydata.dat")
1594-
else:
1595-
with pytest.warns(match="Could not save a custom file path as relative to the project directory. "
1596-
"This may mean the project may not open on other devices. "
1597-
"Error message:"):
1595+
else:
1596+
with pytest.warns(
1597+
match="Could not save a custom file path as relative to the project directory. "
1598+
"This may mean the project may not open on other devices. "
1599+
"Error message:"
1600+
):
15981601
assert RATapi.project.try_relative_to(data_path, relative_path) == Path("/tmp/project/data/mydata.dat")

0 commit comments

Comments
 (0)