|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +import jsonschema |
| 5 | +from openpyxl import load_workbook |
| 6 | + |
| 7 | + |
| 8 | +def validate_properties_with_schema(json_file: str) -> bool: |
| 9 | + """ |
| 10 | + This function checks if the json properties are valid according to the schema. |
| 11 | +
|
| 12 | + Args: |
| 13 | + json_file: the json with the properties to be validated |
| 14 | +
|
| 15 | + Returns: |
| 16 | + True if the data passed validation, False otherwise |
| 17 | +
|
| 18 | + """ |
| 19 | + current_dir = os.path.dirname(os.path.realpath(__file__)) |
| 20 | + with open(os.path.join(current_dir, 'knora-schema-properties-only.json')) as schema: |
| 21 | + properties_schema = json.load(schema) |
| 22 | + |
| 23 | + try: |
| 24 | + jsonschema.validate(instance=json_file, schema=properties_schema) |
| 25 | + except jsonschema.exceptions.ValidationError as err: |
| 26 | + print(err) |
| 27 | + return False |
| 28 | + print('Properties data passed schema validation.') |
| 29 | + return True |
| 30 | + |
| 31 | + |
| 32 | +def properties_excel2json(excelfile: str, outfile: str): |
| 33 | + """ |
| 34 | + Converts properties described in an Excel file into a properties section which can be integrated into a DSP ontology |
| 35 | +
|
| 36 | + Args: |
| 37 | + excelfile: path to the Excel file containing the properties |
| 38 | + outfile: path to the output JSON file containing the properties section for the ontology |
| 39 | +
|
| 40 | + Returns: |
| 41 | + None |
| 42 | + """ |
| 43 | + # load file |
| 44 | + wb = load_workbook(filename=excelfile, read_only=True) |
| 45 | + sheet = wb.worksheets[0] |
| 46 | + props = [row_to_prop(row) for row in sheet.iter_rows(min_row=2, values_only=True, max_col=9)] |
| 47 | + |
| 48 | + prefix = '"properties":' |
| 49 | + |
| 50 | + if validate_properties_with_schema(json.loads(json.dumps(props, indent=4))): |
| 51 | + # write final list to JSON file if list passed validation |
| 52 | + with open(file=outfile, mode='w+', encoding='utf-8') as file: |
| 53 | + file.write(prefix) |
| 54 | + json.dump(props, file, indent=4) |
| 55 | + print('Properties file was created successfully and written to file:', outfile) |
| 56 | + else: |
| 57 | + print('Properties data is not valid according to schema.') |
| 58 | + |
| 59 | + return props |
| 60 | + |
| 61 | + |
| 62 | +def row_to_prop(row): |
| 63 | + """ |
| 64 | + Parses the row of an Excel sheet and makes a property from it |
| 65 | +
|
| 66 | + Args: |
| 67 | + row: the row of an Excel sheet |
| 68 | +
|
| 69 | + Returns: |
| 70 | + prop (JSON): the property in JSON format |
| 71 | + """ |
| 72 | + name, super_, object_, en, de, fr, it, gui_element, hlist = row |
| 73 | + labels = {} |
| 74 | + if en: |
| 75 | + labels['en'] = en |
| 76 | + if de: |
| 77 | + labels['de'] = de |
| 78 | + if fr: |
| 79 | + labels['fr'] = fr |
| 80 | + if it: |
| 81 | + labels['it'] = it |
| 82 | + if not labels: |
| 83 | + raise Exception(f"No label given in any of the four languages: {name}") |
| 84 | + prop = { |
| 85 | + 'name': name, |
| 86 | + 'super': [super_], |
| 87 | + 'object': object_, |
| 88 | + 'labels': labels, |
| 89 | + 'gui_element': gui_element |
| 90 | + } |
| 91 | + if hlist: |
| 92 | + prop['gui_attributes'] = {'hlist': hlist} |
| 93 | + return prop |
0 commit comments