Coverage report: - 95% + 66%
Coverage report:
coverage.py v7.3.2, - created at 2023-11-17 19:27 +0000 + created at 2023-11-21 19:45 +0000
From e703d25dd1549a0f31fcdb86fd32d0a848cbe6fc Mon Sep 17 00:00:00 2001
From: github-actions
- 45 statements
-
+ 47 statements
+
@@ -65,7 +65,7 @@
» next
coverage.py v7.3.2,
- created at 2023-11-17 19:27 +0000
+ created at 2023-11-21 19:45 +0000
@@ -65,7 +65,7 @@
1import site
-2from pathlib import Path
-3from typing import List
- -5from otlmow_converter.OtlmowConverter import OtlmowConverter
-6from otlmow_model.Helpers.AssetCreator import dynamic_create_instance_from_uri
-7from otlmow_modelbuilder.OSLOCollector import OSLOCollector
- - -10class SubsetTemplateCreator:
-11 def __init__(self):
-12 pass
- -14 @staticmethod
-15 def _load_collector_from_subset_path(path_to_subset: Path) -> OSLOCollector:
-16 collector = OSLOCollector(path_to_subset)
-17 collector.collect_all(include_abstract=True)
-18 return collector
- -20 def generate_template_from_subset(self, path_to_subset: Path, path_to_template_file_and_extension: Path,
-21 **kwargs):
-22 collector = self._load_collector_from_subset_path(path_to_subset=path_to_subset)
-23 otl_objects = []
- -25 for class_object in list(filter(lambda cl: cl.abstract == 0, collector.classes)):
-26 class_directory = None
-27 if kwargs is not None and 'class_directory' in kwargs:
-28 class_directory = kwargs['class_directory']
-29 instance = dynamic_create_instance_from_uri(class_object.objectUri, directory=class_directory)
-30 if instance is None:
-31 continue
-32 instance._assetId.fill_with_dummy_data()
-33 otl_objects.append(instance)
- -35 attributen = collector.find_attributes_by_class(class_object)
-36 for attribute_object in attributen:
-37 attr = getattr(instance, '_' + attribute_object.name)
-38 attr.fill_with_dummy_data()
-39 converter = OtlmowConverter()
-40 converter.create_file_from_assets(filepath=path_to_template_file_and_extension,
-41 list_of_objects=otl_objects, **kwargs)
- -43 @classmethod
-44 def filters_assets_by_subset(cls, path_to_subset: Path, list_of_otl_objectUri: List):
-45 collector = cls._load_collector_from_subset_path(path_to_subset=path_to_subset)
-46 filtered_list = [x for x in collector.classes if x.objectUri in list_of_otl_objectUri]
-47 return filtered_list
- -49 @staticmethod
-50 def _try_getting_settings_of_converter() -> Path:
-51 converter_path = Path(site.getsitepackages()[0]) / 'otlmow_converter'
-52 return converter_path / 'settings_otlmow_converter.json'
+1import logging
+2import os
+3import site
+4from pathlib import Path
+5from typing import List
+6from openpyxl.reader.excel import load_workbook
+7from openpyxl.styles import PatternFill
+8from openpyxl.utils import get_column_letter
+9from openpyxl.worksheet.dimensions import DimensionHolder, ColumnDimension
+10from otlmow_converter.DotnotationHelper import DotnotationHelper
+ +12from otlmow_converter.OtlmowConverter import OtlmowConverter
+13from otlmow_model.Helpers.AssetCreator import dynamic_create_instance_from_uri
+14from otlmow_modelbuilder.OSLOCollector import OSLOCollector
+ +16ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
+ + +19class SubsetTemplateCreator:
+20 def __init__(self):
+21 pass
+ +23 @staticmethod
+24 def _load_collector_from_subset_path(path_to_subset: Path) -> OSLOCollector:
+25 collector = OSLOCollector(path_to_subset)
+26 collector.collect_all(include_abstract=True)
+27 return collector
+ +29 def generate_template_from_subset(self, path_to_subset: Path, path_to_template_file_and_extension: Path,
+30 **kwargs):
+31 collector = self._load_collector_from_subset_path(path_to_subset=path_to_subset)
+32 otl_objects = []
+ +34 for class_object in list(filter(lambda cl: cl.abstract == 0, collector.classes)):
+35 model_directory = None
+36 if kwargs is not None and 'model_directory' in kwargs:
+37 model_directory = kwargs['model_directory']
+38 instance = dynamic_create_instance_from_uri(class_object.objectUri, model_directory=model_directory)
+39 if instance is None:
+40 continue
+41 instance._assetId.fill_with_dummy_data()
+42 otl_objects.append(instance)
+ +44 attributen = collector.find_attributes_by_class(class_object)
+45 for attribute_object in attributen:
+46 attr = getattr(instance, '_' + attribute_object.name)
+47 attr.fill_with_dummy_data()
+48 converter = OtlmowConverter()
+49 converter.create_file_from_assets(filepath=path_to_template_file_and_extension,
+50 list_of_objects=otl_objects, **kwargs)
+51 path_is_split = kwargs.get('split_per_type', False)
+52 instantiated_attributes = []
+53 if not path_is_split:
+54 instantiated_attributes = converter.create_assets_from_file(filepath=path_to_template_file_and_extension,
+55 path_to_subset=path_to_subset)
+56 self.alter_template(changes=kwargs, path_to_template_file_and_extension=path_to_template_file_and_extension,
+57 path_to_subset=path_to_subset, instantiated_attributes=instantiated_attributes)
+ +59 @classmethod
+60 def alter_template(cls, changes, path_to_template_file_and_extension: Path, path_to_subset: Path,
+61 instantiated_attributes: List):
+62 # use **kwargs to pass changes
+63 generate_choice_list = changes.get('generate_choice_list', False)
+64 add_geo_artefact = changes.get('add_geo_artefact', False)
+65 add_attribute_info = changes.get('add_attribute_info', False)
+66 highlight_deprecated_attributes = changes.get('highlight_deprecated_attributes', False)
+67 amount_of_examples = changes.get('amount_of_examples', 0)
+68 if generate_choice_list:
+69 raise NotImplementedError("generate_choice_list is not implemented yet")
+70 if add_geo_artefact:
+71 raise NotImplementedError("add_geo_artefact is not implemented yet")
+72 if amount_of_examples > 0:
+73 raise NotImplementedError("amount_of_examples is not implemented yet")
+74 if highlight_deprecated_attributes:
+75 cls.check_for_deprecated_attributes(path_to_workbook=path_to_template_file_and_extension,
+76 instantiated_attributes=instantiated_attributes,
+77 path_to_subset=path_to_subset)
+78 if add_attribute_info:
+79 cls.add_attribute_info_excel(path_to_workbook=path_to_template_file_and_extension,
+80 instantiated_attributes=instantiated_attributes)
+ +82 @classmethod
+83 def filters_assets_by_subset(cls, path_to_subset: Path, list_of_otl_objectUri: List):
+84 collector = cls._load_collector_from_subset_path(path_to_subset=path_to_subset)
+85 filtered_list = [x for x in collector.classes if x.objectUri in list_of_otl_objectUri]
+86 return filtered_list
+ +88 @staticmethod
+89 def _try_getting_settings_of_converter() -> Path:
+90 converter_path = Path(site.getsitepackages()[0]) / 'otlmow_converter'
+91 return converter_path / 'settings_otlmow_converter.json'
+ +93 @classmethod
+94 def design_workbook_excel(cls, path_to_workbook: Path):
+95 wb = load_workbook(path_to_workbook)
+96 for sheet in wb:
+97 for rows in sheet.iter_rows(min_row=1, max_row=1):
+98 for cell in rows:
+99 cell.fill = PatternFill(start_color="808080", end_color="808080", fill_type="solid")
+100 dim_holder = DimensionHolder(worksheet=sheet)
+101 for col in range(sheet.min_column, sheet.max_column + 1):
+102 dim_holder[get_column_letter(col)] = ColumnDimension(sheet, min=col, max=col, width=20)
+103 sheet.column_dimensions = dim_holder
+104 wb.save(path_to_workbook)
+ +106 @classmethod
+107 def add_attribute_info_excel(cls, path_to_workbook: Path, instantiated_attributes: List):
+108 dotnotation_module = DotnotationHelper()
+109 workbook = load_workbook(path_to_workbook)
+110 for sheet in workbook:
+111 filter_uri = SubsetTemplateCreator.find_uri_in_sheet(sheet)
+112 single_attribute = [x for x in instantiated_attributes if x.typeURI == filter_uri]
+113 for rows in sheet.iter_rows(min_row=1, max_row=1, min_col=2):
+114 for cell in rows:
+115 dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute[0],
+116 cell.value)
+117 cell.value = dotnotation_attribute.definition + "\n\n" + " " + cell.value
+118 workbook.save(path_to_workbook)
+ +120 @classmethod
+121 def check_for_deprecated_attributes(cls, path_to_workbook: Path, instantiated_attributes: List,
+122 path_to_subset: Path):
+123 dotnotation_module = DotnotationHelper()
+124 workbook = load_workbook(path_to_workbook)
+125 for sheet in workbook:
+126 filter_uri = SubsetTemplateCreator.find_uri_in_sheet(sheet)
+127 single_attribute = [x for x in instantiated_attributes if x.typeURI == filter_uri]
+128 for rows in sheet.iter_rows(min_row=1, max_row=1, min_col=2):
+129 for cell in rows:
+130 is_deprecated = False
+131 if cell.value.count('.') == 1:
+132 dot_split = cell.value.split('.')
+133 attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute[0],
+134 dot_split[0])
+ +136 if len(attribute.deprecated_version) > 0:
+137 is_deprecated = True
+138 dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute[0],
+139 cell.value)
+140 if len(dotnotation_attribute.deprecated_version) > 0:
+141 is_deprecated = True
+ +143 if is_deprecated:
+144 cell.fill = PatternFill(start_color="FF0000", end_color="FF0000", fill_type="solid")
+ +146 workbook.save(path_to_workbook)
+ +148 @classmethod
+149 def find_uri_in_sheet(cls, sheet):
+150 filter_uri = None
+151 for row in sheet.iter_rows(min_row=1, max_row=1):
+152 for cell in row:
+153 if cell.value == 'typeURI':
+154 row_index = cell.row
+155 column_index = cell.column
+156 filter_uri = sheet.cell(row=row_index + 1, column=column_index).value
+157 return filter_uri
+ + +160if __name__ == '__main__':
+161 subset_tool = SubsetTemplateCreator()
+162 subset_location = Path(ROOT_DIR) / 'UnitTests' / 'Subset' / 'Flitspaal_noAgent3.0.db'
+163 print(subset_location)
+164 xls_location = Path(ROOT_DIR) / 'UnitTests' / 'Subset' / 'testFileStorage' / 'template_file.xlsx'
+165 subset_tool.generate_template_from_subset(path_to_subset=subset_location,
+166 path_to_template_file_and_extension=xls_location, add_attribute_info=True,
+167 highlight_deprecated_attributes=True)
coverage.py v7.3.2, - created at 2023-11-17 19:27 +0000 + created at 2023-11-21 19:45 +0000
coverage.py v7.3.2, - created at 2023-11-17 19:27 +0000 + created at 2023-11-21 19:45 +0000