Skip to content

Commit 7239d70

Browse files
authored
optional suffix (#564)
* optional suffix * docstring * suffix defaults to .json * replaces .json with suffix * linters
1 parent f5030f4 commit 7239d70

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/aind_data_schema/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def default_filename(cls):
129129
name = cls._get_direct_subclass(cls).__name__
130130
return re.sub(r"(?<!^)(?=[A-Z])", "_", name).lower() + ".json"
131131

132-
def write_standard_file(self, output_directory: Optional[Path] = None, prefix=None):
132+
def write_standard_file(self, output_directory: Optional[Path] = None, prefix=None, suffix=None):
133133
"""
134134
Writes schema to standard json file
135135
Parameters
@@ -138,13 +138,17 @@ def write_standard_file(self, output_directory: Optional[Path] = None, prefix=No
138138
optional Path object for output directory
139139
prefix:
140140
optional str for intended filepath with extra naming convention
141-
141+
suffix:
142+
optional str for intended filepath with extra naming convention
142143
"""
143144
if prefix is None:
144145
filename = self.default_filename()
145146
else:
146147
filename = str(prefix) + "_" + self.default_filename()
147148

149+
if suffix:
150+
filename = filename.replace(".json", suffix)
151+
148152
if output_directory is not None:
149153
output_directory = Path(output_directory)
150154
filename = output_directory / filename

tests/test_base.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def test_default_filename(self, mock_log):
5151
)
5252

5353
@patch("builtins.open", new_callable=unittest.mock.mock_open())
54-
def test_write_standard_file_no_prefix(self, mock_open):
55-
"""tests that standard file is named and written as expected with no prefix"""
54+
def test_write_standard_file_no_prefix_suffix(self, mock_open):
55+
"""tests that standard file is named and written as expected with no prefix or suffix"""
5656
p = Procedures.construct()
5757
default_filename = p.default_filename()
5858
json_contents = p.json(indent=3)
@@ -77,6 +77,21 @@ def test_write_standard_file_with_prefix(self, mock_open):
7777
mock_open.assert_called_once_with(expected_file_path, "w")
7878
mock_open.return_value.__enter__().write.assert_called_once_with(json_contents)
7979

80+
@patch("builtins.open", new_callable=unittest.mock.mock_open())
81+
def test_write_standard_file_with_suffix(self, mock_open):
82+
"""tests that standard file is named and written as expected with filename suffix"""
83+
p = Procedures.construct()
84+
json_contents = p.json(indent=3)
85+
suffix = ".aind.json"
86+
p.write_standard_file(suffix=suffix)
87+
88+
# It's expected that the file will be written to something like
89+
# procedure.aind.json
90+
expected_file_path = "procedures.aind.json"
91+
92+
mock_open.assert_called_once_with(expected_file_path, "w")
93+
mock_open.return_value.__enter__().write.assert_called_once_with(json_contents)
94+
8095
@patch("builtins.open", new_callable=unittest.mock.mock_open())
8196
def test_write_standard_file_with_output_directory(self, mock_open):
8297
"""tests that standard file is named and written as expected with designated output directory"""

0 commit comments

Comments
 (0)