data-transport/transport/__init__.py

98 lines
3.1 KiB
Python
Raw Normal View History

"""
2022-01-29 17:15:45 +00:00
Data Transport, The Phi Technology LLC
Steve L. Nyemba, steve@the-phi.com
2022-01-29 17:15:45 +00:00
This library is designed to serve as a wrapper to a set of supported data stores :
- couchdb
- mongodb
- Files (character delimited)
- Queues (RabbmitMq)
- Session (Flask)
- s3
2022-01-29 17:15:45 +00:00
- sqlite
The supported operations are read/write and providing meta data to the calling code
2024-03-28 20:34:39 +00:00
We separated reads from writes to mitigate accidents associated with writes.
Source Code is available under MIT License:
https://healthcareio.the-phi.com/data-transport
https://hiplab.mc.vanderbilt.edu/git/hiplab/data-transport
"""
2024-02-27 18:37:16 +00:00
import numpy as np
2024-04-01 17:52:06 +00:00
2024-04-01 20:41:39 +00:00
import sql, nosql, cloud, other
2024-03-28 20:34:39 +00:00
import pandas as pd
import json
2021-11-18 21:21:26 +00:00
import os
2024-03-28 20:34:39 +00:00
from info import __version__,__author__
2024-04-01 17:52:06 +00:00
from transport.iowrapper import IWriter, IReader
from transport.plugins import PluginLoader
2024-04-01 20:41:39 +00:00
from transport import providers
2024-03-28 20:34:39 +00:00
PROVIDERS = {}
def init():
global PROVIDERS
for _module in [cloud,sql,nosql,other] :
for _provider_name in dir(_module) :
if _provider_name.startswith('__') :
continue
PROVIDERS[_provider_name] = {'module':getattr(_module,_provider_name),'type':_module.__name__}
def instance (**_args):
"""
type:
read: true|false (default true)
auth_file
"""
global PROVIDERS
if 'auth_file' in _args:
if os.path.exists(_args['auth_file']) :
f = open(_args['auth_file'])
_args = dict (_args,** json.loads(f.read()) )
f.close()
else:
filename = _args['auth_file']
raise Exception(f" {filename} was not found or is invalid")
if _args['provider'] in PROVIDERS :
_info = PROVIDERS[_args['provider']]
_module = _info['module']
if 'context' in _args :
_context = _args['context']
else:
_context = 'read'
2024-04-01 17:52:06 +00:00
_pointer = getattr(_module,'Reader') if _context == 'read' else getattr(_module,'Writer')
_agent = _pointer (**_args)
#
loader = None
if 'plugins' in _args :
_params = _args['plugins']
if 'path' in _params and 'names' in _params :
loader = PluginLoader(**_params)
elif type(_params) == list:
loader = PluginLoader()
for _delegate in _params :
loader.set(_delegate)
return IReader(_agent,loader) if _context == 'read' else IWriter(_agent,loader)
2024-03-28 20:34:39 +00:00
else:
raise Exception ("Missing or Unknown provider")
pass
def supported ():
_info = {}
for _provider in PROVIDERS :
_item = PROVIDERS[_provider]
if _item['type'] not in _info :
_info[_item['type']] = []
_info[_item['type']].append(_provider)
_df = pd.DataFrame()
for _id in _info :
if not _df.shape[0] :
_df = pd.DataFrame(_info[_id],columns=[_id.replace('transport.','')])
else:
_df = pd.DataFrame(_info[_id],columns=[_id.replace('transport.','')]).join(_df, how='outer')
return _df.fillna('')
class factory :
2024-03-28 20:34:39 +00:00
pass
factory.instance = instance
init()