data-transport/transport/disk.py

194 lines
5.2 KiB
Python
Raw Normal View History

import os
2020-05-18 02:57:18 +00:00
import sys
if sys.version_info[0] > 2 :
from transport.common import Reader, Writer #, factory
else:
from common import Reader,Writer
import json
2020-12-11 09:22:49 +00:00
# from threading import Lock
2020-07-28 02:47:02 +00:00
import sqlite3
2020-12-11 08:46:31 +00:00
import pandas as pd
2020-12-11 09:22:49 +00:00
from multiprocessing import Lock
class DiskReader(Reader) :
"""
This class is designed to read data from disk (location on hard drive)
@pre : isready() == True
"""
2020-07-28 02:47:02 +00:00
def __init__(self,**params):
"""
@param path absolute path of the file to be read
"""
Reader.__init__(self)
self.path = params['path'] ;
self.delimiter = params['delimiter'] if 'delimiter' in params else None
def isready(self):
return os.path.exists(self.path)
2020-05-18 02:57:18 +00:00
def read(self,**args):
"""
2019-12-02 03:32:47 +00:00
This function reads the rows from a designated location on disk
@param size number of rows to be read, -1 suggests all rows
"""
2020-05-18 02:57:18 +00:00
size = -1 if 'size' not in args else int(args['size'])
f = open(self.path,'rU')
i = 1
for row in f:
i += 1
if size == i:
break
if self.delimiter :
yield row.split(self.char)
yield row
f.close()
class DiskWriter(Writer):
2020-07-28 02:47:02 +00:00
"""
This function writes output to disk in a designated location. The function will write a text to a text file
- If a delimiter is provided it will use that to generate a xchar-delimited file
- If not then the object will be dumped as is
"""
2020-07-28 02:47:02 +00:00
THREAD_LOCK = Lock()
def __init__(self,**params):
Writer.__init__(self)
self.cache['meta'] = {'cols':0,'rows':0,'delimiter':None}
if 'path' in params:
self.path = params['path']
else:
self.path = 'data-transport.log'
self.delimiter = params['delimiter'] if 'delimiter' in params else None
# if 'name' in params:
# self.name = params['name'];
# else:
# self.name = 'data-transport.log'
2019-12-02 03:32:47 +00:00
# if os.path.exists(self.path) == False:
# os.mkdir(self.path)
def meta(self):
return self.cache['meta']
def isready(self):
"""
This function determines if the class is ready for execution or not
i.e it determines if the preconditions of met prior execution
"""
return True
# p = self.path is not None and os.path.exists(self.path)
# q = self.name is not None
# return p and q
def format (self,row):
self.cache['meta']['cols'] += len(row) if isinstance(row,list) else len(row.keys())
self.cache['meta']['rows'] += 1
return (self.delimiter.join(row) if self.delimiter else json.dumps(row))+"\n"
def write(self,info):
"""
This function writes a record to a designated file
@param label <passed|broken|fixed|stats>
@param row row to be written
"""
2020-07-28 02:47:02 +00:00
try:
DiskWriter.THREAD_LOCK.acquire()
f = open(self.path,'a')
if self.delimiter :
if type(info) == list :
for row in info :
f.write(self.format(row))
else:
f.write(self.format(info))
else:
if not type(info) == str :
2020-08-11 22:20:46 +00:00
f.write(json.dumps(info)+"\n")
2020-07-28 02:47:02 +00:00
else:
f.write(info)
f.close()
except Exception as e:
#
# Not sure what should be done here ...
pass
finally:
DiskWriter.THREAD_LOCK.release()
2020-09-22 18:30:04 +00:00
class SQLiteReader (DiskReader):
2020-09-26 21:53:33 +00:00
def __init__(self,**args):
2020-09-22 18:30:04 +00:00
DiskReader.__init__(self,**args)
self.conn = sqlite3.connect(self.path,isolation_level=None)
self.conn.row_factory = sqlite3.Row
self.table = args['table']
def read(self,**args):
if 'sql' in args :
sql = args['sql']
2020-09-26 21:53:33 +00:00
elif 'filter' in args :
2020-09-22 18:30:04 +00:00
sql = "SELECT :fields FROM ",self.table, "WHERE (:filter)".replace(":filter",args['filter'])
sql = sql.replace(":fields",args['fields']) if 'fields' in args else sql.replace(":fields","*")
2020-09-26 21:53:33 +00:00
return pd.read_sql(sql,self.conn)
2020-09-22 18:30:04 +00:00
def close(self):
try:
self.conn.close()
except Exception as e :
pass
2020-07-28 02:47:02 +00:00
class SQLiteWriter(DiskWriter) :
2020-12-11 09:22:49 +00:00
connection = None
LOCK = Lock()
2020-07-28 02:47:02 +00:00
def __init__(self,**args):
"""
:path
:fields json|csv
"""
DiskWriter.__init__(self,**args)
self.table = args['table']
self.conn = sqlite3.connect(self.path,isolation_level=None)
self.conn.row_factory = sqlite3.Row
self.fields = args['fields'] if 'fields' in args else []
2019-12-02 03:23:54 +00:00
2020-07-28 02:47:02 +00:00
if self.fields and not self.isready():
self.init(self.fields)
2020-12-11 09:22:49 +00:00
SQLiteWriter.connection = self.conn
2020-07-28 02:47:02 +00:00
def init(self,fields):
self.fields = fields;
sql = " ".join(["CREATE TABLE IF NOT EXISTS ",self.table," (", ",".join(self.fields),")"])
cursor = self.conn.cursor()
cursor.execute(sql)
cursor.close()
self.conn.commit()
def isready(self):
try:
sql = "SELECT count(*) FROM sqlite_master where name=':table'"
sql = sql.replace(":table",self.table)
cursor = self.conn.cursor()
r = cursor.execute(sql)
r = r.fetchall()
cursor.close()
return r[0][0]
except Exception as e:
pass
return 0
#
# If the table doesn't exist we should create it
#
def write(self,info):
"""
"""
if not self.fields :
self.init(list(info.keys()))
if type(info) != list :
info = [info]
2020-12-11 09:22:49 +00:00
SQLiteWriter.LOCK.acquire()
try:
cursor = self.conn.cursor()
sql = " " .join(["INSERT INTO ",self.table,"(", ",".join(self.fields) ,")", "values(':values')"])
for row in info :
stream = json.dumps(row)
stream = stream.replace("'","''")
cursor.execute(sql.replace(":values",stream) )
2020-07-28 02:47:02 +00:00
# self.conn.commit()
# print (sql)
2020-12-11 09:22:49 +00:00
except Exception as e :
pass
SQLiteWriter.LOCK.release()