Last active
October 17, 2023 23:02
Python module to read SnowSQL config file for Snowflake connection information
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import configparser | |
from pathlib import Path | |
home = Path.home() | |
config_path = Path.home().joinpath( '.snowsql', 'config' ) | |
def main(): | |
print(f"Use as an import. User's home directory is {home}") | |
def retrieve(key): | |
config = configparser.ConfigParser() | |
config.read(f"{config_path}") | |
# Check if the connection section exists | |
if f"connections.{key}" not in config: | |
raise ValueError(f"The connection section 'connections.{key}' doesn't exist in the SnowSQL config file.") | |
connection_section = config[f"connections.{key}"] | |
# List of potential keys we want to retrieve | |
# Only account, user, and password are required | |
potential_keys = [ | |
("accountname", "account"), | |
("username", "user"), | |
("password", "password"), | |
("rolename", "role"), | |
("dbname", "database"), | |
("schemaname", "schema"), | |
("warehouse", "warehouse") | |
] | |
CONNECTION_PARAMETERS = {} | |
for config_key, output_key in potential_keys: | |
if config_key in connection_section: | |
CONNECTION_PARAMETERS[output_key] = connection_section[config_key] | |
return CONNECTION_PARAMETERS | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment