73 lines
3 KiB
Python
73 lines
3 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 importMarkToSourceRelation(api, engine):
|
|
print('importing mark to source relation')
|
|
test = False
|
|
|
|
tableName = "r__mar__8130_que_kurzt_"
|
|
bundleId = 'b0edbf644e07765a5ae319802ec0289b' # Mark to source relation
|
|
|
|
try:
|
|
processedRows = pd.read_csv(f'./logs/{tableName}.csv')
|
|
except FileNotFoundError:
|
|
processedRows = pd.DataFrame(columns=[ 'id', 'uuid', 'uri'])
|
|
|
|
# Load sources table
|
|
sqlTable = pd.read_sql_table(tableName, con=engine)
|
|
|
|
entityValues = {}
|
|
|
|
# Create entities
|
|
for index, row in sqlTable.iterrows():
|
|
# For every row in table...
|
|
if index < len(processedRows) and sqlTable.loc[index, 'id'] == processedRows.loc[index, 'id']:
|
|
# skip if already processed
|
|
print(f'Skipping already processed entity {sqlTable.loc[index, "id"]}')
|
|
continue
|
|
# Create Entity property dicts
|
|
entityValues = {}
|
|
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.
|
|
docId = ''
|
|
match key:
|
|
case 'id':
|
|
docId = value[0]
|
|
case 'f__mar__uuid':
|
|
entityValues['ffe35cef0c5d28bbebe195436706fc7c'] = value # Date
|
|
fUuid = value[0]
|
|
case 'f__8130_que_kurzt___uuid':
|
|
entityValues['f86e4b7f52add5640b824a601c66a2f6'] = value # Note
|
|
case _:
|
|
print(f'{key} is not a valid field, skipping.')
|
|
|
|
# Create Material
|
|
entity = Entity(api=api, fields=entityValues, bundle_id=bundleId)
|
|
api.save(entity)
|
|
|
|
print(f'Created Mark to Source Relation {index}: {entity.uri} of {len(sqlTable)}')
|
|
|
|
# Write log
|
|
processedRows = processedRows._append({'id': row['id'], 'uuid': fUuid, 'uri': entity.uri}, ignore_index=True)
|
|
processedRows.to_csv(f'./logs/{tableName}.csv', index=False)
|
|
if test:
|
|
exit()
|
|
print('finished importing mark to source relation')
|