class ExportNetworkDataModule(DataExportModule):
"""Export database values."""
_module_type_name = "export.database"
def export__database__as__sqlite_db(
self, value: KiaraDatabase, base_path: str, name: str
):
"""Export network data as a sqlite database file."""
target_path = os.path.abspath(os.path.join(base_path, f"{name}.sqlite"))
shutil.copy2(value.db_file_path, target_path)
return {"files": target_path}
def export__database__as__sql_dump(
self, value: KiaraDatabase, base_path: str, name: str
):
"""Export network data as a sql dump file."""
import sqlite_utils
db = sqlite_utils.Database(value.db_file_path)
target_path = Path(os.path.join(base_path, f"{name}.sql"))
with target_path.open("wt") as f:
for line in db.conn.iterdump():
f.write(line + "\n")
return {"files": target_path.as_posix()}
def export__database__as__csv_files(
self, value: KiaraDatabase, base_path: str, name: str
):
"""Export network data as 2 csv files (one for edges, one for nodes."""
import sqlite3
files = []
for table_name in value.table_names:
target_path = os.path.join(base_path, f"{name}__{table_name}.csv")
os.makedirs(os.path.dirname(target_path), exist_ok=True)
# copied from: https://stackoverflow.com/questions/2952366/dump-csv-from-sqlalchemy
con = sqlite3.connect(value.db_file_path)
outfile = open(target_path, "wt")
outcsv = csv.writer(outfile)
cursor = con.execute(f"select * from {table_name}")
# dump column titles (optional)
outcsv.writerow(x[0] for x in cursor.description)
# dump rows
outcsv.writerows(cursor.fetchall())
outfile.close()
files.append(target_path)
return {"files": files}