2019-09-17 04:08:43 +00:00
|
|
|
"""
|
|
|
|
Data Transport - 1.0
|
|
|
|
Steve L. Nyemba, The Phi Technology LLC
|
|
|
|
|
|
|
|
This module is designed to serve as a wrapper to a set of supported data stores :
|
|
|
|
- couchdb
|
|
|
|
- mongodb
|
|
|
|
- Files (character delimited)
|
2019-09-17 17:00:45 +00:00
|
|
|
- Queues (Rabbmitmq)
|
2019-09-17 04:08:43 +00:00
|
|
|
- Session (Flask)
|
|
|
|
- s3
|
|
|
|
The supported operations are read/write and providing meta data to the calling code
|
|
|
|
Requirements :
|
|
|
|
pymongo
|
|
|
|
boto
|
|
|
|
couldant
|
2020-02-01 15:42:45 +00:00
|
|
|
@TODO:
|
|
|
|
Enable read/writing to multiple reads/writes
|
2019-09-17 04:08:43 +00:00
|
|
|
"""
|
|
|
|
__author__ = 'The Phi Technology'
|
|
|
|
import numpy as np
|
|
|
|
import json
|
|
|
|
import importlib
|
|
|
|
# import couch
|
|
|
|
# import mongo
|
2020-02-01 15:42:45 +00:00
|
|
|
class IO:
|
|
|
|
def init(self,**args):
|
2019-09-17 04:08:43 +00:00
|
|
|
"""
|
2020-02-01 15:42:45 +00:00
|
|
|
This function enables attributes to be changed at runtime. Only the attributes defined in the class can be changed
|
|
|
|
Adding attributes will require sub-classing otherwise we may have an unpredictable class ...
|
2019-09-17 04:08:43 +00:00
|
|
|
"""
|
2020-02-01 15:42:45 +00:00
|
|
|
allowed = list(vars(self).keys())
|
|
|
|
for field in args :
|
|
|
|
if field not in allowed :
|
|
|
|
continue
|
|
|
|
value = args[field]
|
|
|
|
setattr(self,field,value)
|
|
|
|
class Reader (IO):
|
|
|
|
"""
|
|
|
|
This class is an abstraction of a read functionalities of a data store
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
def meta(self):
|
2019-09-17 04:08:43 +00:00
|
|
|
"""
|
2020-02-01 15:42:45 +00:00
|
|
|
This function is intended to return meta-data associated with what has just been read
|
|
|
|
@return object of meta data information associated with the content of the store
|
2019-09-17 04:08:43 +00:00
|
|
|
"""
|
2020-02-01 15:42:45 +00:00
|
|
|
raise Exception ("meta function needs to be implemented")
|
2020-05-18 02:57:18 +00:00
|
|
|
def read(self,**args):
|
2019-09-17 04:08:43 +00:00
|
|
|
"""
|
2020-02-01 15:42:45 +00:00
|
|
|
This function is intended to read the content of a store provided parameters to be used at the discretion of the subclass
|
2019-09-17 04:08:43 +00:00
|
|
|
"""
|
2020-02-01 15:42:45 +00:00
|
|
|
raise Exception ("read function needs to be implemented")
|
2019-09-17 04:08:43 +00:00
|
|
|
|
|
|
|
|
2020-02-01 15:42:45 +00:00
|
|
|
class Writer(IO):
|
|
|
|
def __init__(self):
|
|
|
|
self.cache = {"default":[]}
|
|
|
|
def log(self,**args):
|
|
|
|
self.cache[id] = args
|
|
|
|
def meta (self,id="default",**args):
|
|
|
|
raise Exception ("meta function needs to be implemented")
|
2019-09-17 04:08:43 +00:00
|
|
|
def format(self,row,xchar):
|
|
|
|
if xchar is not None and isinstance(row,list):
|
|
|
|
return xchar.join(row)+'\n'
|
|
|
|
elif xchar is None and isinstance(row,dict):
|
|
|
|
row = json.dumps(row)
|
|
|
|
return row
|
2020-02-01 15:42:45 +00:00
|
|
|
def write(self,**args):
|
|
|
|
"""
|
|
|
|
This function will write content to a store given parameters to be used at the discretion of the sub-class
|
|
|
|
"""
|
|
|
|
raise Exception ("write function needs to be implemented")
|
|
|
|
|
|
|
|
def archive(self):
|
|
|
|
"""
|
2019-09-17 04:08:43 +00:00
|
|
|
It is important to be able to archive data so as to insure that growth is controlled
|
|
|
|
Nothing in nature grows indefinitely neither should data being handled.
|
2020-02-01 15:42:45 +00:00
|
|
|
"""
|
|
|
|
raise Exception ("archive function needs to be implemented")
|
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
This function will close the persistent storage connection/handler
|
|
|
|
"""
|
2019-09-17 04:08:43 +00:00
|
|
|
pass
|
2020-02-01 15:42:45 +00:00
|
|
|
class ReadWriter(Reader,Writer) :
|
|
|
|
"""
|
|
|
|
This class implements the read/write functions aggregated
|
|
|
|
"""
|
|
|
|
pass
|
2019-09-17 04:08:43 +00:00
|
|
|
# class factory :
|
|
|
|
# @staticmethod
|
|
|
|
# def instance(**args):
|
|
|
|
# """
|
|
|
|
# This class will create an instance of a transport when providing
|
|
|
|
# :type name of the type we are trying to create
|
|
|
|
# :args The arguments needed to create the instance
|
|
|
|
# """
|
|
|
|
# source = args['type']
|
|
|
|
# params = args['args']
|
|
|
|
# anObject = None
|
|
|
|
|
|
|
|
# if source in ['HttpRequestReader','HttpSessionWriter']:
|
|
|
|
# #
|
|
|
|
# # @TODO: Make sure objects are serializable, be smart about them !!
|
|
|
|
# #
|
|
|
|
# aClassName = ''.join([source,'(**params)'])
|
|
|
|
|
|
|
|
|
|
|
|
# else:
|
|
|
|
|
|
|
|
# stream = json.dumps(params)
|
|
|
|
# aClassName = ''.join([source,'(**',stream,')'])
|
|
|
|
# try:
|
|
|
|
# anObject = eval( aClassName)
|
|
|
|
# #setattr(anObject,'name',source)
|
|
|
|
# except Exception,e:
|
|
|
|
# print ['Error ',e]
|
|
|
|
# return anObject
|