new commit
This commit is contained in:
parent
da296f8a64
commit
e46a9fd4ec
69 changed files with 4199 additions and 4805 deletions
79
01_importMaterialsAndTechnique.py
Normal file
79
01_importMaterialsAndTechnique.py
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
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 importMaterialsAndTechnique(api, engine):
|
||||
print('Importing materials and technique...')
|
||||
|
||||
tableName = 'c__5280_material'
|
||||
bundleId = 'b45978f2b073ff3c73b3c7220ebb3b89'
|
||||
|
||||
try:
|
||||
processedRows = pd.read_csv(f'./logs/{tableName}.csv')
|
||||
except FileNotFoundError:
|
||||
processedRows = pd.DataFrame(columns=['id', 'uuid', 'uri'])
|
||||
|
||||
# Load materials table
|
||||
sqlTable = pd.read_sql_table(tableName, con=engine)
|
||||
|
||||
# Create materials
|
||||
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 material {sqlTable.loc[index, "id"]}')
|
||||
continue
|
||||
# Create Entity property dicts
|
||||
materialValues = {}
|
||||
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]
|
||||
# If value is a list of comma-separated strings, split each item by ',' and flatten.
|
||||
if isinstance(value, list):
|
||||
new_value = []
|
||||
for v in value:
|
||||
if isinstance(v, str) and ',' in v:
|
||||
new_value.extend([x.strip() for x in v.split(',') if x.strip()])
|
||||
else:
|
||||
new_value.append(v)
|
||||
value = new_value
|
||||
|
||||
# Map columns to fields. We use assignments for reification.
|
||||
match key:
|
||||
case 'id':
|
||||
continue
|
||||
case 'f__uuid':
|
||||
materialValues['fedfe553c2332bd4902c887813f29ed8'] = value # UUID
|
||||
case 'f__5280_material':
|
||||
materialValues['f5f4251312f54c0d104ea87761b94bde'] = value # Material
|
||||
case 'f__5300_technik':
|
||||
materialValues['f231e08850022f091ebd5055d8aad30f'] = value # Technique
|
||||
case _:
|
||||
print(f'{key} is not a valid field, skipping.')
|
||||
|
||||
# Create Material
|
||||
material = Entity(api=api, fields=materialValues, bundle_id=bundleId)
|
||||
api.save(material)
|
||||
|
||||
print(f'Created material {index}: {material.uri} of {len(sqlTable)}')
|
||||
|
||||
# Write log
|
||||
processedRows = processedRows._append({'id': row['id'], 'uuid': materialValues['fedfe553c2332bd4902c887813f29ed8'][0], 'uri': material.uri}, ignore_index=True)
|
||||
processedRows.to_csv(f'./logs/{tableName}.csv', index=False)
|
||||
|
||||
print('finish')
|
||||
Loading…
Add table
Add a link
Reference in a new issue