You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
5.9 KiB
Python
159 lines
5.9 KiB
Python
#!/usr/bin/env python
|
|
__doc__ = """
|
|
(c) 2018 - 2021 data-transport
|
|
steve@the-phi.com, The Phi Technology LLC
|
|
https://dev.the-phi.com/git/steve/data-transport.git
|
|
|
|
This program performs ETL between 9 supported data sources : Couchdb, Mongodb, Mysql, Mariadb, PostgreSQL, Netezza,Redshift, Sqlite, File
|
|
LICENSE (MIT)
|
|
Copyright 2016-2020, The Phi Technology LLC
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
Usage :
|
|
transport help -- will print this page
|
|
|
|
transport move <path> [index]
|
|
<path> path to the configuration file
|
|
<index> optional index within the configuration file
|
|
|
|
e.g: configuration file (JSON formatted)
|
|
- single source to a single target
|
|
|
|
{"source":{"provider":"http","url":"https://cdn.wsform.com/wp-content/uploads/2020/06/agreement.csv"}
|
|
"target":{"provider":"sqlite3","path":"transport-demo.sqlite","table":"agreement"}
|
|
}
|
|
|
|
- single source to multiple targets
|
|
{
|
|
"source":{"provider":"http","url":"https://cdn.wsform.com/wp-content/uploads/2020/06/agreement.csv"},
|
|
"target":[
|
|
{"provider":"sqlite3","path":"transport-demo.sqlite","table":"agreement},
|
|
{"provider":"mongodb","db":"transport-demo","collection":"agreement"}
|
|
]
|
|
}
|
|
|
|
"""
|
|
import pandas as pd
|
|
import numpy as np
|
|
import json
|
|
import sys
|
|
import transport
|
|
import time
|
|
from multiprocessing import Process
|
|
|
|
import os
|
|
import transport
|
|
from transport import etl
|
|
# from transport import providers
|
|
import typer
|
|
from typing_extensions import Annotated
|
|
from typing import Optional
|
|
import time
|
|
|
|
app = typer.Typer()
|
|
|
|
# @app.command()
|
|
def help() :
|
|
print (__doc__)
|
|
def wait(jobs):
|
|
while jobs :
|
|
jobs = [thread for thread in jobs if thread.is_alive()]
|
|
time.sleep(1)
|
|
|
|
@app.command(name="apply")
|
|
def apply (path:Annotated[str,typer.Argument(help="path of the configuration file")],
|
|
index:int = typer.Option(help="index of the item of interest, otherwise everything in the file will be processed")):
|
|
"""
|
|
This function applies data transport from one source to one or several others
|
|
"""
|
|
# _proxy = lambda _object: _object.write(_object.read())
|
|
if os.path.exists(path):
|
|
file = open(path)
|
|
_config = json.loads (file.read() )
|
|
file.close()
|
|
if index :
|
|
_config = [_config[ int(index)]]
|
|
jobs = []
|
|
for _args in _config :
|
|
pthread = etl.instance(**_args) #-- automatically starts the process
|
|
jobs.append(pthread)
|
|
#
|
|
# @TODO: Log the number of processes started and estimated time
|
|
while jobs :
|
|
jobs = [pthread for pthread in jobs if pthread.is_alive()]
|
|
time.sleep(1)
|
|
#
|
|
# @TODO: Log the job termination here ...
|
|
@app.command(name="providers")
|
|
def supported (format:Annotated[str,typer.Argument(help="format of the output, supported formats are (list,table,json)")]="table") :
|
|
"""
|
|
This function will print supported providers/vendors and their associated classifications
|
|
"""
|
|
_df = (transport.supported())
|
|
if format in ['list','json'] :
|
|
print (json.dumps(_df.to_dict(orient="list")))
|
|
else:
|
|
print (_df)
|
|
print ()
|
|
|
|
@app.command()
|
|
def version():
|
|
"""
|
|
This function will display version and license information
|
|
"""
|
|
|
|
print (transport.__app_name__,'version ',transport.__version__)
|
|
print (transport.__license__)
|
|
|
|
@app.command()
|
|
def generate (path:Annotated[str,typer.Argument(help="path of the ETL configuration file template (name included)")]):
|
|
"""
|
|
This function will generate a configuration template to give a sense of how to create one
|
|
"""
|
|
_config = [
|
|
{
|
|
"source":{"provider":"http","url":"https://raw.githubusercontent.com/codeforamerica/ohana-api/master/data/sample-csv/addresses.csv"},
|
|
"target":
|
|
[{"provider":"files","path":"addresses.csv","delimiter":","},{"provider":"sqlite","database":"sample.db3","table":"addresses"}]
|
|
}
|
|
]
|
|
file = open(path,'w')
|
|
file.write(json.dumps(_config))
|
|
file.close()
|
|
if __name__ == '__main__' :
|
|
app()
|
|
# #
|
|
# # Load information from the file ...
|
|
# if 'help' in SYS_ARGS :
|
|
# print (__doc__)
|
|
# else:
|
|
# try:
|
|
# _info = json.loads(open(SYS_ARGS['config']).read())
|
|
# if 'index' in SYS_ARGS :
|
|
# _index = int(SYS_ARGS['index'])
|
|
# _info = [_item for _item in _info if _info.index(_item) == _index]
|
|
# pass
|
|
# elif 'id' in SYS_ARGS :
|
|
# _info = [_item for _item in _info if 'id' in _item and _item['id'] == SYS_ARGS['id']]
|
|
|
|
# procs = 1 if 'procs' not in SYS_ARGS else int(SYS_ARGS['procs'])
|
|
# jobs = transport.factory.instance(provider='etl',info=_info,procs=procs)
|
|
# print ([len(jobs),' Jobs are running'])
|
|
# N = len(jobs)
|
|
# while jobs :
|
|
# x = len(jobs)
|
|
# jobs = [_job for _job in jobs if _job.is_alive()]
|
|
# if x != len(jobs) :
|
|
# print ([len(jobs),'... jobs still running'])
|
|
# time.sleep(1)
|
|
# print ([N,' Finished running'])
|
|
# except Exception as e:
|
|
|
|
# print (e)
|
|
|
|
|