Skip to content

Commit

Permalink
implement template to pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
Gordon Blackadder committed Aug 26, 2024
1 parent 5db1610 commit 8acb008
Show file tree
Hide file tree
Showing 6 changed files with 2,115 additions and 18 deletions.
4 changes: 2 additions & 2 deletions pydantic_schemas/generators/generate_excel_files.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os

from pydantic_schemas.excel_interface import ExcelInterface
from pydantic_schemas.schema_interface import SchemaInterface

ei = ExcelInterface()
ei = SchemaInterface()

for metadata_type in ei.get_metadata_types():
filename = f"excel_sheets/{metadata_type.capitalize()}_metadata.xlsx"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Dict, Optional

from openpyxl import load_workbook
from pydantic import BaseModel
Expand All @@ -17,13 +17,16 @@
from .utils.excel_to_pydantic import excel_doc_to_pydantic, excel_single_sheet_to_pydantic
from .utils.pydantic_to_excel import write_across_many_sheets, write_to_single_sheet
from .utils.quick_start import make_skeleton
from .utils.template_to_pydantic import pydantic_from_template
from .utils.utils import standardize_keys_in_dict


class ExcelInterface:
class SchemaInterface:
"""
An Excel interface creating, saving and updating metadata for various types:
Interface with Excel for creating, saving and updating metadata for various types:
documents, scripts, series, survey, table, timeseries, timeseries_db, video
Retrieve pydantic model definitions for each metadata type
"""

_TYPE_TO_SCHEMA = {
Expand Down Expand Up @@ -65,6 +68,20 @@ class ExcelInterface:
"video": excel_single_sheet_to_pydantic, # one sheet
}

def get_metadata_class(self, metadata_type: str):
metadata_type = self._process_metadata_type(metadata_type)
if metadata_type not in self._TYPE_TO_SCHEMA:
raise NameError(f"{metadata_type} not known, must be one of {list(self._TYPE_TO_SCHEMA.keys())}.")
schema = self._TYPE_TO_SCHEMA[metadata_type]
return schema

def template_to_pydantic(self, template: Dict, parent_schema_type: str, name: Optional[str] = None) -> BaseModel:
# metadata_type = self._process_metadata_type(parent_schema_type)
# schema = self._TYPE_TO_SCHEMA[metadata_type]
schema = self.get_metadata_class(parent_schema_type)

return pydantic_from_template(template, schema, name)

def get_metadata_types(self):
return list(self._TYPE_TO_READER.keys())

Expand All @@ -78,7 +95,7 @@ def _merge_dicts(base, update):
update_value = update[key]
if isinstance(base_value, dict):
if isinstance(update_value, dict) and len(update_value) > 0:
new_dict[key] = ExcelInterface._merge_dicts(base_value, update_value)
new_dict[key] = SchemaInterface._merge_dicts(base_value, update_value)
else:
new_dict[key] = base_value
elif isinstance(base_value, list):
Expand All @@ -88,7 +105,7 @@ def _merge_dicts(base, update):
for i in range(min_length):
if isinstance(base_value[i], dict):
if isinstance(update_value[i], dict):
new_list.append(ExcelInterface._merge_dicts(base_value[i], update_value[i]))
new_list.append(SchemaInterface._merge_dicts(base_value[i], update_value[i]))
else:
new_list.append(base_value[i])
else:
Expand All @@ -114,8 +131,7 @@ def _process_metadata_type(metadata_type: str) -> str:
return metadata_type

def type_to_outline(self, metadata_type: str, debug: bool = False) -> BaseModel:
metadata_type = self._process_metadata_type(metadata_type)
schema = self._TYPE_TO_SCHEMA[metadata_type]
schema = self.get_metadata_class(metadata_type)
skeleton_object = make_skeleton(schema, debug=debug)
return skeleton_object

Expand Down
4 changes: 2 additions & 2 deletions pydantic_schemas/tests/test_excel_interface.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import pytest

from pydantic_schemas.excel_interface import ExcelInterface
from pydantic_schemas.schema_interface import SchemaInterface


@pytest.mark.parametrize(
"metadata_type", ["document", "script", "series", "survey", "table", "timeseries_db", "timeseries", "video"]
)
def test_metadata(tmpdir, metadata_type):
ei = ExcelInterface()
ei = SchemaInterface()

# Write empty metadata
filename = ei.write_outline_metadata_to_excel(
Expand Down
Loading

0 comments on commit 8acb008

Please sign in to comment.