bug fix: environment variable on default database
This commit is contained in:
parent
3494da8c6e
commit
8ca04e8c48
2
setup.py
2
setup.py
|
@ -8,7 +8,7 @@ def read(fname):
|
||||||
return open(os.path.join(os.path.dirname(__file__), fname)).read()
|
return open(os.path.join(os.path.dirname(__file__), fname)).read()
|
||||||
args = {
|
args = {
|
||||||
"name":"data-transport",
|
"name":"data-transport",
|
||||||
"version":"1.5.5",
|
"version":"1.5.6",
|
||||||
"author":"The Phi Technology LLC","author_email":"info@the-phi.com",
|
"author":"The Phi Technology LLC","author_email":"info@the-phi.com",
|
||||||
"license":"MIT",
|
"license":"MIT",
|
||||||
"packages":["transport"]}
|
"packages":["transport"]}
|
||||||
|
|
|
@ -61,8 +61,8 @@ class factory :
|
||||||
"console":{"class":{"write":Console,"read":Console}},
|
"console":{"class":{"write":Console,"read":Console}},
|
||||||
"file":{"class":{"read":disk.DiskReader,"write":disk.DiskWriter}},
|
"file":{"class":{"read":disk.DiskReader,"write":disk.DiskWriter}},
|
||||||
"sqlite":{"class":{"read":disk.SQLiteReader,"write":disk.SQLiteWriter}},
|
"sqlite":{"class":{"read":disk.SQLiteReader,"write":disk.SQLiteWriter}},
|
||||||
"postgresql":{"port":5432,"host":"localhost","database":os.environ['USER'],"driver":pg,"default":{"type":"VARCHAR"},"class":{"read":sql.SQLReader,"write":sql.SQLWriter}},
|
"postgresql":{"port":5432,"host":"localhost","driver":pg,"default":{"type":"VARCHAR"},"class":{"read":sql.SQLReader,"write":sql.SQLWriter}},
|
||||||
"redshift":{"port":5432,"host":"localhost","database":os.environ['USER'],"driver":pg,"default":{"type":"VARCHAR"},"class":{"read":sql.SQLReader,"write":sql.SQLWriter}},
|
"redshift":{"port":5432,"host":"localhost","driver":pg,"default":{"type":"VARCHAR"},"class":{"read":sql.SQLReader,"write":sql.SQLWriter}},
|
||||||
"bigquery":{"class":{"read":sql.BQReader,"write":sql.BQWriter}},
|
"bigquery":{"class":{"read":sql.BQReader,"write":sql.BQWriter}},
|
||||||
"mysql":{"port":3306,"host":"localhost","default":{"type":"VARCHAR(256)"},"driver":my,"class":{"read":sql.SQLReader,"write":sql.SQLWriter}},
|
"mysql":{"port":3306,"host":"localhost","default":{"type":"VARCHAR(256)"},"driver":my,"class":{"read":sql.SQLReader,"write":sql.SQLWriter}},
|
||||||
"mariadb":{"port":3306,"host":"localhost","default":{"type":"VARCHAR(256)"},"driver":my,"class":{"read":sql.SQLReader,"write":sql.SQLWriter}},
|
"mariadb":{"port":3306,"host":"localhost","default":{"type":"VARCHAR(256)"},"driver":my,"class":{"read":sql.SQLReader,"write":sql.SQLWriter}},
|
||||||
|
|
|
@ -98,7 +98,7 @@ class Console(Writer):
|
||||||
self.debug = self.write
|
self.debug = self.write
|
||||||
self.log = self.write
|
self.log = self.write
|
||||||
pass
|
pass
|
||||||
def write (self,**_args):
|
def write (self,logs,**_args):
|
||||||
if self.lock :
|
if self.lock :
|
||||||
Console.lock.acquire()
|
Console.lock.acquire()
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -33,37 +33,59 @@ class Mongo :
|
||||||
:username username for authentication
|
:username username for authentication
|
||||||
:password password for current user
|
:password password for current user
|
||||||
"""
|
"""
|
||||||
port = str(args['port']) if 'port' in args else '27017'
|
# port = str(args['port']) if 'port' in args else '27017'
|
||||||
host = args['host'] if 'host' in args else 'localhost'
|
# host = args['host'] if 'host' in args else 'localhost'
|
||||||
host = ":".join([host,port]) #-- Formatting host information here
|
# host = ":".join([host,port]) #-- Formatting host information here
|
||||||
self.uid = args['doc'] if 'doc' in args else None #-- document identifier
|
# self.uid = args['doc'] if 'doc' in args else None #-- document identifier
|
||||||
self.dbname = args['dbname'] if 'dbname' in args else args['db']
|
# self.dbname = args['dbname'] if 'dbname' in args else args['db']
|
||||||
authMechanism= 'SCRAM-SHA-256' if 'mechanism' not in args else args['mechanism']
|
self.authMechanism= 'SCRAM-SHA-256' if 'mechanism' not in args else args['mechanism']
|
||||||
authSource=(args['authSource'] if 'authSource' in args else self.dbname)
|
# authSource=(args['authSource'] if 'authSource' in args else self.dbname)
|
||||||
self._lock = False if 'lock' not in args else args['lock']
|
self._lock = False if 'lock' not in args else args['lock']
|
||||||
|
|
||||||
username = password = None
|
username = password = None
|
||||||
if 'username' in args and 'password' in args:
|
# if 'username' in args and 'password' in args:
|
||||||
username = args['username']
|
# username = args['username']
|
||||||
password=args['password']
|
# password=args['password']
|
||||||
if 'auth_file' in args :
|
if 'auth_file' in args :
|
||||||
_info = json.loads((open(args['auth_file'])).read())
|
_info = json.loads((open(args['auth_file'])).read())
|
||||||
username = _info['username']
|
# username = _info['username']
|
||||||
password = _info['password']
|
# password = _info['password']
|
||||||
if 'mechanism' in _info:
|
# if 'mechanism' in _info:
|
||||||
authMechanism = _info['mechanism']
|
# authMechanism = _info['mechanism']
|
||||||
if 'authSource' in _info:
|
# if 'authSource' in _info:
|
||||||
authSource = _info['authSource']
|
# authSource = _info['authSource']
|
||||||
|
# #
|
||||||
|
# # We are allowing the authentication file to set collection and databases too
|
||||||
|
# if 'db' in _info :
|
||||||
|
# self.dbname = _info['db']
|
||||||
|
# if 'doc' in _info :
|
||||||
|
# self.uid = _info['doc']
|
||||||
|
|
||||||
|
else:
|
||||||
|
_info = {}
|
||||||
|
_args = dict(args,**_info)
|
||||||
|
for key in _args :
|
||||||
|
if key in ['username','password'] :
|
||||||
|
username = _args['username'] if key=='username' else username
|
||||||
|
password = _args['password'] if key == 'password' else password
|
||||||
|
continue
|
||||||
|
value = _args[key]
|
||||||
|
|
||||||
|
self.setattr(key,value)
|
||||||
|
#
|
||||||
|
# Let us perform aliasing in order to remain backwards compatible
|
||||||
|
|
||||||
|
self.dbname = self.db if hasattr(self,'db')else self.dbname
|
||||||
|
self.uid = _args['table'] if 'table' in _args else (_args['doc'] if 'doc' in _args else (_args['collection'] if 'collection' in _args else None))
|
||||||
if username and password :
|
if username and password :
|
||||||
self.client = MongoClient(host,
|
self.client = MongoClient(self.host,
|
||||||
username=username,
|
username=username,
|
||||||
password=password ,
|
password=password ,
|
||||||
authSource=authSource,
|
authSource=self.authSource,
|
||||||
authMechanism=authMechanism)
|
authMechanism=self.authMechanism)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.client = MongoClient(host,maxPoolSize=10000)
|
self.client = MongoClient(self.host,maxPoolSize=10000)
|
||||||
|
|
||||||
self.db = self.client[self.dbname]
|
self.db = self.client[self.dbname]
|
||||||
|
|
||||||
|
@ -71,6 +93,11 @@ class Mongo :
|
||||||
p = self.dbname in self.client.list_database_names()
|
p = self.dbname in self.client.list_database_names()
|
||||||
q = self.uid in self.client[self.dbname].list_collection_names()
|
q = self.uid in self.client[self.dbname].list_collection_names()
|
||||||
return p and q
|
return p and q
|
||||||
|
def setattr(self,key,value):
|
||||||
|
_allowed = ['host','port','db','doc','authSource']
|
||||||
|
if key in _allowed :
|
||||||
|
setattr(self,key,value)
|
||||||
|
pass
|
||||||
def close(self):
|
def close(self):
|
||||||
self.client.close()
|
self.client.close()
|
||||||
|
|
||||||
|
@ -110,7 +137,9 @@ class MongoReader(Mongo,Reader):
|
||||||
else:
|
else:
|
||||||
collection = self.db[self.uid]
|
collection = self.db[self.uid]
|
||||||
_filter = args['filter'] if 'filter' in args else {}
|
_filter = args['filter'] if 'filter' in args else {}
|
||||||
return collection.find(_filter)
|
_df = pd.DataFrame(collection.find(_filter))
|
||||||
|
columns = _df.columns.tolist()[1:]
|
||||||
|
return _df[columns]
|
||||||
def view(self,**args):
|
def view(self,**args):
|
||||||
"""
|
"""
|
||||||
This function is designed to execute a view (map/reduce) operation
|
This function is designed to execute a view (map/reduce) operation
|
||||||
|
@ -162,7 +191,7 @@ class MongoWriter(Mongo,Writer):
|
||||||
@param info new record in the collection to be added
|
@param info new record in the collection to be added
|
||||||
"""
|
"""
|
||||||
# document = self.db[self.uid].find()
|
# document = self.db[self.uid].find()
|
||||||
collection = self.db[self.uid]
|
#collection = self.db[self.uid]
|
||||||
# if type(info) == list :
|
# if type(info) == list :
|
||||||
# self.db[self.uid].insert_many(info)
|
# self.db[self.uid].insert_many(info)
|
||||||
# else:
|
# else:
|
||||||
|
|
Loading…
Reference in New Issue