Page MenuHomec4science

metadata_reader.py
No OneTemporary

File Metadata

Created
Tue, Jul 15, 21:58

metadata_reader.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
" module containing class metadataReader"
##########################################################################
import sqlite3
##########################################################################
class metadataReader(object):
"""
class metadataReader: object able to read and store in the local database some metadata contained in a csv file
"""
# ------------------------------------------------------------------ #
# Constructors/Destructors #
# ------------------------------------------------------------------ #
def __init__(self,filename,database_name):
"""__init__: metadata readers are initialised with the name of the file to read, the database where the information has to be stored ana a temporary dictionary containing information about one item (one line of the file) """
# Members ---------------------- #
# string filename
self.filename = filename
# dictionnary current_item
# (containing one set of values, corresponding to one line of a table in the database)
self.current_item = {}
# string database_name
self.database_name = database_name
def __del__(self):
"""__del__: not implemented """
pass
# ------------------------------------------------------------------ #
# Methods #
# ------------------------------------------------------------------ #
# public:
def read(self,filter_reader,key_string=None):
"""read: read a metadata csv file and call the function store of the present module to store metadata """
# 1st option : the information to store in the database are in a file
if self.filename != None :
metadata_file = open(self.filename,'r')
# the first line contains the name of the table
line = metadata_file.readline()
table_name = line.replace('\n','').split('\t')[0]
# the second line contains the name of the fieds
line = metadata_file.readline()
list_of_field_names = line.replace('\n','').split('\t')
# then each line contain information for one line of the table in the database
# see if can be enhance by checking if information already exists and overwrite only if new information is not 'NA'
line = metadata_file.readline()
while line :
# the values of the different fields are stored in a temporary list
list_of_values = line.replace('\n','').split('\t')
# then the dictionnary of field names and values is constructed ...
for i in range(0,len(list_of_field_names)):
self.current_item[list_of_field_names[i]] = list_of_values[i]
# ... so that the information can be stored in the database
self.store(table_name)
line = metadata_file.readline()
def store(self,table_name, item_to_store = None):
"""store: stores the information of one line of a table named 'table_name' in the database """
# if item_to_store was not provided in argument, it will be the current_item from the reader
if item_to_store == None:
item_to_store = self.current_item
# connection with the local database
connection = sqlite3.connect(self.database_name)
cursor = connection.cursor()
# here the commands are constructed from the names of the dictionary containing the information to store
command_line = "INSERT INTO " + table_name + "("
for key in item_to_store:
command_line += key + ","
command_line = command_line[:-1]
command_line += ") VALUES ("
for key in item_to_store:
command_line += ":" + key + ", "
command_line = command_line[:-2]
command_line += ")"
# execute the command to store the current item in the database
try:
cursor.execute(command_line, item_to_store)
# in case the data were already stored in the database
# can be inproved by asking if the data can be overwriten
except:
command_line = "REPLACE INTO " + table_name + "("
for key in item_to_store:
command_line += key + ","
command_line = command_line[:-1]
command_line += ") VALUES ("
for key in item_to_store:
command_line += ":" + key + ", "
command_line = command_line[:-2]
command_line += ")"
cursor.execute(command_line, item_to_store)
# commit the changes in the database
connection.commit()
# close the connection with the database
connection.close()
##########################################################################
if __name__ == '__main__':
test = metadataReader('/home/aline/spe_repository_aa/project/class_diagram/input_files/Metadata/performances.csv','/home/aline/spe_repository_aa/project/class_diagram/input_files/My_database/my_database.db')

Event Timeline