Skip to content

Commit 2c64250

Browse files
Add configurable collection type for parser with COLLECTION as default (#222)
* Initial plan * Add collection_type parameter to parser infrastructure - Add collection_type parameter to run_parser() with default value "COLLECTION" - Add --collection-type CLI option with choices between COLLECTION and DEFAULT_EXPERIMENT - Update tests to cover both collection types and default behavior - Update documentation to explain the new parameter --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: JosePizarro3 <[email protected]>
1 parent 9cef71d commit 2c64250

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

bam_masterdata/cli/cli.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,14 @@ def push_to_openbis(file_path, datamodel_path):
617617
required=False,
618618
help="OpenBIS space name",
619619
)
620-
def parser(files_parser, project_name, collection_name, space_name):
620+
@click.option(
621+
"--collection-type",
622+
"collection_type", # alias
623+
type=click.Choice(["COLLECTION", "DEFAULT_EXPERIMENT"], case_sensitive=False),
624+
default="COLLECTION",
625+
help='Type of collection to create in openBIS. Options are "COLLECTION" or "DEFAULT_EXPERIMENT". Defaults to "COLLECTION".',
626+
)
627+
def parser(files_parser, project_name, collection_name, space_name, collection_type):
621628
parser_map = {} # TODO load from configuration from yaml file
622629
parse_file_dict = {}
623630
for parser_key, filepath in files_parser:
@@ -636,6 +643,7 @@ def parser(files_parser, project_name, collection_name, space_name):
636643
project_name=project_name,
637644
collection_name=collection_name,
638645
files_parser=parse_file_dict,
646+
collection_type=collection_type.upper(),
639647
)
640648

641649

bam_masterdata/cli/run_parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def run_parser(
1515
project_name: str = "PROJECT",
1616
collection_name: str = "",
1717
files_parser: dict[AbstractParser, list[str]] = {},
18+
collection_type: str = "COLLECTION",
1819
) -> None:
1920
"""
2021
Run the parsers on the specified files and collect the results.
@@ -26,6 +27,7 @@ def run_parser(
2627
project_name (str): The project in openBIS where the entities will be stored.
2728
collection_name (str): The collection in openBIS where the entities will be stored.
2829
files_parser (dict): A dictionary where keys are parser instances and values are lists of file paths to be parsed. E.g., {MasterdataParserExample(): ["path/to/file.json", "path/to/another_file.json"]}
30+
collection_type (str): The type of collection to create in openBIS. Options are "COLLECTION" or "DEFAULT_EXPERIMENT". Defaults to "COLLECTION".
2931
"""
3032
# Ensure openbis is provided
3133
if openbis is None:
@@ -91,7 +93,7 @@ def run_parser(
9193
logger.info("Replacing collection code with uppercase and underscores.")
9294
collection_openbis = openbis.new_collection(
9395
code=collection_name.replace(" ", "_").upper(),
94-
type="DEFAULT_EXPERIMENT",
96+
type=collection_type,
9597
project=project,
9698
)
9799
collection_openbis.save()

docs/tutorials/parsing.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ The whole process then depends on the inputs and output `run_parser()`:
5454
- `space_name`: _string_, this coincides with the space name you have associated in your openBIS instance
5555
- `project_name`: _string_, the name of the project where you want to store the parsed objects
5656
- `collection_name`: _string_, optional, the name of the collection you want to store the parsed objects
57+
- `collection_type`: _string_, optional, the type of collection to create in openBIS. Options are `"COLLECTION"` or `"DEFAULT_EXPERIMENT"`. Defaults to `"COLLECTION"`.
5758
- `files_parser`: _dict_, a dictionary where the keys are the parser class instances (see below) and the values are the files you want to pass to the specific parser class
5859
- **Output**: the metadata objects and relationships mapped to your space/project/collection in openBIS
5960

@@ -193,10 +194,18 @@ run_parser(
193194
space_name="MY_RESEARCH_SPACE",
194195
project_name="MY_PROJECT",
195196
collection_name="MY_EXPERIMENT_COLLECTION",
196-
files_parser=files_parser
197+
files_parser=files_parser,
198+
collection_type="COLLECTION" # Optional: defaults to "COLLECTION"
197199
)
198200
```
199201

202+
!!! tip "Collection Types"
203+
You can choose between two collection types:
204+
- `"COLLECTION"` (default): A general-purpose collection type
205+
- `"DEFAULT_EXPERIMENT"`: A collection type designed for experiments with additional metadata fields like start/end dates, experimental goals, etc.
206+
207+
If you don't specify `collection_type`, it defaults to `"COLLECTION"`.
208+
200209
### What Happens Next?
201210

202211
When you call `run_parser()`, the following steps occur automatically:
@@ -235,7 +244,8 @@ run_parser(
235244
space_name="MY_RESEARCH_SPACE",
236245
project_name="MY_PROJECT",
237246
collection_name="MULTI_DATA_COLLECTION",
238-
files_parser=files_parser
247+
files_parser=files_parser,
248+
collection_type="DEFAULT_EXPERIMENT" # Use DEFAULT_EXPERIMENT for experiment data
239249
)
240250
```
241251

@@ -456,7 +466,8 @@ def main():
456466
space_name="RESEARCH_SPACE",
457467
project_name="AUTOMATION_TEST",
458468
collection_name="BATCH_EXPERIMENTS",
459-
files_parser=files_parser
469+
files_parser=files_parser,
470+
collection_type="DEFAULT_EXPERIMENT"
460471
)
461472

462473
print("Metadata injection complete!")

tests/cli/test_run_parser.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def test_run_parser_with_test_parser(cleared_log_storage, mock_openbis):
6767
project_name="TEST_PROJECT",
6868
collection_name="TEST_COLLECTION",
6969
files_parser=files_parser,
70+
collection_type="COLLECTION",
7071
)
7172

7273
# Check that objects were created in openbis
@@ -105,6 +106,7 @@ def get_object_mock(path):
105106
project_name="TEST_PROJECT",
106107
collection_name="TEST_COLLECTION",
107108
files_parser=files_parser,
109+
collection_type="COLLECTION",
108110
)
109111

110112
# Only 2 instruments are created (the person is referenced but not persisted)
@@ -122,4 +124,44 @@ def get_object_mock(path):
122124
)
123125

124126

127+
def test_run_parser_with_default_experiment_type(cleared_log_storage, mock_openbis):
128+
"""Test run_parser with DEFAULT_EXPERIMENT collection type."""
129+
file = "./tests/data/cli/test_parser.txt"
130+
files_parser = {TestParser(): [file]}
131+
run_parser(
132+
openbis=mock_openbis,
133+
space_name="USERNAME_SPACE",
134+
project_name="TEST_PROJECT",
135+
collection_name="TEST_COLLECTION",
136+
files_parser=files_parser,
137+
collection_type="DEFAULT_EXPERIMENT",
138+
)
139+
140+
# Check that objects were created in openbis
141+
assert len(mock_openbis._objects) == 1
142+
143+
# Check logs for success messages
144+
assert any("Added test object" in log["event"] for log in cleared_log_storage)
145+
146+
147+
def test_run_parser_defaults_to_collection_type(cleared_log_storage, mock_openbis):
148+
"""Test that run_parser defaults to COLLECTION type when not specified."""
149+
file = "./tests/data/cli/test_parser.txt"
150+
files_parser = {TestParser(): [file]}
151+
# Call without specifying collection_type - should default to "COLLECTION"
152+
run_parser(
153+
openbis=mock_openbis,
154+
space_name="USERNAME_SPACE",
155+
project_name="TEST_PROJECT",
156+
collection_name="TEST_COLLECTION",
157+
files_parser=files_parser,
158+
)
159+
160+
# Check that objects were created in openbis
161+
assert len(mock_openbis._objects) == 1
162+
163+
# Check logs for success messages
164+
assert any("Added test object" in log["event"] for log in cleared_log_storage)
165+
166+
125167
# TODO add other tests for the different situations in `run_parser()` and parsers from `conftest.py`

0 commit comments

Comments
 (0)