Skip to content

Commit

Permalink
Fix rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
JosePizarro3 committed Dec 18, 2024
1 parent 891689f commit aefe66e
Show file tree
Hide file tree
Showing 18 changed files with 518 additions and 152 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ cython_debug/
# Ruff
.ruff_cache

# static artifacts
artifacts

# pytest coverage
pytest.xml
pytest-coverage.txt
pytest-coverage.txt

# artifacts
artifacts
13 changes: 11 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,23 @@
]
},
{
"name": "bds export-entities-to-json",
"name": "bds export-to-json",
"type": "debugpy",
"request": "launch",
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/.venv/bin/bam_data_store",
"program": "${workspaceFolder}/.venv/bin/bam_masterdata",
"justMyCode": false,
"args": ["export-entities-to-json"]
},
{
"name": "bds export-to-excel",
"type": "debugpy",
"request": "launch",
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/.venv/bin/bam_masterdata",
"justMyCode": false,
"args": ["export-entities-to-excel"]
},
]
},
}
34 changes: 0 additions & 34 deletions bam_data_store/cli/cli.py

This file was deleted.

52 changes: 0 additions & 52 deletions bam_data_store/cli/entities_to_json.py

This file was deleted.

1 change: 0 additions & 1 deletion bam_data_store/utils/__init__.py

This file was deleted.

53 changes: 0 additions & 53 deletions bam_data_store/utils/utils.py

This file was deleted.

File renamed without changes.
80 changes: 80 additions & 0 deletions bam_masterdata/cli/cli.py
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()
75 changes: 75 additions & 0 deletions bam_masterdata/cli/entities_to_excel.py
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
Loading

1 comment on commit aefe66e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
bam_masterdata
   logger.py80100% 
bam_masterdata/cli
   cli.py363636 0%
   entities_to_excel.py393939 0%
   entities_to_json.py353535 0%
bam_masterdata/datamodel
   collection_types.py777 0%
   dataset_types.py00100% 
   object_types.py111111 0%
   property_types.py222 0%
   vocabulary_types.py141414 0%
bam_masterdata/metadata
   definitions.py7844 95%
   entities.py4533 93%
bam_masterdata/utils
   utils.py331111 67%
TOTAL30816247% 

Tests Skipped Failures Errors Time
50 1 💤 0 ❌ 0 🔥 0.309s ⏱️

Please sign in to comment.