-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from tedivm/pyproject_toml
Support reading settings from pyproject.toml
- Loading branch information
Showing
14 changed files
with
266 additions
and
62 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ coverage.xml | |
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
.ruff_cache/ | ||
cover/ | ||
|
||
# Translations | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import importlib | ||
import os | ||
import sys | ||
from pathlib import Path | ||
from typing import List | ||
|
||
from .transformers.dot import Dot | ||
from .transformers.mermaid import Mermaid | ||
|
||
transformers = { | ||
"mmd": Mermaid, | ||
"mermaid": Mermaid, | ||
"dot": Dot, | ||
"gv": Dot, | ||
} | ||
|
||
|
||
def get_graph_string( | ||
base_class_path: str, | ||
import_module: List[str], | ||
python_dir: List[Path], | ||
format: str, | ||
) -> str: | ||
# Update the PYTHON_PATH to allow more module imports. | ||
sys.path.append(str(os.getcwd())) | ||
for dir in python_dir: | ||
sys.path.append(str(dir)) | ||
|
||
# Import the base class so the metadata class can be extracted from it. | ||
# The metadata class is passed to the transformer. | ||
module_path, class_name = base_class_path.split(":", 2) | ||
base_module = importlib.import_module(module_path) | ||
base_class = getattr(base_module, class_name) | ||
metadata = base_class.metadata | ||
|
||
# The modules holding the model classes have to be imported to get put in the metaclass model registry. | ||
# These modules aren't actually used in any way, so they are discarded. | ||
# They are also imported in scope of this function to prevent namespace pollution. | ||
for module in import_module: | ||
if ":*" in module: | ||
# Sure, execs are gross, but this is the only way to dynamically import wildcards. | ||
exec(f"from {module[:-2]} import *") | ||
else: | ||
importlib.import_module(module) | ||
|
||
# Grab a transformer. | ||
if format not in transformers: | ||
raise ValueError(f"Unknown Format: {format}") | ||
transformer = transformers[format] | ||
|
||
# Save the graph structure to string. | ||
return str(transformer(metadata)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import os | ||
import tomllib | ||
from pathlib import Path | ||
from typing import Any, Dict | ||
|
||
|
||
def get_pyproject_settings(dir: Path = Path(os.getcwd())) -> Dict[str, Any] | None: | ||
pyproject = dir / "pyproject.toml" | ||
|
||
if not pyproject.exists(): | ||
return None | ||
|
||
with open(pyproject, "rb") as f: | ||
data = tomllib.load(f) | ||
|
||
return data.get("tool", {}).get("paracelsus", None) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Test Directory | ||
|
||
Please ignore. | ||
|
||
## Schema | ||
|
||
<!-- BEGIN_SQLALCHEMY_DOCS --> | ||
<!-- END_SQLALCHEMY_DOCS --> |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from sqlalchemy.orm import declarative_base | ||
|
||
Base = declarative_base() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from datetime import UTC, datetime | ||
from uuid import uuid4 | ||
|
||
from sqlalchemy import Boolean, DateTime, ForeignKey, String, Text, Uuid | ||
from sqlalchemy.orm import mapped_column | ||
|
||
from .base import Base | ||
|
||
|
||
class User(Base): | ||
__tablename__ = "users" | ||
|
||
id = mapped_column(Uuid, primary_key=True, default=uuid4()) | ||
display_name = mapped_column(String(100)) | ||
created = mapped_column(DateTime, nullable=False, default=datetime.now(UTC)) | ||
|
||
|
||
class Post(Base): | ||
__tablename__ = "posts" | ||
|
||
id = mapped_column(Uuid, primary_key=True, default=uuid4()) | ||
author = mapped_column(ForeignKey(User.id), nullable=False) | ||
created = mapped_column(DateTime, nullable=False, default=datetime.now(UTC)) | ||
live = mapped_column(Boolean, default=False) | ||
content = mapped_column(Text, default="") | ||
|
||
|
||
class Comment(Base): | ||
__tablename__ = "comments" | ||
|
||
id = mapped_column(Uuid, primary_key=True, default=uuid4()) | ||
post = mapped_column(Uuid, ForeignKey(Post.id), default=uuid4()) | ||
author = mapped_column(ForeignKey(User.id), nullable=False) | ||
created = mapped_column(DateTime, nullable=False, default=datetime.now(UTC)) | ||
live = mapped_column(Boolean, default=False) | ||
content = mapped_column(Text, default="") | ||
content = mapped_column(Text, default="") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[tool.paracelsus] | ||
base = "example.base:Base" | ||
imports = [ | ||
"example.models" | ||
] |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from typer.testing import CliRunner | ||
|
||
from paracelsus.cli import app | ||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_graph(package_path): | ||
result = runner.invoke( | ||
app, ["graph", "example.base:Base", "--import-module", "example.models", "--python-dir", str(package_path)] | ||
) | ||
|
||
assert result.exit_code == 0 | ||
|
||
assert "users {" in result.stdout | ||
assert "posts {" in result.stdout | ||
assert "comments {" in result.stdout | ||
|
||
assert "users ||--o{ posts : author" in result.stdout | ||
assert "posts ||--o{ comments : post" in result.stdout | ||
assert "users ||--o{ comments : author" in result.stdout | ||
|
||
assert "CHAR(32) author FK" in result.stdout | ||
assert 'CHAR(32) post FK "nullable"' in result.stdout | ||
assert "DATETIME created" in result.stdout | ||
|
||
|
||
def test_inject(package_path): | ||
result = runner.invoke( | ||
app, | ||
[ | ||
"inject", | ||
str(package_path / "README.md"), | ||
"example.base:Base", | ||
"--import-module", | ||
"example.models", | ||
"--python-dir", | ||
str(package_path), | ||
"--check", | ||
], | ||
) | ||
|
||
assert result.exit_code == 1 | ||
|
||
|
||
def test_version(): | ||
result = runner.invoke(app, ["version"]) | ||
|
||
assert result.exit_code == 0 |
Oops, something went wrong.