|
|
|
@ -9,6 +9,7 @@ import lxml.etree as ET |
|
|
|
import re |
|
|
|
import re |
|
|
|
import time |
|
|
|
import time |
|
|
|
import functools |
|
|
|
import functools |
|
|
|
|
|
|
|
import pickle |
|
|
|
import ModsTransformer as MT |
|
|
|
import ModsTransformer as MT |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -72,6 +73,17 @@ class ImportUtilities: |
|
|
|
cursor.execute(command) |
|
|
|
cursor.execute(command) |
|
|
|
self.conn.commit() |
|
|
|
self.conn.commit() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Adds node_id to table |
|
|
|
|
|
|
|
@timeit |
|
|
|
|
|
|
|
def add_dc_to_database(self, table, csv_file): |
|
|
|
|
|
|
|
cursor = self.conn.cursor() |
|
|
|
|
|
|
|
with open(csv_file, newline='') as csvfile: |
|
|
|
|
|
|
|
reader = csv.DictReader(csvfile) |
|
|
|
|
|
|
|
for row in reader: |
|
|
|
|
|
|
|
command = f"UPDATE {table} SET dublin_core = ? WHERE pid = ?" |
|
|
|
|
|
|
|
cursor.execute(command, (row['dublin_core'], row['PID'])) |
|
|
|
|
|
|
|
self.conn.commit() |
|
|
|
|
|
|
|
|
|
|
|
# Identifies object and datastream location within Fedora objectStores and datastreamStore. |
|
|
|
# Identifies object and datastream location within Fedora objectStores and datastreamStore. |
|
|
|
def dereference(self, identifier: str) -> str: |
|
|
|
def dereference(self, identifier: str) -> str: |
|
|
|
# Replace '+' with '/' in the identifier |
|
|
|
# Replace '+' with '/' in the identifier |
|
|
|
@ -149,13 +161,20 @@ class ImportUtilities: |
|
|
|
self.conn.commit() |
|
|
|
self.conn.commit() |
|
|
|
|
|
|
|
|
|
|
|
# Get all collection contents within namespace |
|
|
|
# Get all collection contents within namespace |
|
|
|
def get_collection_content_pids(self, table, collection): |
|
|
|
def get_collection_content_pids(self, table, collection, filename): |
|
|
|
cursor = self.conn.cursor() |
|
|
|
collection_pids = [collection] |
|
|
|
command = f"SELECT * from {table} where collection_pid = '{collection}'" |
|
|
|
parent_types = ['islandora:bookCModel', 'islandora:collectionCModel', ] |
|
|
|
pids = [] |
|
|
|
pids = [] |
|
|
|
for row in cursor.execute(command): |
|
|
|
cursor = self.conn.cursor() |
|
|
|
pids.append(row['pid']) |
|
|
|
while collection_pids: |
|
|
|
return pids |
|
|
|
collection = collection_pids.pop() |
|
|
|
|
|
|
|
command = f"SELECT * from {table} where collection_pid = '{collection}'" |
|
|
|
|
|
|
|
for row in cursor.execute(command): |
|
|
|
|
|
|
|
if row['content_model'] in parent_types: |
|
|
|
|
|
|
|
collection_pids.append(row['pid']) |
|
|
|
|
|
|
|
pids.append(row['pid']) |
|
|
|
|
|
|
|
with open(filename, 'wb') as file: |
|
|
|
|
|
|
|
pickle.dump(pids, file) |
|
|
|
|
|
|
|
|
|
|
|
# Utility function to prepare database selections for workbench |
|
|
|
# Utility function to prepare database selections for workbench |
|
|
|
def get_worksheet_details(self, content_model=None): |
|
|
|
def get_worksheet_details(self, content_model=None): |
|
|
|
@ -290,16 +309,17 @@ class ImportUtilities: |
|
|
|
cursor = self.conn.cursor() |
|
|
|
cursor = self.conn.cursor() |
|
|
|
result = cursor.execute(f"select dublin_core from {self.namespace} where pid = '{pid}'") |
|
|
|
result = cursor.execute(f"select dublin_core from {self.namespace} where pid = '{pid}'") |
|
|
|
dc = result.fetchone()['dublin_core'] |
|
|
|
dc = result.fetchone()['dublin_core'] |
|
|
|
root = ET.fromstring(dc) |
|
|
|
if dc: |
|
|
|
namespaces = { |
|
|
|
root = ET.fromstring(dc) |
|
|
|
'dc': 'http://purl.org/dc/elements/1.1/' |
|
|
|
namespaces = { |
|
|
|
} |
|
|
|
'dc': 'http://purl.org/dc/elements/1.1/' |
|
|
|
tags_and_values = [(elem.tag, elem.text) for elem in root.findall('.//dc:*', namespaces)] |
|
|
|
} |
|
|
|
dc_vals = {} |
|
|
|
tags_and_values = [(elem.tag, elem.text) for elem in root.findall('.//dc:*', namespaces)] |
|
|
|
for tag, value in tags_and_values: |
|
|
|
dc_vals = {} |
|
|
|
tag = re.sub(r"\{.*?\}", "", tag) |
|
|
|
for tag, value in tags_and_values: |
|
|
|
dc_vals[tag] = value |
|
|
|
tag = re.sub(r"\{.*?\}", "", tag) |
|
|
|
return dc_vals |
|
|
|
dc_vals[tag] = value |
|
|
|
|
|
|
|
return dc_vals |
|
|
|
|
|
|
|
|
|
|
|
def add_title(self): |
|
|
|
def add_title(self): |
|
|
|
cursor = self.conn.cursor() |
|
|
|
cursor = self.conn.cursor() |
|
|
|
@ -313,20 +333,20 @@ class ImportUtilities: |
|
|
|
self.conn.commit() |
|
|
|
self.conn.commit() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_relationships(self): |
|
|
|
def get_relationships(self, table): |
|
|
|
cursor = self.conn.cursor() |
|
|
|
cursor = self.conn.cursor() |
|
|
|
cursor.execute("SELECT pid, nid FROM ivoices") |
|
|
|
cursor.execute(f"SELECT pid, nid FROM {table}") |
|
|
|
pairs = cursor.fetchall() # Fetch all rows |
|
|
|
pairs = cursor.fetchall() # Fetch all rows |
|
|
|
relationships = [] |
|
|
|
relationships = [] |
|
|
|
for pid, nid in pairs: |
|
|
|
for pid, nid in pairs: |
|
|
|
if not pid: |
|
|
|
if not pid: |
|
|
|
continue |
|
|
|
continue |
|
|
|
cursor.execute("SELECT collection_pid FROM ivoices WHERE pid = ?", (pid,)) |
|
|
|
cursor.execute(f"SELECT collection_pid FROM {table} WHERE pid = ?", (pid,)) |
|
|
|
collection_pid = cursor.fetchone() |
|
|
|
collection_pid = cursor.fetchone() |
|
|
|
if collection_pid: # Ensure there is a valid collection_pid |
|
|
|
if collection_pid: # Ensure there is a valid collection_pid |
|
|
|
collection_pid = collection_pid[0] # Extract the value |
|
|
|
collection_pid = collection_pid[0] # Extract the value |
|
|
|
|
|
|
|
|
|
|
|
cursor.execute("SELECT nid FROM ivoices WHERE pid = ?", (collection_pid,)) |
|
|
|
cursor.execute(f"SELECT nid FROM {table} WHERE pid = ?", (collection_pid,)) |
|
|
|
nid_result = cursor.fetchone() |
|
|
|
nid_result = cursor.fetchone() |
|
|
|
|
|
|
|
|
|
|
|
if nid_result: # Ensure a valid nid is found |
|
|
|
if nid_result: # Ensure a valid nid is found |
|
|
|
@ -341,7 +361,7 @@ class ImportUtilities: |
|
|
|
|
|
|
|
|
|
|
|
def make_media_add_worksheet(self, input_file, output_file): |
|
|
|
def make_media_add_worksheet(self, input_file, output_file): |
|
|
|
with open(output_file, mode="w", newline="") as out_file: # Avoid shadowing `output_file` |
|
|
|
with open(output_file, mode="w", newline="") as out_file: # Avoid shadowing `output_file` |
|
|
|
writer = csv.DictWriter(out_file, fieldnames=['node_id', 'file']) |
|
|
|
writer = csv.DictWriter(out_file, fieldnames=['node_id', 'file', 'media_use_tid']) |
|
|
|
writer.writeheader() |
|
|
|
writer.writeheader() |
|
|
|
with open(input_file, "r") as file: |
|
|
|
with open(input_file, "r") as file: |
|
|
|
for line in file: # Iterate directly over lines |
|
|
|
for line in file: # Iterate directly over lines |
|
|
|
@ -349,9 +369,15 @@ class ImportUtilities: |
|
|
|
'node_id': line.split('_')[0], # Get everything before the first underscore |
|
|
|
'node_id': line.split('_')[0], # Get everything before the first underscore |
|
|
|
'file': line.strip() # Remove trailing newlines |
|
|
|
'file': line.strip() # Remove trailing newlines |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if 'MODS' in line: |
|
|
|
|
|
|
|
row['media_use_tid'] = 57 |
|
|
|
|
|
|
|
if 'PBCORE' in line: |
|
|
|
|
|
|
|
row['media_use_tid'] = 56 |
|
|
|
writer.writerow(row) |
|
|
|
writer.writerow(row) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
if __name__ == '__main__': |
|
|
|
MU = ImportUtilities('ivoices') |
|
|
|
MU = ImportUtilities('upei') |
|
|
|
MU.make_media_add_worksheet('inputs/ivoice_file.txt', 'outputs/ivoice_media.csv') |
|
|
|
print(MU.dereference('ivoices:ivoices20100403acass001')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|