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 importJournalAssignment(api, engine): test = False tableName = "c__8310_zeitschrift" bundleId = 'b5508ef3bb28f139ebdd9f6d545825c4' 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) # 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__uuid': entityValues['fadaaac928ec555c2574b3a9a4f5543d'] = value # UUID fUuid = value[0] case 'f__8310_zeitschrift': entityValues['fd8fc741f6d4142637c061900b1cdd01'] = value # Client case 'f__8312_zusatzzschr': entityValues['f51edfb30c99d28bee1cf32b81190254'] = value # Date 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 journal assignment {index}: {entity.uri} of {len(sqlTable)}') # Write log processedRows = processedRows._append({'id': docId, 'uuid': fUuid, 'uri': entity.uri}, ignore_index=True) processedRows.to_csv(f'./logs/{tableName}.csv', index=False) if test: exit() print('finish')