otlmow-template 0.3__tar.gz → 0.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: otlmow_template
3
- Version: 0.3
4
- Author-email: David Vlaminck <david.vlaminck@mow.vlaanderen.be>
3
+ Version: 0.5
4
+ Author-email: David Vlaminck <david.vlaminck@mow.vlaanderen.be>, Jasper Berton <jasperberton1@telenet.be>
5
5
  License: GNU GENERAL PUBLIC LICENSE
6
6
  Version 3, 29 June 2007
7
7
 
@@ -679,10 +679,11 @@ License: GNU GENERAL PUBLIC LICENSE
679
679
  Project-URL: Homepage, https://github.com/davidvlaminck/OTLMOW-Template
680
680
  Project-URL: Bug Tracker, https://github.com/davidvlaminck/OTLMOW-Template/issues
681
681
  Classifier: Programming Language :: Python :: 3
682
- Classifier: Programming Language :: Python :: 3.8
683
682
  Classifier: Programming Language :: Python :: 3.9
684
683
  Classifier: Programming Language :: Python :: 3.10
685
684
  Classifier: Programming Language :: Python :: 3.11
685
+ Classifier: Programming Language :: Python :: 3.12
686
+ Classifier: Programming Language :: Python :: 3.13
686
687
  Classifier: Operating System :: OS Independent
687
688
  Classifier: Development Status :: 5 - Production/Stable
688
689
  Classifier: Environment :: Console
@@ -693,11 +694,11 @@ Classifier: Natural Language :: Dutch
693
694
  Classifier: Topic :: Software Development
694
695
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
695
696
  Classifier: Topic :: Software Development :: Quality Assurance
696
- Requires-Python: >=3.8
697
+ Requires-Python: >=3.9
697
698
  Description-Content-Type: text/markdown
698
699
  License-File: LICENSE
699
- Requires-Dist: otlmow-converter>=0.14
700
- Requires-Dist: otlmow-modelbuilder>=0.11
700
+ Requires-Dist: otlmow-converter>=0.15
701
+ Requires-Dist: otlmow-modelbuilder>=0.24
701
702
 
702
703
  # OTLMOW-Template
703
704
  [![PyPI](https://img.shields.io/pypi/v/otlmow-template?label=latest%20release)](https://pypi.org/project/otlmow-template/)
@@ -3,6 +3,7 @@ import ntpath
3
3
  import os
4
4
  import tempfile
5
5
  from pathlib import Path
6
+ from typing import List, Sequence, Optional
6
7
 
7
8
  from otlmow_converter.DotnotationHelper import DotnotationHelper
8
9
  from otlmow_converter.OtlmowConverter import OtlmowConverter
@@ -62,8 +63,8 @@ class CsvTemplateCreator:
62
63
  if add_geo_artefact is False:
63
64
  [header, data] = cls.remove_geo_artefact_csv(header=header, data=data)
64
65
  if add_attribute_info:
65
- [info, header] = cls.add_attribute_info_csv(header=header, data=data,
66
- instantiated_attributes=instantiated_attributes)
66
+ info = cls.add_attribute_info_csv(header=header, data=data,
67
+ instantiated_objects=instantiated_attributes)
67
68
  new_file.write(delimiter.join(info) + '\n')
68
69
  data = cls.remove_mock_data_csv(data=data, rows_of_examples=amount_of_examples)
69
70
  if highlight_deprecated_attributes:
@@ -75,30 +76,25 @@ class CsvTemplateCreator:
75
76
  new_file.close()
76
77
 
77
78
  @classmethod
78
- def add_attribute_info_csv(cls, header, data, instantiated_attributes):
79
+ def add_attribute_info_csv(cls, header: List[str], data: List[List[str]], instantiated_objects: List) -> List[str]:
79
80
  info_data = []
80
81
  info_data.extend(header)
81
- found_uri = []
82
+
82
83
  dotnotation_module = DotnotationHelper()
83
- uri_index = cls.find_uri_in_csv(header)
84
- for d in data:
85
- if d[uri_index] not in found_uri:
86
- found_uri.append(d[uri_index])
87
- for uri in found_uri:
88
- single_object = next(x for x in instantiated_attributes if x.typeURI == uri)
89
- for dotnototation_title in info_data:
84
+
85
+ uri_index = cls.get_type_uri_index_in_row(header)
86
+ found_uris = set(d[uri_index] for d in data)
87
+
88
+ for uri in found_uris:
89
+ single_object = next(x for x in instantiated_objects if x.typeURI == uri)
90
+ for index, dotnototation_title in enumerate(info_data):
90
91
  if dotnototation_title == 'typeURI':
91
- index = info_data.index(dotnototation_title)
92
92
  info_data[index] = 'De URI van het object volgens https://www.w3.org/2001/XMLSchema#anyURI .'
93
93
  else:
94
- index = info_data.index(dotnototation_title)
95
- try:
96
- dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(
97
- single_object, dotnototation_title)
98
- except AttributeError as e:
99
- continue
94
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(
95
+ single_object, dotnototation_title)
100
96
  info_data[index] = dotnotation_attribute.definition
101
- return [info_data, header]
97
+ return info_data
102
98
 
103
99
  @classmethod
104
100
  def remove_mock_data_csv(cls, data, rows_of_examples):
@@ -110,7 +106,7 @@ class CsvTemplateCreator:
110
106
  def highlight_deprecated_attributes_csv(cls, header, data, instantiated_attributes):
111
107
  found_uri = []
112
108
  dotnotation_module = DotnotationHelper()
113
- uri_index = cls.find_uri_in_csv(header)
109
+ uri_index = cls.get_type_uri_index_in_row(header)
114
110
  for d in data:
115
111
  if d[uri_index] not in found_uri:
116
112
  found_uri.append(d[uri_index])
@@ -142,11 +138,11 @@ class CsvTemplateCreator:
142
138
  return header
143
139
 
144
140
  @classmethod
145
- def find_uri_in_csv(cls, header):
146
- filter_uri = None
147
- if 'typeURI' in header:
148
- filter_uri = header.index('typeURI')
149
- return filter_uri
141
+ def get_type_uri_index_in_row(cls, header: Sequence[str]) -> Optional[int]:
142
+ try:
143
+ return header.index('typeURI')
144
+ except ValueError:
145
+ return None
150
146
 
151
147
  @classmethod
152
148
  def remove_geo_artefact_csv(cls, header, data):
@@ -0,0 +1,453 @@
1
+ import csv
2
+ import logging
3
+ import ntpath
4
+ import os
5
+ import site
6
+ import tempfile
7
+ from pathlib import Path
8
+
9
+
10
+ from openpyxl.reader.excel import load_workbook
11
+ from openpyxl.styles import PatternFill
12
+ from openpyxl.utils import get_column_letter
13
+ from openpyxl.worksheet.datavalidation import DataValidation
14
+ from openpyxl.worksheet.dimensions import DimensionHolder, ColumnDimension
15
+ from otlmow_converter.DotnotationHelper import DotnotationHelper
16
+ from otlmow_converter.OtlmowConverter import OtlmowConverter
17
+ from otlmow_model.OtlmowModel.BaseClasses.BooleanField import BooleanField
18
+ from otlmow_model.OtlmowModel.BaseClasses.KeuzelijstField import KeuzelijstField
19
+ from otlmow_model.OtlmowModel.BaseClasses.OTLObject import dynamic_create_instance_from_uri
20
+ from otlmow_model.OtlmowModel.Helpers.generated_lists import get_hardcoded_relation_dict
21
+ from otlmow_modelbuilder.OSLOCollector import OSLOCollector
22
+
23
+ ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
24
+
25
+ enumeration_validation_rules = {
26
+ "valid_uri_and_types": {},
27
+ "valid_regexes": [
28
+ "^https://wegenenverkeer.data.vlaanderen.be/ns/.+"]
29
+ }
30
+
31
+
32
+ class SubsetTemplateCreator:
33
+ def __init__(self):
34
+ pass
35
+
36
+ @staticmethod
37
+ def _load_collector_from_subset_path(path_to_subset: Path) -> OSLOCollector:
38
+ collector = OSLOCollector(path_to_subset)
39
+ collector.collect_all(include_abstract=True)
40
+ return collector
41
+
42
+ def generate_template_from_subset(self, path_to_subset: Path, path_to_template_file_and_extension: Path,
43
+ ignore_relations: bool = True, **kwargs):
44
+ tempdir = Path(tempfile.gettempdir()) / 'temp-otlmow'
45
+ if not tempdir.exists():
46
+ os.makedirs(tempdir)
47
+ test = ntpath.basename(path_to_template_file_and_extension)
48
+ temporary_path = Path(tempdir) / test
49
+ instantiated_attributes = self.generate_basic_template(
50
+ path_to_subset=path_to_subset, temporary_path=temporary_path, ignore_relations=ignore_relations,
51
+ path_to_template_file_and_extension=path_to_template_file_and_extension, **kwargs)
52
+ extension = os.path.splitext(path_to_template_file_and_extension)[-1].lower()
53
+ if extension == '.xlsx':
54
+ self.alter_excel_template(path_to_template_file_and_extension=path_to_template_file_and_extension,
55
+ temporary_path=temporary_path,
56
+ path_to_subset=path_to_subset, instantiated_attributes=instantiated_attributes,
57
+ **kwargs)
58
+ elif extension == '.csv':
59
+ self.determine_multiplicity_csv(path_to_template_file_and_extension=path_to_template_file_and_extension,
60
+ path_to_subset=path_to_subset,
61
+ instantiated_attributes=instantiated_attributes,
62
+ temporary_path=temporary_path,
63
+ **kwargs)
64
+
65
+ def generate_basic_template(self, path_to_subset: Path, path_to_template_file_and_extension: Path,
66
+ temporary_path: Path, ignore_relations: bool = True, **kwargs):
67
+ collector = self._load_collector_from_subset_path(path_to_subset=path_to_subset)
68
+ otl_objects = []
69
+ amount_of_examples = kwargs.get('amount_of_examples', 0)
70
+ model_directory = None
71
+ if kwargs is not None:
72
+ model_directory = kwargs.get('model_directory', None)
73
+ relation_dict = get_hardcoded_relation_dict(model_directory=model_directory)
74
+
75
+ for class_object in list(filter(lambda cl: cl.abstract == 0, collector.classes)):
76
+ if ignore_relations and class_object.objectUri in relation_dict:
77
+ continue
78
+
79
+ if amount_of_examples != 0:
80
+ for _ in range(amount_of_examples):
81
+ instance = dynamic_create_instance_from_uri(class_object.objectUri, model_directory=model_directory)
82
+ if instance is None:
83
+ continue
84
+ instance.fill_with_dummy_data()
85
+ otl_objects.append(instance)
86
+ else:
87
+ instance = dynamic_create_instance_from_uri(class_object.objectUri, model_directory=model_directory)
88
+ if instance is None:
89
+ continue
90
+ instance.fill_with_dummy_data()
91
+ otl_objects.append(instance)
92
+
93
+ DotnotationHelper.clear_list_of_list_attributes(instance)
94
+
95
+ converter = OtlmowConverter()
96
+ converter.from_objects_to_file(file_path=temporary_path,
97
+ sequence_of_objects=otl_objects, **kwargs)
98
+ path_is_split = kwargs.get('split_per_type', True)
99
+ extension = os.path.splitext(path_to_template_file_and_extension)[-1].lower()
100
+ instantiated_attributes = []
101
+ if path_is_split is False or extension == '.xlsx':
102
+ instantiated_attributes = converter.from_file_to_objects(file_path=temporary_path,
103
+ path_to_subset=path_to_subset)
104
+ return instantiated_attributes
105
+
106
+ @classmethod
107
+ def alter_excel_template(cls, path_to_template_file_and_extension: Path, path_to_subset: Path,
108
+ instantiated_attributes: list, temporary_path, **kwargs):
109
+ generate_choice_list = kwargs.get('generate_choice_list', False)
110
+ add_geo_artefact = kwargs.get('add_geo_artefact', False)
111
+ add_attribute_info = kwargs.get('add_attribute_info', False)
112
+ highlight_deprecated_attributes = kwargs.get('highlight_deprecated_attributes', False)
113
+ amount_of_examples = kwargs.get('amount_of_examples', 0)
114
+ wb = load_workbook(temporary_path)
115
+ wb.create_sheet('Keuzelijsten')
116
+ # Volgorde is belangrijk! Eerst rijen verwijderen indien nodig dan choice list toevoegen,
117
+ # staat namelijk vast op de kolom en niet het attribuut in die kolom
118
+ if add_geo_artefact is False:
119
+ cls.remove_geo_artefact_excel(workbook=wb)
120
+ if generate_choice_list:
121
+ cls.add_choice_list_excel(workbook=wb, instantiated_attributes=instantiated_attributes,
122
+ path_to_subset=path_to_subset)
123
+ cls.add_mock_data_excel(workbook=wb, rows_of_examples=amount_of_examples)
124
+ if highlight_deprecated_attributes:
125
+ cls.check_for_deprecated_attributes(workbook=wb, instantiated_attributes=instantiated_attributes)
126
+ if add_attribute_info:
127
+ cls.add_attribute_info_excel(workbook=wb, instantiated_attributes=instantiated_attributes)
128
+ cls.design_workbook_excel(workbook=wb)
129
+ wb.save(path_to_template_file_and_extension)
130
+ file_location = os.path.dirname(temporary_path)
131
+ [f.unlink() for f in Path(file_location).glob("*") if f.is_file()]
132
+
133
+ def determine_multiplicity_csv(self, path_to_template_file_and_extension: Path, path_to_subset: Path,
134
+ instantiated_attributes: list, temporary_path: Path, **kwargs):
135
+ path_is_split = kwargs.get('split_per_type', True)
136
+ if path_is_split is False:
137
+ self.alter_csv_template(path_to_template_file_and_extension=path_to_template_file_and_extension,
138
+ temporary_path=temporary_path, path_to_subset=path_to_subset, **kwargs)
139
+ else:
140
+ self.multiple_csv_template(path_to_template_file_and_extension=path_to_template_file_and_extension,
141
+ temporary_path=temporary_path,
142
+ path_to_subset=path_to_subset, instantiated_attributes=instantiated_attributes,
143
+ **kwargs)
144
+ file_location = os.path.dirname(temporary_path)
145
+ [f.unlink() for f in Path(file_location).glob("*") if f.is_file()]
146
+
147
+ @classmethod
148
+ def filters_assets_by_subset(cls, path_to_subset: Path, list_of_otl_objectUri: [str] = None):
149
+ if list_of_otl_objectUri is None:
150
+ list_of_otl_objectUri = []
151
+
152
+ collector = cls._load_collector_from_subset_path(path_to_subset=path_to_subset)
153
+ if list_of_otl_objectUri == []:
154
+ return collector.classes
155
+ return [x for x in collector.classes if x.objectUri in list_of_otl_objectUri]
156
+
157
+ @staticmethod
158
+ def _try_getting_settings_of_converter() -> Path:
159
+ converter_path = Path(site.getsitepackages()[0]) / 'otlmow_converter'
160
+ return converter_path / 'settings_otlmow_converter.json'
161
+
162
+ @classmethod
163
+ def design_workbook_excel(cls, workbook):
164
+ for sheet in workbook:
165
+ dim_holder = DimensionHolder(worksheet=sheet)
166
+ for col in range(sheet.min_column, sheet.max_column + 1):
167
+ dim_holder[get_column_letter(col)] = ColumnDimension(sheet, min=col, max=col, width=20)
168
+ sheet.column_dimensions = dim_holder
169
+
170
+ @classmethod
171
+ def add_attribute_info_excel(cls, workbook, instantiated_attributes: list):
172
+ dotnotation_module = DotnotationHelper()
173
+ for sheet in workbook:
174
+ if sheet == workbook['Keuzelijsten']:
175
+ break
176
+ filter_uri = SubsetTemplateCreator.find_uri_in_sheet(sheet)
177
+ single_attribute = next(x for x in instantiated_attributes if x.typeURI == filter_uri)
178
+ sheet.insert_rows(1)
179
+ for rows in sheet.iter_rows(min_row=2, max_row=2, min_col=1):
180
+ for cell in rows:
181
+ if cell.value == 'typeURI':
182
+ value = 'De URI van het object volgens https://www.w3.org/2001/XMLSchema#anyURI .'
183
+ elif cell.value.find('[DEPRECATED]') != -1:
184
+ strip = cell.value.split(' ')
185
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute,
186
+ strip[1])
187
+ value = dotnotation_attribute.definition
188
+ else:
189
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute,
190
+ cell.value)
191
+ value = dotnotation_attribute.definition
192
+
193
+ sheet.cell(row=1, column=cell.column, value=value)
194
+ sheet.cell(row=1, column=cell.column).fill = PatternFill(start_color="808080", end_color="808080",
195
+ fill_type="solid")
196
+
197
+ @classmethod
198
+ def check_for_deprecated_attributes(cls, workbook, instantiated_attributes: list):
199
+ dotnotation_module = DotnotationHelper()
200
+ for sheet in workbook:
201
+ if sheet == workbook['Keuzelijsten']:
202
+ break
203
+ filter_uri = SubsetTemplateCreator.find_uri_in_sheet(sheet)
204
+ single_attribute = next(x for x in instantiated_attributes if x.typeURI == filter_uri)
205
+ for rows in sheet.iter_rows(min_row=1, max_row=1, min_col=2):
206
+ for cell in rows:
207
+ is_deprecated = False
208
+ if cell.value.count('.') == 1:
209
+ dot_split = cell.value.split('.')
210
+ attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute,
211
+ dot_split[0])
212
+
213
+ if len(attribute.deprecated_version) > 0:
214
+ is_deprecated = True
215
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute,
216
+ cell.value)
217
+ if len(dotnotation_attribute.deprecated_version) > 0:
218
+ is_deprecated = True
219
+
220
+ if is_deprecated:
221
+ cell.value = f'[DEPRECATED] {cell.value}'
222
+
223
+ @classmethod
224
+ def find_uri_in_sheet(cls, sheet):
225
+ filter_uri = None
226
+ for row in sheet.iter_rows(min_row=1, max_row=1):
227
+ for cell in row:
228
+ if cell.value == 'typeURI':
229
+ row_index = cell.row
230
+ column_index = cell.column
231
+ filter_uri = sheet.cell(row=row_index + 1, column=column_index).value
232
+ return filter_uri
233
+
234
+ @classmethod
235
+ def remove_geo_artefact_excel(cls, workbook):
236
+ for sheet in workbook:
237
+ for row in sheet.iter_rows(min_row=1, max_row=1):
238
+ for cell in row:
239
+ if cell.value == 'geometry':
240
+ sheet.delete_cols(cell.column)
241
+
242
+ @classmethod
243
+ def add_choice_list_excel(cls, workbook, instantiated_attributes: list, path_to_subset: Path):
244
+ choice_list_dict = {}
245
+ dotnotation_module = DotnotationHelper()
246
+ for sheet in workbook:
247
+ if sheet == workbook['Keuzelijsten']:
248
+ break
249
+ filter_uri = SubsetTemplateCreator.find_uri_in_sheet(sheet)
250
+ single_attribute = next(x for x in instantiated_attributes if x.typeURI == filter_uri)
251
+ for rows in sheet.iter_rows(min_row=1, max_row=1, min_col=2):
252
+ for cell in rows:
253
+ if cell.value.find('[DEPRECATED]') != -1:
254
+ strip = cell.value.split(' ')
255
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute,
256
+ strip[1])
257
+ else:
258
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute,
259
+ cell.value)
260
+
261
+ if issubclass(dotnotation_attribute.field, KeuzelijstField):
262
+ name = dotnotation_attribute.field.naam
263
+ valid_options = [v.invulwaarde for k, v in dotnotation_attribute.field.options.items()
264
+ if v.status != 'verwijderd']
265
+ if (dotnotation_attribute.field.naam not in choice_list_dict):
266
+ choice_list_dict = cls.add_choice_list_to_sheet(
267
+ workbook=workbook, name=name, options=valid_options, choice_list_dict=choice_list_dict)
268
+ column = choice_list_dict[dotnotation_attribute.field.naam]
269
+ start_range = f"${column}$2"
270
+ end_range = f"${column}${len(valid_options) + 1}"
271
+ data_val = DataValidation(type="list", formula1=f"Keuzelijsten!{start_range}:{end_range}",
272
+ allowBlank=True)
273
+ sheet.add_data_validation(data_val)
274
+ data_val.add(f'{get_column_letter(cell.column)}2:{get_column_letter(cell.column)}1000')
275
+ if issubclass(dotnotation_attribute.field, BooleanField):
276
+ data_validation = DataValidation(type="list", formula1='"TRUE,FALSE,-"', allow_blank=True)
277
+ column = cell.column
278
+ sheet.add_data_validation(data_validation)
279
+ data_validation.add(f'{get_column_letter(column)}2:{get_column_letter(column)}1000')
280
+ sheet.add_data_validation(data_validation)
281
+
282
+ @classmethod
283
+ def add_mock_data_excel(cls, workbook, rows_of_examples: int):
284
+ for sheet in workbook:
285
+ if sheet == workbook["Keuzelijsten"]:
286
+ break
287
+ if rows_of_examples == 0:
288
+ for rows in sheet.iter_rows(min_row=2, max_row=2):
289
+ for cell in rows:
290
+ cell.value = ''
291
+
292
+ @classmethod
293
+ def remove_geo_artefact_csv(cls, header, data):
294
+ if 'geometry' in header:
295
+ deletion_index = header.index('geometry')
296
+ header.remove('geometry')
297
+ for d in data:
298
+ d.pop(deletion_index)
299
+ return [header, data]
300
+
301
+ @classmethod
302
+ def multiple_csv_template(cls, path_to_template_file_and_extension, path_to_subset, temporary_path,
303
+ instantiated_attributes, **kwargs):
304
+ file_location = os.path.dirname(path_to_template_file_and_extension)
305
+ tempdir = Path(tempfile.gettempdir()) / 'temp-otlmow'
306
+ logging.debug(file_location)
307
+ file_name = ntpath.basename(path_to_template_file_and_extension)
308
+ split_file_name = file_name.split('.')
309
+ things_in_there = os.listdir(tempdir)
310
+ csv_templates = [x for x in things_in_there if x.startswith(f'{split_file_name[0]}_')]
311
+ for file in csv_templates:
312
+ test_template_loc = Path(os.path.dirname(path_to_template_file_and_extension)) / file
313
+ temp_loc = Path(tempdir) / file
314
+ cls.alter_csv_template(path_to_template_file_and_extension=test_template_loc, temporary_path=temp_loc,
315
+ path_to_subset=path_to_subset, **kwargs)
316
+
317
+ @classmethod
318
+ def alter_csv_template(cls, path_to_template_file_and_extension, path_to_subset, temporary_path,
319
+ **kwargs):
320
+ converter = OtlmowConverter()
321
+ instantiated_attributes = converter.from_file_to_objects(file_path=temporary_path,
322
+ path_to_subset=path_to_subset)
323
+ header = []
324
+ data = []
325
+ delimiter = ';'
326
+ add_geo_artefact = kwargs.get('add_geo_artefact', False)
327
+ add_attribute_info = kwargs.get('add_attribute_info', False)
328
+ highlight_deprecated_attributes = kwargs.get('highlight_deprecated_attributes', False)
329
+ amount_of_examples = kwargs.get('amount_of_examples', 0)
330
+ quote_char = '"'
331
+ with open(temporary_path, 'r+', encoding='utf-8') as csvfile:
332
+ with open(path_to_template_file_and_extension, 'w', encoding='utf-8') as new_file:
333
+ reader = csv.reader(csvfile, delimiter=delimiter, quotechar=quote_char)
334
+ for row_nr, row in enumerate(reader):
335
+ if row_nr == 0:
336
+ header = row
337
+ else:
338
+ data.append(row)
339
+ if add_geo_artefact is False:
340
+ [header, data] = cls.remove_geo_artefact_csv(header=header, data=data)
341
+ if add_attribute_info:
342
+ [info, header] = cls.add_attribute_info_csv(header=header, data=data,
343
+ instantiated_attributes=instantiated_attributes)
344
+ new_file.write(delimiter.join(info) + '\n')
345
+ data = cls.add_mock_data_csv(header=header, data=data, rows_of_examples=amount_of_examples)
346
+ if highlight_deprecated_attributes:
347
+ header = cls.highlight_deprecated_attributes_csv(header=header, data=data,
348
+ instantiated_attributes=instantiated_attributes)
349
+ new_file.write(delimiter.join(header) + '\n')
350
+ for d in data:
351
+ new_file.write(delimiter.join(d) + '\n')
352
+
353
+ @classmethod
354
+ def add_attribute_info_csv(cls, header, data, instantiated_attributes):
355
+ info_data = []
356
+ info_data.extend(header)
357
+ found_uri = []
358
+ dotnotation_module = DotnotationHelper()
359
+ uri_index = cls.find_uri_in_csv(header)
360
+ for d in data:
361
+ if d[uri_index] not in found_uri:
362
+ found_uri.append(d[uri_index])
363
+ for uri in found_uri:
364
+ single_object = next(x for x in instantiated_attributes if x.typeURI == uri)
365
+ for dotnototation_title in info_data:
366
+ if dotnototation_title == 'typeURI':
367
+ index = info_data.index(dotnototation_title)
368
+ info_data[index] = 'De URI van het object volgens https://www.w3.org/2001/XMLSchema#anyURI .'
369
+ else:
370
+ index = info_data.index(dotnototation_title)
371
+ try:
372
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(
373
+ single_object, dotnototation_title)
374
+ except AttributeError as e:
375
+ continue
376
+ info_data[index] = dotnotation_attribute.definition
377
+ return [info_data, header]
378
+
379
+ @classmethod
380
+ def add_mock_data_csv(cls, header, data, rows_of_examples):
381
+ if rows_of_examples == 0:
382
+ data = []
383
+ return data
384
+
385
+ @classmethod
386
+ def highlight_deprecated_attributes_csv(cls, header, data, instantiated_attributes):
387
+ found_uri = []
388
+ dotnotation_module = DotnotationHelper()
389
+ uri_index = cls.find_uri_in_csv(header)
390
+ for d in data:
391
+ if d[uri_index] not in found_uri:
392
+ found_uri.append(d[uri_index])
393
+ for uri in found_uri:
394
+ single_object = next(x for x in instantiated_attributes if x.typeURI == uri)
395
+ for dotnototation_title in header:
396
+ if dotnototation_title == 'typeURI':
397
+ continue
398
+
399
+ index = header.index(dotnototation_title)
400
+ value = header[index]
401
+ try:
402
+ is_deprecated = False
403
+ if dotnototation_title.count('.') == 1:
404
+ dot_split = dotnototation_title.split('.')
405
+ attribute = dotnotation_module.get_attribute_by_dotnotation(single_object,
406
+ dot_split[0])
407
+
408
+ if len(attribute.deprecated_version) > 0:
409
+ is_deprecated = True
410
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_object,
411
+ dotnototation_title)
412
+ if len(dotnotation_attribute.deprecated_version) > 0:
413
+ is_deprecated = True
414
+ except AttributeError:
415
+ continue
416
+ if is_deprecated:
417
+ header[index] = f"[DEPRECATED] {value}"
418
+ return header
419
+
420
+ @classmethod
421
+ def find_uri_in_csv(cls, header):
422
+ return header.index('typeURI') if 'typeURI' in header else None
423
+
424
+ @classmethod
425
+ def add_choice_list_to_sheet(cls, workbook, name, options, choice_list_dict):
426
+ active_sheet = workbook['Keuzelijsten']
427
+ row_nr = 2
428
+ for rows in active_sheet.iter_rows(min_row=1, max_row=1, min_col=1, max_col=700):
429
+ for cell in rows:
430
+ if cell.value is None:
431
+ cell.value = name
432
+ column_nr = cell.column
433
+ for option in options:
434
+ active_sheet.cell(row=row_nr, column=column_nr, value=option)
435
+ row_nr += 1
436
+ choice_list_dict[name] = get_column_letter(column_nr)
437
+ break
438
+ return choice_list_dict
439
+
440
+
441
+ if __name__ == '__main__':
442
+ subset_tool = SubsetTemplateCreator()
443
+ subset_location = Path(ROOT_DIR) / 'UnitTests' / 'Subset' / 'Flitspaal_noAgent3.0.db'
444
+ # directory = Path(ROOT_DIR) / 'UnitTests' / 'TestClasses'
445
+ # Slash op het einde toevoegen verandert weinig of niks aan het resultaat
446
+ # directory = os.path.join(directory, '')
447
+ xls_location = Path(ROOT_DIR) / 'UnitTests' / 'Subset' / 'testFileStorage' / 'template_file.csv'
448
+ subset_tool.generate_template_from_subset(path_to_subset=subset_location,
449
+ path_to_template_file_and_extension=xls_location, add_attribute_info=True,
450
+ highlight_deprecated_attributes=True,
451
+ amount_of_examples=5,
452
+ generate_choice_list=True,
453
+ split_per_type=False)
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
- Name: otlmow-template
3
- Version: 0.3
4
- Author-email: David Vlaminck <david.vlaminck@mow.vlaanderen.be>
2
+ Name: otlmow_template
3
+ Version: 0.5
4
+ Author-email: David Vlaminck <david.vlaminck@mow.vlaanderen.be>, Jasper Berton <jasperberton1@telenet.be>
5
5
  License: GNU GENERAL PUBLIC LICENSE
6
6
  Version 3, 29 June 2007
7
7
 
@@ -679,10 +679,11 @@ License: GNU GENERAL PUBLIC LICENSE
679
679
  Project-URL: Homepage, https://github.com/davidvlaminck/OTLMOW-Template
680
680
  Project-URL: Bug Tracker, https://github.com/davidvlaminck/OTLMOW-Template/issues
681
681
  Classifier: Programming Language :: Python :: 3
682
- Classifier: Programming Language :: Python :: 3.8
683
682
  Classifier: Programming Language :: Python :: 3.9
684
683
  Classifier: Programming Language :: Python :: 3.10
685
684
  Classifier: Programming Language :: Python :: 3.11
685
+ Classifier: Programming Language :: Python :: 3.12
686
+ Classifier: Programming Language :: Python :: 3.13
686
687
  Classifier: Operating System :: OS Independent
687
688
  Classifier: Development Status :: 5 - Production/Stable
688
689
  Classifier: Environment :: Console
@@ -693,11 +694,11 @@ Classifier: Natural Language :: Dutch
693
694
  Classifier: Topic :: Software Development
694
695
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
695
696
  Classifier: Topic :: Software Development :: Quality Assurance
696
- Requires-Python: >=3.8
697
+ Requires-Python: >=3.9
697
698
  Description-Content-Type: text/markdown
698
699
  License-File: LICENSE
699
- Requires-Dist: otlmow-converter>=0.14
700
- Requires-Dist: otlmow-modelbuilder>=0.11
700
+ Requires-Dist: otlmow-converter>=0.15
701
+ Requires-Dist: otlmow-modelbuilder>=0.24
701
702
 
702
703
  # OTLMOW-Template
703
704
  [![PyPI](https://img.shields.io/pypi/v/otlmow-template?label=latest%20release)](https://pypi.org/project/otlmow-template/)
@@ -0,0 +1,2 @@
1
+ otlmow-converter>=0.15
2
+ otlmow-modelbuilder>=0.24
@@ -4,16 +4,18 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "otlmow_template"
7
- version = "0.3"
8
- authors = [{name = "David Vlaminck", email = "david.vlaminck@mow.vlaanderen.be"}]
7
+ version = "0.5"
8
+ authors = [{name = "David Vlaminck", email = "david.vlaminck@mow.vlaanderen.be"},
9
+ {name = "Jasper Berton", email = "jasperberton1@telenet.be"},]
9
10
  readme = "README.md"
10
11
  license = {file = "LICENSE"}
11
12
  classifiers = [
12
13
  "Programming Language :: Python :: 3",
13
- "Programming Language :: Python :: 3.8",
14
14
  "Programming Language :: Python :: 3.9",
15
15
  "Programming Language :: Python :: 3.10",
16
16
  "Programming Language :: Python :: 3.11",
17
+ "Programming Language :: Python :: 3.12",
18
+ "Programming Language :: Python :: 3.13",
17
19
  "Operating System :: OS Independent",
18
20
  "Development Status :: 5 - Production/Stable",
19
21
  "Environment :: Console",
@@ -25,10 +27,10 @@ classifiers = [
25
27
  "Topic :: Software Development :: Libraries :: Python Modules",
26
28
  "Topic :: Software Development :: Quality Assurance",
27
29
  ]
28
- requires-python = ">=3.8"
30
+ requires-python = ">=3.9"
29
31
  dependencies = [
30
- 'otlmow-converter >= 0.14',
31
- 'otlmow-modelbuilder >= 0.11',
32
+ 'otlmow-converter >= 0.15',
33
+ 'otlmow-modelbuilder >= 0.24',
32
34
  ]
33
35
 
34
36
  [tool.setuptools.packages.find]
@@ -1,122 +0,0 @@
1
- import ntpath
2
- import os
3
- import site
4
- import tempfile
5
- from pathlib import Path
6
- from otlmow_converter.OtlmowConverter import OtlmowConverter
7
- from otlmow_model.OtlmowModel.Helpers.AssetCreator import dynamic_create_instance_from_uri
8
- from otlmow_modelbuilder.OSLOCollector import OSLOCollector
9
-
10
- from otlmow_template.CsvTemplateCreator import CsvTemplateCreator
11
- from otlmow_template.ExcelTemplateCreator import ExcelTemplateCreator
12
-
13
- ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
14
-
15
- enumeration_validation_rules = {
16
- "valid_uri_and_types": {},
17
- "valid_regexes": [
18
- "^https://wegenenverkeer.data.vlaanderen.be/ns/.+"]
19
- }
20
-
21
-
22
- class SubsetTemplateCreator:
23
- def __init__(self):
24
- pass
25
-
26
- @staticmethod
27
- def _load_collector_from_subset_path(path_to_subset: Path) -> OSLOCollector:
28
- collector = OSLOCollector(path_to_subset)
29
- collector.collect_all(include_abstract=True)
30
- return collector
31
-
32
- def generate_template_from_subset(self, path_to_subset: Path, path_to_template_file_and_extension: Path,
33
- **kwargs):
34
- temporary_path = self.return_temp_path(path_to_template_file_and_extension=path_to_template_file_and_extension)
35
- instantiated_attributes = self.generate_basic_template(path_to_subset=path_to_subset,
36
- temporary_path=temporary_path,
37
- path_to_template_file_and_extension=path_to_template_file_and_extension,
38
- **kwargs)
39
- extension = os.path.splitext(path_to_template_file_and_extension)[-1].lower()
40
- if extension == '.xlsx':
41
- ExcelTemplateCreator().alter_excel_template(
42
- path_to_template_file_and_extension=path_to_template_file_and_extension,
43
- temporary_path=temporary_path, instantiated_attributes=instantiated_attributes, **kwargs)
44
- elif extension == '.csv':
45
- CsvTemplateCreator().determine_multiplicity_csv(
46
- path_to_template_file_and_extension=path_to_template_file_and_extension,
47
- path_to_subset=path_to_subset,
48
- temporary_path=temporary_path,
49
- **kwargs)
50
-
51
- def generate_basic_template(self, path_to_subset: Path, path_to_template_file_and_extension: Path,
52
- temporary_path: Path, **kwargs):
53
- collector = self._load_collector_from_subset_path(path_to_subset=path_to_subset)
54
- otl_objects = []
55
- amount_of_examples = kwargs.get('amount_of_examples', 0)
56
- class_list = self.filters_assets_by_subset(path_to_subset=path_to_subset, **kwargs)
57
-
58
- for class_object in list(class_list):
59
- model_directory = None
60
- if kwargs is not None:
61
- model_directory = kwargs.get('model_directory', None)
62
- if amount_of_examples != 0:
63
- for i in range(amount_of_examples):
64
- instance = dynamic_create_instance_from_uri(class_object.objectUri, model_directory=model_directory)
65
- if instance is None:
66
- continue
67
- instance.fill_with_dummy_data()
68
- otl_objects.append(instance)
69
- else:
70
- instance = dynamic_create_instance_from_uri(class_object.objectUri, model_directory=model_directory)
71
- if instance is None:
72
- continue
73
- instance.fill_with_dummy_data()
74
- otl_objects.append(instance)
75
-
76
- converter = OtlmowConverter()
77
- converter.create_file_from_assets(filepath=temporary_path,
78
- list_of_objects=otl_objects, **kwargs)
79
- path_is_split = kwargs.get('split_per_type', True)
80
- extension = os.path.splitext(path_to_template_file_and_extension)[-1].lower()
81
- instantiated_attributes = []
82
- if path_is_split is False or extension == '.xlsx':
83
- instantiated_attributes = converter.create_assets_from_file(filepath=temporary_path,
84
- path_to_subset=path_to_subset)
85
- return instantiated_attributes
86
-
87
- @classmethod
88
- def filters_assets_by_subset(cls, path_to_subset: Path, **kwargs):
89
- list_of_otl_object_uri = kwargs.get('list_of_otl_objectUri', None)
90
- collector = cls._load_collector_from_subset_path(path_to_subset=path_to_subset)
91
- if list_of_otl_object_uri is None:
92
- return [x for x in collector.classes if x.abstract == 0]
93
- else:
94
- collector = cls._load_collector_from_subset_path(path_to_subset=path_to_subset)
95
- filtered_list = [x for x in collector.classes if x.objectUri in list_of_otl_object_uri]
96
- return filtered_list
97
-
98
- @staticmethod
99
- def _try_getting_settings_of_converter() -> Path:
100
- converter_path = Path(site.getsitepackages()[0]) / 'otlmow_converter'
101
- return converter_path / 'settings_otlmow_converter.json'
102
-
103
- @classmethod
104
- def return_temp_path(cls, path_to_template_file_and_extension: Path):
105
- tempdir = Path(tempfile.gettempdir()) / 'temp-otlmow'
106
- if not tempdir.exists():
107
- os.makedirs(tempdir)
108
- test = ntpath.basename(path_to_template_file_and_extension)
109
- temporary_path = Path(tempdir) / test
110
- return temporary_path
111
-
112
-
113
- if __name__ == '__main__':
114
- subset_tool = SubsetTemplateCreator()
115
- subset_location = Path(ROOT_DIR) / 'UnitTests' / 'Subset' / 'Flitspaal_noAgent3.0.db'
116
- xls_location = Path(ROOT_DIR) / 'UnitTests' / 'Subset' / 'testFileStorage' / 'template_file.xlsx'
117
- subset_tool.generate_template_from_subset(path_to_subset=subset_location,
118
- path_to_template_file_and_extension=xls_location, add_attribute_info=True,
119
- highlight_deprecated_attributes=True,
120
- amount_of_examples=5,
121
- generate_choice_list=True,
122
- )
@@ -1,2 +0,0 @@
1
- otlmow-converter>=0.14
2
- otlmow-modelbuilder>=0.11
File without changes
File without changes
File without changes