Skip to content

Commit

Permalink
Merge pull request #9 from JustinFrizzell/config-file-names
Browse files Browse the repository at this point in the history
Config file names
  • Loading branch information
JustinFrizzell authored Nov 29, 2023
2 parents 3fbd5bc + 548e5ec commit d093c85
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 34 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
</p>

**SQLconnect** is a Python package designed to simplify the process of connecting to SQL databases. It uses a `connections.yaml` file for database configuration and a `.env` file for secure credentials management. SQLconnect supports executing SQL from `.sql` files or Python strings, and retrieving data into pandas DataFrames. This package is particularly useful for data analysts and developers who need a straightforward way to interact with SQL databases.
**SQLconnect** is a Python package designed to simplify the process of connecting to SQL databases. It uses a `sqlconnect.yaml` file for database configuration and a `sqlconnect.env` file for secure credentials management. SQLconnect supports executing SQL from `.sql` files or Python strings, and retrieving data into pandas DataFrames. This package is particularly useful for data analysts and developers who need a straightforward way to interact with SQL databases.

## Features

- Allows easy configuration and management of database connections using a `connections.yaml` file.
- Allows easy configuration and management of database connections using a `sqlconnect.yaml` file.

- Supports the use of a `.env` file for secure storage of database credentials, enhancing security.
- Supports the use of a `sqlconnect.env` file for secure storage of database credentials, enhancing security.

- Leverages SQLAlchemy for database connections, providing a robust and flexible framework for SQL operations.

Expand All @@ -35,7 +35,7 @@ pip install sqlconnect

## Configuration

To use SQLconnect, create a `connections.yaml` file in the root of your project (or in your home directory) with the following example structure:
To use SQLconnect, create a `sqlconnect.yaml` file in the root of your project (or in your home directory) with the following example structure:

```yaml
connections:
Expand All @@ -44,13 +44,13 @@ connections:
odbc_driver: 'SQL+Server'
server: 'prod-server.database.com'
database: 'ProdDB'
username: '${DB_PROD_USERNAME}' # References DB_PROD_USERNAME in .env
password: '${DB_PROD_PASSWORD}' # References DB_PROD_PASSWORD in .env
username: '${DB_PROD_USERNAME}' # References DB_PROD_USERNAME in sqlconnect.env
password: '${DB_PROD_PASSWORD}' # References DB_PROD_PASSWORD in sqlconnect.env
options:
- 'Trusted_Connection=No'
```
Also create a `.env` file in the root of your project (or in your home directory) with the following example structure:
Also create a `sqlconnect.env` file in the root of your project (or in your home directory) with the following example structure:

```bash
# This file should be kept secure and not checked into version control (add to .gitignore)
Expand All @@ -68,7 +68,7 @@ Here's a quick example to get you started:
```python
import sqlconnect as sc
# Set up a database connection, all configuration is handled with connections.yaml and .env
# Set up a database connection, all configuration is handled with sqlconnect.yaml and sqlconnect.env
connection = sc.Sqlconnector("Database_PROD")
# Assign the results of a query to a pandas DataFrame
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</div>
<div style="height: 20px;"></div> <!-- Spacer -->

**SQLconnect** is a Python package designed to simplify the process of connecting to SQL databases. It uses a ``connections.yaml`` file for database configuration and a ``.env`` file for secure credentials management. SQLconnect supports executing SQL from ``.sql`` files or Python strings, and retrieving data into pandas DataFrames. This package is particularly useful for data analysts and developers who need a straightforward way to interact with SQL databases.
**SQLconnect** is a Python package designed to simplify the process of connecting to SQL databases. It uses a ``sqlconnect.yaml`` file for database configuration and a ``sqlconnect.env`` file for secure credentials management. SQLconnect supports executing SQL from ``.sql`` files or Python strings, and retrieving data into pandas DataFrames. This package is particularly useful for data analysts and developers who need a straightforward way to interact with SQL databases.

Contents
========
Expand Down
10 changes: 5 additions & 5 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Configuration

To use SQLconnect, create a `connections.yaml` file in the root of your project (or in your home directory) with the following example structure:
To use SQLconnect, create a `sqlconnect.yaml` file in the root of your project (or in your home directory) with the following example structure:

```yaml
connections:
Expand All @@ -11,13 +11,13 @@ connections:
odbc_driver: 'SQL+Server'
server: 'prod-server.database.com'
database: 'ProdDB'
username: '${DB_PROD_USERNAME}' # References DB_PROD_USERNAME in .env
password: '${DB_PROD_PASSWORD}' # References DB_PROD_PASSWORD in .env
username: '${DB_PROD_USERNAME}' # References DB_PROD_USERNAME in sqlconnect.env
password: '${DB_PROD_PASSWORD}' # References DB_PROD_PASSWORD in sqlconnect.env
options:
- 'Trusted_Connection=No'
```
Also create a `.env` file in the root of your project (or in your home directory) with the following example structure:
Also create a `sqlconnect.env` file in the root of your project (or in your home directory) with the following example structure:

```bash
# This file should be kept secure and not checked into version control (add to .gitignore)
Expand All @@ -35,7 +35,7 @@ Here's a quick example to get you started:
```python
import sqlconnect as sc
# Set up a database connection, all configuration is handled with connections.yaml and .env
# Set up a database connection, all configuration is handled with sqlconnect.yaml and sqlconnect.env
connection = sc.Sqlconnector("Database_PROD")
# Assign the results of a query to a pandas DataFrame
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setuptools.setup(
name="sqlconnect",
version="0.2.0",
version="0.2.1",
author="Justin Frizzell",
description="Package to simplify connections to SQL databases.",
long_description=Path("README.md").read_text(encoding="utf-8"),
Expand All @@ -12,7 +12,6 @@
project_urls={
"Documentation": "https://sqlconnect.readthedocs.io/en/latest/",
"Source": "https://github.com/JustinFrizzell/sqlconnect",
# You can add other links here if necessary
},
packages=setuptools.find_packages(),
install_requires=["pandas", "sqlalchemy", "pyyaml", "pyodbc", "python-dotenv"],
Expand Down
32 changes: 16 additions & 16 deletions sqlconnect/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
- os: Used for environment variable management.
- pathlib: For file path manipulations.
- yaml: Required for parsing YAML configuration files.
- dotenv: Used for loading environment variables from '.env' files.
- dotenv: Used for loading environment variables from 'sqlconnect.env' files.
Example Usage:
# Used within Sqlconnector class
Expand All @@ -29,7 +29,7 @@
- Configuration files should define connection parameters like 'sqlalchemy_driver', 'odbc_driver',
'server', 'database', and optionally 'username', 'password'.
- Usernames and passwords should be referenced as environment variables in the format '${ENV_VAR}'.
- Attempts to load '.env' files from the current directory or the user's home directory for environment
- Attempts to load 'sqlconnect.env' files from the current directory or the user's home directory for environment
variables.
"""
import os
Expand All @@ -52,7 +52,7 @@ def get_connection_config(connection_name: str, config_path: str = None) -> dict
The name of the connection for which the configuration is to be retrieved.
config_path : str, optional
The path to the configuration file. If not provided, the function searches
in 'connections.yaml' or 'connections.yml' in the current directory, and
in 'sqlconnect.yaml' or 'sqlconnect.yml' in the current directory, and
then in the user's home directory.
Returns
Expand Down Expand Up @@ -83,10 +83,10 @@ def get_connection_config(connection_name: str, config_path: str = None) -> dict
[Path(config_path)]
if config_path
else [
Path("connections.yaml"),
Path("connections.yml"),
Path.home() / "connections.yaml",
Path.home() / "connections.yml",
Path("sqlconnect.yaml"),
Path("sqlconnect.yml"),
Path.home() / "sqlconnect.yaml",
Path.home() / "sqlconnect.yml",
]
)

Expand Down Expand Up @@ -114,8 +114,8 @@ def get_connection_config(connection_name: str, config_path: str = None) -> dict
return connection_config

raise FileNotFoundError(
f"Config file not found in {Path('connections.yaml').absolute()} "
f"or {Path.home() / 'connections.yaml'}"
f"Config file not found in {Path('sqlconnect.yaml').absolute()} "
f"or {Path.home() / 'sqlconnect.yaml'}"
)


Expand Down Expand Up @@ -145,8 +145,8 @@ def get_db_url(connection_config: dict) -> str:
------
EnvironmentError
If 'username' and/or 'password' are specified as environment variables in the configuration
and these variables are not found in either the current directory's '.env' file or the
user's home directory '.env' file.
and these variables are not found in either the current directory's 'sqlconnect.env' file or the
user's home directory 'sqlconnect.env' file.
Examples
--------
Expand All @@ -163,7 +163,7 @@ def get_db_url(connection_config: dict) -> str:
Notes
-----
The function checks for '.env' files in the current directory and the user's home directory
The function checks for 'sqlconnect.env' files in the current directory and the user's home directory
for environment variables if 'username' and 'password' are provided as environment variable keys.
It uses `os.getenv` to retrieve these environment variables.
"""
Expand All @@ -177,10 +177,10 @@ def get_db_url(connection_config: dict) -> str:
# Check for username and password
auth_details = ""
if "username" in connection_config and "password" in connection_config:
if Path(".env").exists():
load_dotenv(Path(".env"))
if Path("sqlconnect.env").exists():
load_dotenv(Path("sqlconnect.env"))
else:
home_env_path = Path.home() / ".env"
home_env_path = Path.home() / "sqlconnect.env"
if home_env_path.exists():
load_dotenv(home_env_path)

Expand All @@ -192,7 +192,7 @@ def get_db_url(connection_config: dict) -> str:
if username is None or password is None:
raise EnvironmentError(
f"Environment variables '{env_username_key}' and/or '{env_password_key}' not "
f"found in {Path('.env').absolute()} or {Path.home() / '.env'}"
f"found in {Path('sqlconnect.env').absolute()} or {Path.home() / 'sqlconnect.env'}"
)

auth_details = f"{username}:{password}@"
Expand Down
2 changes: 1 addition & 1 deletion sqlconnect/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Example:
>>> import sqlconnect as sc
>>>
>>> # Set up a database connection. All configuration is handled with connections.yaml and .env
>>> # Set up a database connection. All configuration is handled with sqlconnect.yaml and sqlconnect.env
>>> connection = sc.Sqlconnector("Database_PROD")
>>>
>>> # Assign the results of a query to a pandas DataFrame
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sqlconnect_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@pytest.fixture
def mock_config_file_yaml(tmp_path):
# Create a temporary YAML config file for testing
config_file = tmp_path / "connections.yaml"
config_file = tmp_path / "sqlconnect.yaml"
yaml_content = yaml.dump(TEST_CONFIG_DICT)
config_file.write_text(yaml_content)

Expand Down

0 comments on commit d093c85

Please sign in to comment.