Skip to content

Commit

Permalink
Pyproject support for exclude/include args
Browse files Browse the repository at this point in the history
This also fixes a deprecation warning for a sqlalchemy function.
  • Loading branch information
tedivm committed Jun 2, 2024
1 parent 1a332cf commit 0ec71c8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ imports = [
]
```

This also works with excludes and includes.

```toml
[tool.paracelsus]
base = "example.base:Base"
imports = [
"example.models"
]
exclude_tables = [
"comments"
]
```

## Sponsorship

This project is developed by [Robert Hafner](https://blog.tedivm.com) If you find this project useful please consider sponsoring me using Github!
Expand Down
14 changes: 9 additions & 5 deletions paracelsus/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ def graph(
settings = get_pyproject_settings()
base_class = get_base_class(base_class_path, settings)

if settings and "imports" in settings:
if "imports" in settings:
import_module.extend(settings["imports"])

typer.echo(
get_graph_string(
base_class_path=base_class,
import_module=import_module,
include_tables=set(include_tables),
exclude_tables=set(exclude_tables),
include_tables=set(include_tables + settings.get("include_tables", [])),
exclude_tables=set(exclude_tables + settings.get("exclude_tables", [])),
python_dir=python_dir,
format=format.value,
)
Expand Down Expand Up @@ -141,12 +141,16 @@ def inject(
),
] = False,
):
settings = get_pyproject_settings()
if "imports" in settings:
import_module.extend(settings["imports"])

# Generate Graph
graph = get_graph_string(
base_class_path=base_class_path,
import_module=import_module,
include_tables=set(include_tables),
exclude_tables=set(exclude_tables),
include_tables=set(include_tables + settings.get("include_tables", [])),
exclude_tables=set(exclude_tables + settings.get("exclude_tables", [])),
python_dir=python_dir,
format=format.value,
)
Expand Down
8 changes: 7 additions & 1 deletion paracelsus/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,11 @@ def filter_metadata(
filtered_metadata = MetaData()
for tablename, table in metadata.tables.items():
if tablename in include_tables:
table.tometadata(filtered_metadata)
if hasattr(table, "to_metadata"):
# to_metadata is the new way to do this, but it's only available in newer versions of SQLAlchemy.
table = table.to_metadata(filtered_metadata)
else:
# tometadata is deprecated, but we still need to support it for older versions of SQLAlchemy.
table = table.tometadata(filtered_metadata)

return filtered_metadata
6 changes: 3 additions & 3 deletions paracelsus/pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import toml as tomllib # type: ignore


def get_pyproject_settings(dir: Path = Path(os.getcwd())) -> Dict[str, Any] | None:
def get_pyproject_settings(dir: Path = Path(os.getcwd())) -> Dict[str, Any]:
pyproject = dir / "pyproject.toml"

if not pyproject.exists():
return None
return {}

with open(pyproject, "rb") as f:
data = tomllib.loads(f.read().decode())

return data.get("tool", {}).get("paracelsus", None)
return data.get("tool", {}).get("paracelsus", {})
2 changes: 1 addition & 1 deletion tests/test_pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def test_pyproject(package_path):

def test_pyproject_none():
settings = get_pyproject_settings()
assert settings is None
assert settings == {}

0 comments on commit 0ec71c8

Please sign in to comment.