Browse Source

Aloowed table use

main
astanley 2 years ago
parent
commit
a2194ebf2a
  1. 36
      GetFileSize.py
  2. 15
      ImportServerUtilities.py
  3. 37
      ImportUtilities.py

36
GetFileSize.py

@ -0,0 +1,36 @@
#!/usr/bin/env python3
import os
import argparse
def human_readable_size(size_bytes):
"""Convert bytes into a human-readable format (KB, MB, GB, etc.)."""
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
if size_bytes < 1024:
return f"{size_bytes:.2f} {unit}"
size_bytes /= 1024
return f"{size_bytes:.2f} PB"
def total_size_of_files(pattern, directory):
"""Calculate total size of all files in subdirectories that contain 'pattern' in their filename."""
total_size = 0
for root, _, files in os.walk(directory):
for file in files:
if pattern in file: # Match the pattern in filename
file_path = os.path.join(root, file)
total_size += os.path.getsize(file_path)
return human_readable_size(total_size)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Calculate total size of files matching a pattern in a directory.")
parser.add_argument("--pattern", required=True, help="Substring to match in filenames.")
parser.add_argument("--directory", required=True, help="Directory to search in.")
args = parser.parse_args()
total_size = total_size_of_files(args.pattern, args.directory)
print(f"Total size of all files containing '{args.pattern}' in '{args.directory}': {total_size}")

15
ImportServerUtilities.py

@ -134,9 +134,10 @@ class ImportServerUtilities:
print(f"FoXML file for {pid} is missing")
# Adds all MODS records from datastreamStore to database.
def add_mods_to_database(self):
@IU.ImportUtilities.timeit
def add_mods_to_database(self, table):
cursor = self.iu.conn.cursor()
pids = self.get_pids_from_objectstore(self.namespace)
pids = self.get_pids_from_objectstore(table)
for pid in pids:
foxml_file = self.iu.dereference(pid)
foxml = f"{self.objectStore}/{foxml_file}"
@ -152,9 +153,15 @@ class ImportServerUtilities:
mods_xml = fw.get_inline_mods()
if mods_xml:
mods_xml = mods_xml.replace("'", "''")
command = f"""UPDATE {self.namespace} set mods = '{mods_xml}' where pid = '{pid}'"""
command = f"""UPDATE {table} set mods = '{mods_xml}' where pid = '{pid}'"""
cursor.execute(command)
self.iu.conn.commit()
MS = ImportServerUtilities('namespace')
MS = ImportServerUtilities('island_archives')
tables = ['bdh', 'craipe', 'sdu', 'herbarium', 'islemag', 'leg', 'herbarium', 'islemag', 'pwc', 'peimag', 'lmmi']
for table in tables:
MS.add_mods_to_database(table)

37
ImportUtilities.py

@ -114,34 +114,36 @@ class ImportUtilities:
return pids
# Processes CSV returned from direct objectStore harvest
def process_full_institution(self, csv_file):
def process_full_institution(self, csv_file, table):
cursor = self.conn.cursor()
cursor.execute(f"""
CREATE TABLE if not exists {self.namespace}(
CREATE TABLE if not exists {table}(
title TEXT,
pid TEXT PRIMARY KEY,
nid TEXT,
content_model TEXT,
collection_pid TEXT,
page_of TEXT,
sequence TEXT,
constituent_of TEXT
dublin_core TEXT,
mods TEXT
)""")
self.conn.commit()
with open(csv_file, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
collection = row['collection_pid']
page_of = row['page_of']
if not page_of:
page_of = ' '
constituent_of = row['constituent_of']
if not constituent_of:
constituent_of = ' '
try:
command = f"INSERT OR REPLACE INTO {self.namespace} VALUES('{row['pid']}', '{row['content_model']}', '{collection}','{page_of}', '{row['sequence']}','{constituent_of}')"
cursor.execute(command)
except sqlite3.Error:
print(command)
print(row['pid'])
command = f"""
INSERT OR REPLACE INTO {table}
(title, pid, content_model, collection_pid, page_of, sequence, constituent_of)
VALUES (:title, :pid, :content_model, :collection_pid, :page_of, :sequence, :constituent_of)
"""
cursor.execute(command, row)
except sqlite3.Error as e:
print(f"SQLite Error: {e}") # Print the error message
print(f"SQL Command: {command}") # Optional: Print the SQL command for debugging
print(f"Parameters: {row}")
self.conn.commit()
# Get all collection contents within namespace
@ -310,15 +312,12 @@ class ImportUtilities:
cursor = self.conn.cursor()
cursor.execute("SELECT pid, nid FROM ivoices")
pairs = cursor.fetchall() # Fetch all rows
relationships = []
for pid, nid in pairs:
if not pid:
continue
cursor.execute("SELECT collection_pid FROM ivoices WHERE pid = ?", (pid,))
collection_pid = cursor.fetchone()
if collection_pid: # Ensure there is a valid collection_pid
collection_pid = collection_pid[0] # Extract the value
@ -335,5 +334,5 @@ class ImportUtilities:
if __name__ == '__main__':
MU = ImportUtilities('ivoices')
MU.example_function()
MU = ImportUtilities('island_archives')
MU.process_full_institution('inputs/sdu_complete.csv', 'sdu')

Loading…
Cancel
Save