-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix utils testing * Sorting files in utils and testing to avoid weird testing behaviors * Added CLI to export to excel * Fix data type printing to excel column * Added examples for collection type and property type Fix entities-to-json for the special case of PropertyTypeDef * Fixed descriptions * Add skip test
- Loading branch information
1 parent
1cadc6d
commit d1c65b4
Showing
10 changed files
with
256 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import inspect | ||
import os | ||
from typing import TYPE_CHECKING, Any | ||
|
||
if TYPE_CHECKING: | ||
from openpyxl.worksheet.worksheet import Worksheet | ||
|
||
from bam_data_store.utils import import_module | ||
|
||
|
||
def entities_to_excel( | ||
worksheet: 'Worksheet', | ||
module_path: str, | ||
definitions_module: Any, | ||
) -> None: | ||
""" | ||
Export entities to the Excel file. The Python modules are imported using the function `import_module`, | ||
and their contents are inspected (using `inspect`) to find the classes in the datamodel containing | ||
`defs` and with a `to_json` method defined. Each row is then appended to the `worksheet`. | ||
Args: | ||
worksheet (Worksheet): The worksheet to append the entities. | ||
module_path (str): Path to the Python module file. | ||
definitions_module (Any): The module containing the definitions of the entities. This is used | ||
to match the header definitions of the entities. | ||
""" | ||
def_members = inspect.getmembers(definitions_module, inspect.isclass) | ||
module = import_module(module_path=module_path) | ||
for _, obj in inspect.getmembers(module, inspect.isclass): | ||
# Ensure the class has the `to_json` method | ||
if not hasattr(obj, 'defs') or not callable(getattr(obj, 'to_json')): | ||
continue | ||
|
||
obj_instance = obj() | ||
|
||
# Entity title | ||
obj_definitions = obj_instance.defs | ||
worksheet.append([obj_definitions.excel_name]) | ||
|
||
# Entity header definitions and values | ||
for def_name, def_cls in def_members: | ||
if def_name == obj_definitions.name: | ||
break | ||
worksheet.append(obj_definitions.excel_headers) | ||
header_values = [ | ||
getattr(obj_definitions, f_set) for f_set in def_cls.model_fields.keys() | ||
] | ||
worksheet.append(header_values) | ||
|
||
# Properties assignment for ObjectType | ||
if obj_instance.entity_type == 'ObjectType': | ||
if not obj_instance.properties: | ||
continue | ||
worksheet.append(obj_instance.properties[0].excel_headers) | ||
for prop in obj_instance.properties: | ||
row = [] | ||
for f_set in prop.model_fields.keys(): | ||
if f_set == 'data_type': | ||
val = prop.data_type.value | ||
else: | ||
val = getattr(prop, f_set) | ||
row.append(val) | ||
worksheet.append(row) | ||
# Terms assignment for VocabularyType | ||
elif obj_instance.entity_type == 'VocabularyType': | ||
if not obj_instance.terms: | ||
continue | ||
worksheet.append(obj_instance.terms[0].excel_headers) | ||
for term in obj_instance.terms: | ||
worksheet.append( | ||
getattr(term, f_set) for f_set in term.model_fields.keys() | ||
) | ||
|
||
# ? do the PropertyTypeDef need to be exported to Excel? | ||
|
||
worksheet.append(['']) # empty row after entity definitions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from bam_data_store.metadata.definitions import ( | ||
CollectionTypeDef, | ||
PropertyTypeAssignment, | ||
) | ||
from bam_data_store.metadata.entities import CollectionType | ||
|
||
|
||
class DefaultExperiment(CollectionType): | ||
defs = CollectionTypeDef( | ||
version=1, | ||
code='DEFAULT_EXPERIMENT', | ||
description=""" | ||
Default Experiment//Standard-Experiment | ||
""", | ||
) | ||
|
||
name = PropertyTypeAssignment( | ||
version=1, | ||
code='$NAME', | ||
data_type='VARCHAR', | ||
property_label='Name', | ||
description=""" | ||
Name | ||
""", | ||
mandatory=True, | ||
show_in_edit_views=True, | ||
section='General information', | ||
) | ||
|
||
grant = PropertyTypeAssignment( | ||
version=1, | ||
code='DEFAULT_EXPERIMENT.GRANT', | ||
data_type='VARCHAR', | ||
property_label='Grant', | ||
description=""" | ||
Grant | ||
""", | ||
mandatory=False, | ||
show_in_edit_views=True, | ||
section='General information', | ||
) | ||
|
||
experimental_goals = PropertyTypeAssignment( | ||
version=1, | ||
code='DEFAULT_EXPERIMENT.EXPERIMENTAL_GOALS', | ||
data_type='MULTILINE_VARCHAR', | ||
property_label='Goals', | ||
description=""" | ||
Goals of the experiment | ||
""", | ||
mandatory=False, | ||
show_in_edit_views=True, | ||
section='Experimental details', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from bam_data_store.metadata.definitions import PropertyTypeDef | ||
|
||
Name = PropertyTypeDef( | ||
version=1, | ||
code='$NAME', | ||
description=""" | ||
Name | ||
""", | ||
property_label='Name', | ||
data_type='VARCHAR', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters