|
|
@ -62,34 +62,25 @@ class DiskWriter(Writer):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
THREAD_LOCK = Lock()
|
|
|
|
THREAD_LOCK = Lock()
|
|
|
|
def __init__(self,**params):
|
|
|
|
def __init__(self,**params):
|
|
|
|
Writer.__init__(self)
|
|
|
|
super().__init__()
|
|
|
|
self.cache['meta'] = {'cols':0,'rows':0,'delimiter':None}
|
|
|
|
self._path = params['path']
|
|
|
|
if 'path' in params:
|
|
|
|
self._delimiter = params['delimiter']
|
|
|
|
self.path = params['path']
|
|
|
|
|
|
|
|
else:
|
|
|
|
# def meta(self):
|
|
|
|
self.path = 'data-transport.log'
|
|
|
|
# return self.cache['meta']
|
|
|
|
self.delimiter = params['delimiter'] if 'delimiter' in params else None
|
|
|
|
# def isready(self):
|
|
|
|
# if 'name' in params:
|
|
|
|
# """
|
|
|
|
# self.name = params['name'];
|
|
|
|
# This function determines if the class is ready for execution or not
|
|
|
|
# else:
|
|
|
|
# i.e it determines if the preconditions of met prior execution
|
|
|
|
# self.name = 'data-transport.log'
|
|
|
|
# """
|
|
|
|
# if os.path.exists(self.path) == False:
|
|
|
|
# return True
|
|
|
|
# os.mkdir(self.path)
|
|
|
|
# # p = self.path is not None and os.path.exists(self.path)
|
|
|
|
def meta(self):
|
|
|
|
# # q = self.name is not None
|
|
|
|
return self.cache['meta']
|
|
|
|
# # return p and q
|
|
|
|
def isready(self):
|
|
|
|
# def format (self,row):
|
|
|
|
"""
|
|
|
|
# self.cache['meta']['cols'] += len(row) if isinstance(row,list) else len(row.keys())
|
|
|
|
This function determines if the class is ready for execution or not
|
|
|
|
# self.cache['meta']['rows'] += 1
|
|
|
|
i.e it determines if the preconditions of met prior execution
|
|
|
|
# return (self.delimiter.join(row) if self.delimiter else json.dumps(row))+"\n"
|
|
|
|
"""
|
|
|
|
|
|
|
|
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,**_args):
|
|
|
|
def write(self,info,**_args):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
This function writes a record to a designated file
|
|
|
|
This function writes a record to a designated file
|
|
|
@ -97,21 +88,30 @@ class DiskWriter(Writer):
|
|
|
|
@param row row to be written
|
|
|
|
@param row row to be written
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
|
|
_mode = 'a' if 'overwrite' not in _args else 'w'
|
|
|
|
_mode = 'a' if 'overwrite' not in _args else 'w'
|
|
|
|
DiskWriter.THREAD_LOCK.acquire()
|
|
|
|
DiskWriter.THREAD_LOCK.acquire()
|
|
|
|
f = open(self.path,_mode)
|
|
|
|
# # _path = _args['path'] if 'path' in _args else self.path
|
|
|
|
if self.delimiter :
|
|
|
|
# # _delim= _args['delimiter'] if 'delimiter' in _args else self._delimiter
|
|
|
|
if type(info) == list :
|
|
|
|
# # info.to_csv(_path,sep=_delim)
|
|
|
|
for row in info :
|
|
|
|
# info.to_csv(self.path)
|
|
|
|
f.write(self.format(row))
|
|
|
|
# f = open(self.path,_mode)
|
|
|
|
else:
|
|
|
|
# if self.delimiter :
|
|
|
|
f.write(self.format(info))
|
|
|
|
# if type(info) == list :
|
|
|
|
else:
|
|
|
|
# for row in info :
|
|
|
|
if not type(info) == str :
|
|
|
|
# f.write(self.format(row))
|
|
|
|
f.write(json.dumps(info)+"\n")
|
|
|
|
# else:
|
|
|
|
else:
|
|
|
|
# f.write(self.format(info))
|
|
|
|
f.write(info)
|
|
|
|
# else:
|
|
|
|
f.close()
|
|
|
|
# if not type(info) == str :
|
|
|
|
|
|
|
|
# f.write(json.dumps(info)+"\n")
|
|
|
|
|
|
|
|
# else:
|
|
|
|
|
|
|
|
# f.write(info)
|
|
|
|
|
|
|
|
# f.close()
|
|
|
|
|
|
|
|
_delim = self._delimiter if 'delimiter' not in _args else _args['delimiter']
|
|
|
|
|
|
|
|
_path = self.path if 'path' not in _args else _args['path']
|
|
|
|
|
|
|
|
info.to_csv(_path,index=False,sep=_delim)
|
|
|
|
|
|
|
|
pass
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Not sure what should be done here ...
|
|
|
|
# Not sure what should be done here ...
|
|
|
@ -220,16 +220,19 @@ class SQLiteWriter(SQLite,DiskWriter) :
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# If the table doesn't exist we should create it
|
|
|
|
# If the table doesn't exist we should create it
|
|
|
|
#
|
|
|
|
#
|
|
|
|
def write(self,info):
|
|
|
|
def write(self,info,**_args):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
if not self.fields :
|
|
|
|
if not self.fields :
|
|
|
|
|
|
|
|
if type(info) == pd.DataFrame :
|
|
|
|
|
|
|
|
_columns = list(info.columns)
|
|
|
|
self.init(list(info.keys()))
|
|
|
|
self.init(list(info.keys()))
|
|
|
|
|
|
|
|
|
|
|
|
if type(info) == dict :
|
|
|
|
if type(info) == dict :
|
|
|
|
info = [info]
|
|
|
|
info = [info]
|
|
|
|
elif type(info) == pd.DataFrame :
|
|
|
|
elif type(info) == pd.DataFrame :
|
|
|
|
|
|
|
|
info = info.fillna('')
|
|
|
|
info = info.to_dict(orient='records')
|
|
|
|
info = info.to_dict(orient='records')
|
|
|
|
|
|
|
|
|
|
|
|
SQLiteWriter.LOCK.acquire()
|
|
|
|
SQLiteWriter.LOCK.acquire()
|
|
|
|