otlmow-template 0.3__tar.gz → 0.4__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.4
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
 
@@ -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,465 @@
1
+ import ntpath
2
+ import os
3
+ import csv
4
+ import site
5
+ import tempfile
6
+ from pathlib import Path
7
+ from typing import List
8
+ from openpyxl.reader.excel import load_workbook
9
+ from openpyxl.styles import PatternFill
10
+ from openpyxl.utils import get_column_letter
11
+ from openpyxl.worksheet.datavalidation import DataValidation
12
+ from openpyxl.worksheet.dimensions import DimensionHolder, ColumnDimension
13
+ from otlmow_converter.DotnotationHelper import DotnotationHelper
14
+ from otlmow_converter.OtlmowConverter import OtlmowConverter
15
+ from otlmow_model.OtlmowModel.BaseClasses.BooleanField import BooleanField
16
+ from otlmow_model.OtlmowModel.BaseClasses.KeuzelijstField import KeuzelijstField
17
+ from otlmow_model.OtlmowModel.BaseClasses.OTLObject import dynamic_create_instance_from_uri
18
+ from otlmow_modelbuilder.OSLOCollector import OSLOCollector
19
+
20
+ ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
21
+
22
+ enumeration_validation_rules = {
23
+ "valid_uri_and_types": {},
24
+ "valid_regexes": [
25
+ "^https://wegenenverkeer.data.vlaanderen.be/ns/.+"]
26
+ }
27
+
28
+
29
+ class SubsetTemplateCreator:
30
+ def __init__(self):
31
+ pass
32
+
33
+ @staticmethod
34
+ def _load_collector_from_subset_path(path_to_subset: Path) -> OSLOCollector:
35
+ collector = OSLOCollector(path_to_subset)
36
+ collector.collect_all(include_abstract=True)
37
+ return collector
38
+
39
+ def generate_template_from_subset(self, path_to_subset: Path, path_to_template_file_and_extension: Path,
40
+ **kwargs):
41
+ tempdir = Path(tempfile.gettempdir()) / 'temp-otlmow'
42
+ if not tempdir.exists():
43
+ os.makedirs(tempdir)
44
+ test = ntpath.basename(path_to_template_file_and_extension)
45
+ temporary_path = Path(tempdir) / test
46
+ instantiated_attributes = self.generate_basic_template(path_to_subset=path_to_subset,
47
+ temporary_path=temporary_path,
48
+ path_to_template_file_and_extension=path_to_template_file_and_extension,
49
+ **kwargs)
50
+ extension = os.path.splitext(path_to_template_file_and_extension)[-1].lower()
51
+ if extension == '.xlsx':
52
+ self.alter_excel_template(path_to_template_file_and_extension=path_to_template_file_and_extension,
53
+ temporary_path=temporary_path,
54
+ path_to_subset=path_to_subset, instantiated_attributes=instantiated_attributes,
55
+ **kwargs)
56
+ elif extension == '.csv':
57
+ self.determine_multiplicity_csv(path_to_template_file_and_extension=path_to_template_file_and_extension,
58
+ path_to_subset=path_to_subset,
59
+ instantiated_attributes=instantiated_attributes,
60
+ temporary_path=temporary_path,
61
+ **kwargs)
62
+
63
+ def generate_basic_template(self, path_to_subset: Path, path_to_template_file_and_extension: Path,
64
+ temporary_path: Path, **kwargs):
65
+ collector = self._load_collector_from_subset_path(path_to_subset=path_to_subset)
66
+ otl_objects = []
67
+ amount_of_examples = kwargs.get('amount_of_examples', 0)
68
+
69
+ for class_object in list(filter(lambda cl: cl.abstract == 0, collector.classes)):
70
+ model_directory = None
71
+ if kwargs is not None:
72
+ model_directory = kwargs.get('model_directory', None)
73
+ if amount_of_examples != 0:
74
+ for i in range(amount_of_examples):
75
+ instance = dynamic_create_instance_from_uri(class_object.objectUri, model_directory=model_directory)
76
+ if instance is None:
77
+ continue
78
+ instance.fill_with_dummy_data()
79
+ otl_objects.append(instance)
80
+ else:
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
+
87
+ # TODO: check if this is needed, as the above line should cover this
88
+ attributen = collector.find_attributes_by_class(class_object)
89
+ for attribute_object in attributen:
90
+ attr = getattr(instance, '_' + attribute_object.name)
91
+ attr.fill_with_dummy_data()
92
+ converter = OtlmowConverter()
93
+ converter.from_objects_to_file(file_path=temporary_path,
94
+ sequence_of_objects=otl_objects, **kwargs)
95
+ path_is_split = kwargs.get('split_per_type', True)
96
+ extension = os.path.splitext(path_to_template_file_and_extension)[-1].lower()
97
+ instantiated_attributes = []
98
+ if path_is_split is False or extension == '.xlsx':
99
+ instantiated_attributes = converter.from_file_to_objects(file_path=temporary_path,
100
+ path_to_subset=path_to_subset)
101
+ return instantiated_attributes
102
+
103
+ @classmethod
104
+ def alter_excel_template(cls, path_to_template_file_and_extension: Path, path_to_subset: Path,
105
+ instantiated_attributes: List, temporary_path, **kwargs):
106
+ generate_choice_list = kwargs.get('generate_choice_list', False)
107
+ add_geo_artefact = kwargs.get('add_geo_artefact', False)
108
+ add_attribute_info = kwargs.get('add_attribute_info', False)
109
+ highlight_deprecated_attributes = kwargs.get('highlight_deprecated_attributes', False)
110
+ amount_of_examples = kwargs.get('amount_of_examples', 0)
111
+ wb = load_workbook(temporary_path)
112
+ wb.create_sheet('Keuzelijsten')
113
+ # Volgorde is belangrijk! Eerst rijen verwijderen indien nodig dan choice list toevoegen,
114
+ # staat namelijk vast op de kolom en niet het attribuut in die kolom
115
+ if add_geo_artefact is False:
116
+ cls.remove_geo_artefact_excel(workbook=wb)
117
+ if generate_choice_list:
118
+ cls.add_choice_list_excel(workbook=wb, instantiated_attributes=instantiated_attributes,
119
+ path_to_subset=path_to_subset)
120
+ cls.add_mock_data_excel(workbook=wb, rows_of_examples=amount_of_examples)
121
+ if highlight_deprecated_attributes:
122
+ cls.check_for_deprecated_attributes(workbook=wb, instantiated_attributes=instantiated_attributes)
123
+ if add_attribute_info:
124
+ cls.add_attribute_info_excel(workbook=wb, instantiated_attributes=instantiated_attributes)
125
+ cls.design_workbook_excel(workbook=wb)
126
+ wb.save(path_to_template_file_and_extension)
127
+ file_location = os.path.dirname(temporary_path)
128
+ [f.unlink() for f in Path(file_location).glob("*") if f.is_file()]
129
+
130
+ def determine_multiplicity_csv(self, path_to_template_file_and_extension: Path, path_to_subset: Path,
131
+ instantiated_attributes: List, temporary_path: Path, **kwargs):
132
+ path_is_split = kwargs.get('split_per_type', True)
133
+ if path_is_split is False:
134
+ self.alter_csv_template(path_to_template_file_and_extension=path_to_template_file_and_extension,
135
+ temporary_path=temporary_path, path_to_subset=path_to_subset, **kwargs)
136
+ else:
137
+ self.multiple_csv_template(path_to_template_file_and_extension=path_to_template_file_and_extension,
138
+ temporary_path=temporary_path,
139
+ path_to_subset=path_to_subset, instantiated_attributes=instantiated_attributes,
140
+ **kwargs)
141
+ file_location = os.path.dirname(temporary_path)
142
+ [f.unlink() for f in Path(file_location).glob("*") if f.is_file()]
143
+
144
+ @classmethod
145
+ def filters_assets_by_subset(cls, path_to_subset: Path, list_of_otl_objectUri: [str] = None):
146
+ if list_of_otl_objectUri is None:
147
+ list_of_otl_objectUri = []
148
+
149
+ collector = cls._load_collector_from_subset_path(path_to_subset=path_to_subset)
150
+ if list_of_otl_objectUri == []:
151
+ return [x for x in collector.classes]
152
+ return [x for x in collector.classes if x.objectUri in list_of_otl_objectUri]
153
+
154
+ @staticmethod
155
+ def _try_getting_settings_of_converter() -> Path:
156
+ converter_path = Path(site.getsitepackages()[0]) / 'otlmow_converter'
157
+ return converter_path / 'settings_otlmow_converter.json'
158
+
159
+ @classmethod
160
+ def design_workbook_excel(cls, workbook):
161
+ for sheet in workbook:
162
+ dim_holder = DimensionHolder(worksheet=sheet)
163
+ for col in range(sheet.min_column, sheet.max_column + 1):
164
+ dim_holder[get_column_letter(col)] = ColumnDimension(sheet, min=col, max=col, width=20)
165
+ sheet.column_dimensions = dim_holder
166
+
167
+ @classmethod
168
+ def add_attribute_info_excel(cls, workbook, instantiated_attributes: List):
169
+ dotnotation_module = DotnotationHelper()
170
+ for sheet in workbook:
171
+ if sheet == workbook['Keuzelijsten']:
172
+ break
173
+ filter_uri = SubsetTemplateCreator.find_uri_in_sheet(sheet)
174
+ single_attribute = next(x for x in instantiated_attributes if x.typeURI == filter_uri)
175
+ sheet.insert_rows(1)
176
+ for rows in sheet.iter_rows(min_row=2, max_row=2, min_col=1):
177
+ for cell in rows:
178
+ if cell.value == 'typeURI':
179
+ value = 'De URI van het object volgens https://www.w3.org/2001/XMLSchema#anyURI .'
180
+ elif cell.value.find('[DEPRECATED]') != -1:
181
+ strip = cell.value.split(' ')
182
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute,
183
+ strip[1])
184
+ value = dotnotation_attribute.definition
185
+ else:
186
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute,
187
+ cell.value)
188
+ value = dotnotation_attribute.definition
189
+
190
+ sheet.cell(row=1, column=cell.column, value=value)
191
+ sheet.cell(row=1, column=cell.column).fill = PatternFill(start_color="808080", end_color="808080",
192
+ fill_type="solid")
193
+
194
+ @classmethod
195
+ def check_for_deprecated_attributes(cls, workbook, instantiated_attributes: List):
196
+ dotnotation_module = DotnotationHelper()
197
+ for sheet in workbook:
198
+ if sheet == workbook['Keuzelijsten']:
199
+ break
200
+ filter_uri = SubsetTemplateCreator.find_uri_in_sheet(sheet)
201
+ single_attribute = next(x for x in instantiated_attributes if x.typeURI == filter_uri)
202
+ for rows in sheet.iter_rows(min_row=1, max_row=1, min_col=2):
203
+ for cell in rows:
204
+ is_deprecated = False
205
+ if cell.value.count('.') == 1:
206
+ dot_split = cell.value.split('.')
207
+ attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute,
208
+ dot_split[0])
209
+
210
+ if len(attribute.deprecated_version) > 0:
211
+ is_deprecated = True
212
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute,
213
+ cell.value)
214
+ if len(dotnotation_attribute.deprecated_version) > 0:
215
+ is_deprecated = True
216
+
217
+ if is_deprecated:
218
+ cell.value = '[DEPRECATED] ' + cell.value
219
+
220
+ @classmethod
221
+ def find_uri_in_sheet(cls, sheet):
222
+ filter_uri = None
223
+ for row in sheet.iter_rows(min_row=1, max_row=1):
224
+ for cell in row:
225
+ if cell.value == 'typeURI':
226
+ row_index = cell.row
227
+ column_index = cell.column
228
+ filter_uri = sheet.cell(row=row_index + 1, column=column_index).value
229
+ return filter_uri
230
+
231
+ @classmethod
232
+ def remove_geo_artefact_excel(cls, workbook):
233
+ for sheet in workbook:
234
+ for row in sheet.iter_rows(min_row=1, max_row=1):
235
+ for cell in row:
236
+ if cell.value == 'geometry':
237
+ sheet.delete_cols(cell.column)
238
+
239
+ @classmethod
240
+ def add_choice_list_excel(cls, workbook, instantiated_attributes: List, path_to_subset: Path):
241
+ choice_list_dict = {}
242
+ dotnotation_module = DotnotationHelper()
243
+ for sheet in workbook:
244
+ if sheet == workbook['Keuzelijsten']:
245
+ break
246
+ filter_uri = SubsetTemplateCreator.find_uri_in_sheet(sheet)
247
+ single_attribute = next(x for x in instantiated_attributes if x.typeURI == filter_uri)
248
+ for rows in sheet.iter_rows(min_row=1, max_row=1, min_col=2):
249
+ for cell in rows:
250
+ if cell.value.find('[DEPRECATED]') != -1:
251
+ strip = cell.value.split(' ')
252
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute,
253
+ strip[1])
254
+ else:
255
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_attribute,
256
+ cell.value)
257
+
258
+ if issubclass(dotnotation_attribute.field, KeuzelijstField):
259
+ name = dotnotation_attribute.field.naam
260
+ valid_options = [v.invulwaarde for k, v in dotnotation_attribute.field.options.items()
261
+ if v.status != 'verwijderd']
262
+ if dotnotation_attribute.field.naam in choice_list_dict:
263
+ column = choice_list_dict[dotnotation_attribute.field.naam]
264
+ else:
265
+ choice_list_dict = cls.add_choice_list_to_sheet(workbook=workbook, name=name,
266
+ options=valid_options,
267
+ choice_list_dict=choice_list_dict)
268
+ column = choice_list_dict[dotnotation_attribute.field.naam]
269
+ print(column)
270
+ option_list = []
271
+ for option in valid_options:
272
+ option_list.append(option)
273
+ start_range = f"${column}$2"
274
+ end_range = f"${column}${len(valid_options) + 1}"
275
+ data_val = DataValidation(type="list", formula1=f"Keuzelijsten!{start_range}:{end_range}",
276
+ allowBlank=True)
277
+ sheet.add_data_validation(data_val)
278
+ data_val.add(f'{get_column_letter(cell.column)}2:{get_column_letter(cell.column)}1000')
279
+ if issubclass(dotnotation_attribute.field, BooleanField):
280
+ data_validation = DataValidation(type="list", formula1='"TRUE,FALSE,-"', allow_blank=True)
281
+ column = cell.column
282
+ sheet.add_data_validation(data_validation)
283
+ data_validation.add(f'{get_column_letter(column)}2:{get_column_letter(column)}1000')
284
+ sheet.add_data_validation(data_validation)
285
+
286
+ @classmethod
287
+ def add_mock_data_excel(cls, workbook, rows_of_examples: int):
288
+ for sheet in workbook:
289
+ if sheet == workbook["Keuzelijsten"]:
290
+ break
291
+ if rows_of_examples == 0:
292
+ for rows in sheet.iter_rows(min_row=2, max_row=2):
293
+ for cell in rows:
294
+ cell.value = ''
295
+
296
+ @classmethod
297
+ def remove_geo_artefact_csv(cls, header, data):
298
+ if 'geometry' in header:
299
+ deletion_index = header.index('geometry')
300
+ header.remove('geometry')
301
+ for d in data:
302
+ d.pop(deletion_index)
303
+ return [header, data]
304
+
305
+ @classmethod
306
+ def multiple_csv_template(cls, path_to_template_file_and_extension, path_to_subset, temporary_path,
307
+ instantiated_attributes, **kwargs):
308
+ file_location = os.path.dirname(path_to_template_file_and_extension)
309
+ tempdir = Path(tempfile.gettempdir()) / 'temp-otlmow'
310
+ print(file_location)
311
+ file_name = ntpath.basename(path_to_template_file_and_extension)
312
+ split_file_name = file_name.split('.')
313
+ things_in_there = os.listdir(tempdir)
314
+ csv_templates = [x for x in things_in_there if x.startswith(split_file_name[0] + '_')]
315
+ print(csv_templates)
316
+ for file in csv_templates:
317
+ test_template_loc = Path(os.path.dirname(path_to_template_file_and_extension)) / file
318
+ temp_loc = Path(tempdir) / file
319
+ cls.alter_csv_template(path_to_template_file_and_extension=test_template_loc, temporary_path=temp_loc,
320
+ path_to_subset=path_to_subset, **kwargs)
321
+
322
+ @classmethod
323
+ def alter_csv_template(cls, path_to_template_file_and_extension, path_to_subset, temporary_path,
324
+ **kwargs):
325
+ converter = OtlmowConverter()
326
+ instantiated_attributes = converter.from_file_to_objects(file_path=temporary_path,
327
+ path_to_subset=path_to_subset)
328
+ header = []
329
+ data = []
330
+ delimiter = ';'
331
+ add_geo_artefact = kwargs.get('add_geo_artefact', False)
332
+ add_attribute_info = kwargs.get('add_attribute_info', False)
333
+ highlight_deprecated_attributes = kwargs.get('highlight_deprecated_attributes', False)
334
+ amount_of_examples = kwargs.get('amount_of_examples', 0)
335
+ quote_char = '"'
336
+ with open(temporary_path, 'r+', encoding='utf-8') as csvfile:
337
+ new_file = open(path_to_template_file_and_extension, 'w', encoding='utf-8')
338
+ reader = csv.reader(csvfile, delimiter=delimiter, quotechar=quote_char)
339
+ for row_nr, row in enumerate(reader):
340
+ if row_nr == 0:
341
+ header = row
342
+ else:
343
+ data.append(row)
344
+ if add_geo_artefact is False:
345
+ [header, data] = cls.remove_geo_artefact_csv(header=header, data=data)
346
+ if add_attribute_info:
347
+ [info, header] = cls.add_attribute_info_csv(header=header, data=data,
348
+ instantiated_attributes=instantiated_attributes)
349
+ new_file.write(delimiter.join(info) + '\n')
350
+ data = cls.add_mock_data_csv(header=header, data=data, rows_of_examples=amount_of_examples)
351
+ if highlight_deprecated_attributes:
352
+ header = cls.highlight_deprecated_attributes_csv(header=header, data=data,
353
+ instantiated_attributes=instantiated_attributes)
354
+ new_file.write(delimiter.join(header) + '\n')
355
+ for d in data:
356
+ new_file.write(delimiter.join(d) + '\n')
357
+ new_file.close()
358
+
359
+ @classmethod
360
+ def add_attribute_info_csv(cls, header, data, instantiated_attributes):
361
+ info_data = []
362
+ info_data.extend(header)
363
+ found_uri = []
364
+ dotnotation_module = DotnotationHelper()
365
+ uri_index = cls.find_uri_in_csv(header)
366
+ for d in data:
367
+ if d[uri_index] not in found_uri:
368
+ found_uri.append(d[uri_index])
369
+ for uri in found_uri:
370
+ single_object = next(x for x in instantiated_attributes if x.typeURI == uri)
371
+ for dotnototation_title in info_data:
372
+ if dotnototation_title == 'typeURI':
373
+ index = info_data.index(dotnototation_title)
374
+ info_data[index] = 'De URI van het object volgens https://www.w3.org/2001/XMLSchema#anyURI .'
375
+ else:
376
+ index = info_data.index(dotnototation_title)
377
+ try:
378
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(
379
+ single_object, dotnototation_title)
380
+ except AttributeError as e:
381
+ continue
382
+ info_data[index] = dotnotation_attribute.definition
383
+ return [info_data, header]
384
+
385
+ @classmethod
386
+ def add_mock_data_csv(cls, header, data, rows_of_examples):
387
+ if rows_of_examples == 0:
388
+ data = []
389
+ return data
390
+
391
+ @classmethod
392
+ def highlight_deprecated_attributes_csv(cls, header, data, instantiated_attributes):
393
+ found_uri = []
394
+ dotnotation_module = DotnotationHelper()
395
+ uri_index = cls.find_uri_in_csv(header)
396
+ for d in data:
397
+ if d[uri_index] not in found_uri:
398
+ found_uri.append(d[uri_index])
399
+ for uri in found_uri:
400
+ single_object = next(x for x in instantiated_attributes if x.typeURI == uri)
401
+ for dotnototation_title in header:
402
+ if dotnototation_title == 'typeURI':
403
+ continue
404
+ else:
405
+ index = header.index(dotnototation_title)
406
+ value = header[index]
407
+ try:
408
+ is_deprecated = False
409
+ if dotnototation_title.count('.') == 1:
410
+ dot_split = dotnototation_title.split('.')
411
+ attribute = dotnotation_module.get_attribute_by_dotnotation(single_object,
412
+ dot_split[0])
413
+
414
+ if len(attribute.deprecated_version) > 0:
415
+ is_deprecated = True
416
+ dotnotation_attribute = dotnotation_module.get_attribute_by_dotnotation(single_object,
417
+ dotnototation_title)
418
+ if len(dotnotation_attribute.deprecated_version) > 0:
419
+ is_deprecated = True
420
+ except AttributeError:
421
+ continue
422
+ if is_deprecated:
423
+ header[index] = "[DEPRECATED] " + value
424
+ return header
425
+
426
+ @classmethod
427
+ def find_uri_in_csv(cls, header):
428
+ filter_uri = None
429
+ if 'typeURI' in header:
430
+ filter_uri = header.index('typeURI')
431
+ return filter_uri
432
+
433
+ @classmethod
434
+ def add_choice_list_to_sheet(cls, workbook, name, options, choice_list_dict):
435
+ active_sheet = workbook['Keuzelijsten']
436
+ print(options)
437
+ row_nr = 2
438
+ for rows in active_sheet.iter_rows(min_row=1, max_row=1, min_col=1, max_col=700):
439
+ for cell in rows:
440
+ if cell.value is None:
441
+ cell.value = name
442
+ column_nr = cell.column
443
+ for option in options:
444
+ print(option)
445
+ active_sheet.cell(row=row_nr, column=column_nr, value=option)
446
+ row_nr += 1
447
+ print(row_nr)
448
+ choice_list_dict[name] = get_column_letter(column_nr)
449
+ break
450
+ return choice_list_dict
451
+
452
+
453
+ if __name__ == '__main__':
454
+ subset_tool = SubsetTemplateCreator()
455
+ subset_location = Path(ROOT_DIR) / 'UnitTests' / 'Subset' / 'Flitspaal_noAgent3.0.db'
456
+ # directory = Path(ROOT_DIR) / 'UnitTests' / 'TestClasses'
457
+ # Slash op het einde toevoegen verandert weinig of niks aan het resultaat
458
+ # directory = os.path.join(directory, '')
459
+ xls_location = Path(ROOT_DIR) / 'UnitTests' / 'Subset' / 'testFileStorage' / 'template_file.csv'
460
+ subset_tool.generate_template_from_subset(path_to_subset=subset_location,
461
+ path_to_template_file_and_extension=xls_location, add_attribute_info=True,
462
+ highlight_deprecated_attributes=True,
463
+ amount_of_examples=5,
464
+ generate_choice_list=True,
465
+ 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.4
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
 
@@ -4,8 +4,9 @@ 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.4"
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 = [
@@ -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
- )
File without changes
File without changes
File without changes