Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def read_dsn(dsn: str):
"""
---------------------------------------------------------------------------
Reads the DSN information from the ODBCINI environment variable.
Parameters
----------
dsn: str
DSN name
Returns
-------
dict
dictionary with all the credentials
"""
check_types([("dsn", dsn, [str], False)])
f = open(os.environ['ODBCINI'], "r")
odbc = f.read()
f.close()
if ("[{}]".format(dsn) not in odbc):
raise ValueError("The DSN '{}' doesn't exist".format(dsn))
odbc = odbc.split("[{}]\n".format(dsn))[1].split("\n\n")[0].split("\n")
dsn = {}
for elem in odbc:
info = elem.replace(' ','').split('=')
dsn[info[0].lower()] = info[1]
return (dsn)
#---#
"""
---------------------------------------------------------------------------
Converts the ODBC dictionary obtained with the read_dsn method to the
vertica_python format.
Parameters
----------
dsn: str
DSN name
Returns
-------
dict
dictionary with all the credentials
"""
check_types([("dsn", dsn, [str], False)])
dsn = read_dsn(dsn)
conn_info = {'host': dsn["servername"], 'port': 5433, 'user': dsn["uid"], 'password': dsn["pwd"], 'database': dsn["database"]}
return (conn_info)
#---#
Method used to save the connection.
auto : uses vertica_python if vertica_python installed,
otherwise pyodbc, otherwise jaydebeapi.
pyodbc : ODBC.
jaydebeapi : JDBC.
vertica_python : Vertica Python Native Client (recommended).
name: str, optional
Name of the auto connection.
See Also
--------
change_auto_connection : Changes the current auto creation.
read_auto_connect : Automatically creates a connection.
vertica_cursor : Creates a Vertica Database cursor using the input method.
"""
check_types([
("dsn", dsn, [dict], False),
("method", method, ["auto", "pyodbc", "jaydebeapi", "vertica_python"], True)])
if ("port" not in dsn):
print("\u26A0 Warning: No port found in the 'dsn' dictionary. The default port is 5433.")
dsn["port"] = 5433
if ("user" not in dsn):
print("\u26A0 Warning: No user found in the 'dsn' dictionary. The default user is 'dbadmin'.")
dsn["user"] = "dbadmin"
if (method == "auto"):
try:
import vertica_python
method = "vertica_python"
except:
try:
import pyodbc
method = "pyodbc"