Module export.database in plugin tabular v0.5.0
Export database values.
Author(s) | Markus Binsteiner (markus@frkl.io) |
Tags | tabular |
Python class | kiara_plugin.tabular.modules.db.ExportNetworkDataModule |
Module configuration options
Configuration class: kiara.modules.included_core_modules.export_as.DataExportModuleConfig
Name | Description | Type | Required? | Default |
---|---|---|---|---|
source_type | The type of the source data that is going to be exported. | string | true | null |
target_profile | The name of the target profile. Used to distinguish different target formats for the same data type. | string | true | null |
constants | Value constants for this module. | object | false | null |
defaults | Value defaults for this module. | object | false | null |
Module source code
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}