70 lines
3.2 KiB
Python
70 lines
3.2 KiB
Python
import uuid # For UUID creation
|
|
from initDb import initDb # For database initialization
|
|
from wisski.api import Api, Pathbuilder, Entity # For WissKI API
|
|
import os # For environment variable loading
|
|
from dotenv import load_dotenv # For environment variable loading
|
|
import pandas as pd # For dataframe handling
|
|
|
|
def importInspectionMarkRelation(api, engine):
|
|
print('Importing inspection mark relations...')
|
|
|
|
tableName = 'c__67b7_beziehung'
|
|
bundleId = 'bd9b0ff8dc3a6d9284e1798531389bf1'
|
|
|
|
try:
|
|
processedRows = pd.read_csv(f'./logs/{tableName}.csv')
|
|
except FileNotFoundError:
|
|
processedRows = pd.DataFrame(columns=['id', 'uuid', 'uri'])
|
|
|
|
# Load sources table
|
|
inspectionMarkRelationsTable = pd.read_sql_table(tableName, con=engine)
|
|
|
|
# Create inspectionMarkRelations
|
|
for index, row in inspectionMarkRelationsTable.iterrows():
|
|
# For every row in table...
|
|
if index < len(processedRows) and inspectionMarkRelationsTable.loc[index, 'id'] == processedRows.loc[index, 'id']:
|
|
# skip if already processed
|
|
print(f'Skipping already processed inspectionMarkRelation {inspectionMarkRelationsTable.loc[index, "id"]}')
|
|
continue
|
|
# Create Entity property dicts
|
|
inspectionMarkRelationValues = {}
|
|
for key, value in row.items():
|
|
# For every column in row...
|
|
if (value is None) or (value == ''):
|
|
# skip if cell has no value
|
|
continue
|
|
# Properties of an entity have to be an array, so...
|
|
value = str(value).replace('&###{{new_line}}###'.format(), '&')
|
|
value = str(value).replace('###{{new_line}}###', '&')
|
|
value = str(value).replace(' & ', '&')
|
|
if '&' in str(value):
|
|
# ...Explode "&"-separated values to array items
|
|
value = [x.strip() for x in str(value).split('&')]
|
|
else:
|
|
# ...Or parse to array
|
|
value = [value]
|
|
# Map columns to fields. We use assignments for reification.
|
|
match key:
|
|
case 'id':
|
|
continue
|
|
case 'f__uuid':
|
|
inspectionMarkRelationValues['ffd502413c286815811ae5546f73935b'] = value # UUID
|
|
case 'f__67b8_bez_bz_nr':
|
|
inspectionMarkRelationValues['ff3f6dd331ed27515f6721ac8312706c'] = value # Inspection Mark Identifier
|
|
case 'f__67b7_beziehung':
|
|
inspectionMarkRelationValues['f1cb8db7e1c26a5b5fe0c9d8fca60de2'] = value # Relation
|
|
|
|
case _:
|
|
print(f'{key} is not a valid field, skipping.')
|
|
|
|
# Create Material
|
|
inspectionMarkRelation = Entity(api=api, fields=inspectionMarkRelationValues, bundle_id='bd9b0ff8dc3a6d9284e1798531389bf1')
|
|
api.save(inspectionMarkRelation)
|
|
|
|
print(f'Created inspection mark relation {index}: {inspectionMarkRelation.uri}')
|
|
|
|
# Write log
|
|
processedRows = processedRows._append({'id': row['id'], 'uuid': inspectionMarkRelationValues['ffd502413c286815811ae5546f73935b'][0], 'uri': inspectionMarkRelation.uri}, ignore_index=True)
|
|
processedRows.to_csv(f'./logs/{tableName}.csv', index=False)
|
|
|
|
print('finish')
|