-
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.
- Loading branch information
1 parent
891689f
commit aefe66e
Showing
18 changed files
with
518 additions
and
152 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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,80 @@ | ||
import os | ||
|
||
import click | ||
from openpyxl import Workbook | ||
|
||
from bam_masterdata.cli.entities_to_excel import entities_to_excel | ||
from bam_masterdata.cli.entities_to_json import entities_to_json | ||
from bam_masterdata.logger import logger | ||
from bam_masterdata.utils import ( | ||
delete_and_create_dir, | ||
import_module, | ||
listdir_py_modules, | ||
) | ||
|
||
|
||
@click.group(help='Entry point to run `bam_masterdata` CLI commands.') | ||
def cli(): | ||
pass | ||
|
||
|
||
@cli.command(help='Export entities to JSON files to the `./artifacts/` folder.') | ||
def export_entities_to_json(): | ||
# Get the directories from the Python modules and the export directory for the static artifacts | ||
datamodel_dir = os.path.join('.', 'bam_masterdata', 'datamodel') | ||
export_dir = os.path.join('.', 'artifacts') | ||
|
||
# Delete and create the export directory | ||
delete_and_create_dir(directory_path=export_dir, logger=logger) | ||
|
||
# Get the Python modules to process the datamodel | ||
py_modules = listdir_py_modules(directory_path=datamodel_dir, logger=logger) | ||
|
||
# Process each module using the `to_json` method of each entity | ||
for module_path in py_modules: | ||
entities_to_json(module_path=module_path, export_dir=export_dir, logger=logger) | ||
|
||
click.echo(f'All entity artifacts have been generated and saved to {export_dir}') | ||
|
||
|
||
@cli.command( | ||
help=""" | ||
Export entities to an Excel file in the path `./artifacts/masterdata.xlsx`. | ||
""", | ||
) | ||
def export_entities_to_excel(): | ||
# Get the Python modules to process the datamodel | ||
datamodel_dir = os.path.join('.', 'bam_masterdata', 'datamodel') | ||
py_modules = listdir_py_modules(directory_path=datamodel_dir, logger=logger) | ||
|
||
# Load the definitions module classes | ||
definitions_module = import_module( | ||
module_path='./bam_masterdata/metadata/definitions.py' | ||
) | ||
|
||
# Process the modules and save the entities to the openBIS masterdata Excel file | ||
masterdata_file = os.path.join('.', 'artifacts', 'masterdata.xlsx') | ||
wb = Workbook() | ||
for i, module_path in enumerate(py_modules): | ||
if i == 0: | ||
ws = wb.active | ||
else: | ||
ws = wb.create_sheet() | ||
ws.title = ( | ||
os.path.basename(module_path) | ||
.capitalize() | ||
.replace('.py', '') | ||
.replace('_', ' ') | ||
) | ||
entities_to_excel( | ||
worksheet=ws, | ||
module_path=module_path, | ||
definitions_module=definitions_module, | ||
) | ||
wb.save(masterdata_file) | ||
|
||
click.echo(f'All masterdata have been generated and saved to {masterdata_file}') | ||
|
||
|
||
if __name__ == '__main__': | ||
cli() |
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,75 @@ | ||
import inspect | ||
from typing import TYPE_CHECKING, Any | ||
|
||
if TYPE_CHECKING: | ||
from openpyxl.worksheet.worksheet import Worksheet | ||
|
||
from bam_masterdata.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 |
Oops, something went wrong.
aefe66e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report