Skip to main content

swagger-dialect

SQLAlchemy Swagger definition reflection

Swagger_DBAPI : DBAPI compatible class that returns Swagger model definitions as:

  • Tables
  • Columns
  • Foreign key relationships

Very important to Note, currently a primary key identity column id is added to each extracted table definition

SwaggerDialect : SQLAlchemy dialect which can translate the schema extraction via Swagger_DBAPI into valid SQLAlchemy MetaData

Python 10 support only as use is made of the new match statement syntax

Here is an example where Python code is generated for SQLAlchemy from the Swagger definitions. (sqlacodegen>=3.0rc1)

import logging
import argparse
import sys
from contextlib import ExitStack
from typing import TextIO
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import MetaData
from sqlacodegen.generators import DeclarativeGenerator

from swagger_dialect import register_swagger_dialect

LOGGING_FORMAT = "[%(levelname)1.1s %(asctime)s.%(msecs)03d %(process)d %(module)s:%(lineno)d %(name)s] %(message)s"

register_swagger_dialect()


def get_args():
    parser = argparse.ArgumentParser(
        description="Generates SQLAlchemy model code from an existing database."
    )
    parser.add_argument(
        "--option", nargs="*", help="options passed to the generator class"
    )
    parser.add_argument(
        "--version", action="store_true", help="print the version number and exit"
    )
    parser.add_argument(
        "--schemas", help="load tables from the given schemas (comma separated)"
    )
    parser.add_argument(
        "--tables", help="tables to process (comma-separated, default: all)"
    )
    parser.add_argument("--noviews", action="store_true", help="ignore views")
    parser.add_argument("--outfile", help="file to write output to (default: stdout)")
    return parser.parse_args()


def run_reflection(url, model_file_name=None):

    # Use reflection to fill in the metadata
    engine = create_engine(url)
    metadata = MetaData()
    tables = None
    incl_views = False
    schemas = [None]
    for schema in schemas:
        metadata.reflect(engine, schema, incl_views, tables)

    args = get_args()
    generator = DeclarativeGenerator(metadata, engine, set(args.option or ()))

    # Open the target file (if given)
    with ExitStack() as stack:
        outfile: TextIO
        if model_file_name:
            outfile = open(model_file_name, "w", encoding="utf-8")
            stack.enter_context(outfile)
        else:
            outfile = sys.stdout
        outfile.write(generator.generate())

if __name__ == "__main__":
    run_reflection(url='swagger://MyAppSwagger.yaml', model_file_name="generated_model.py")

Metadata

Release files for swagger-dialect 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for swagger-dialect 0.0.3
File Interpreter ABI Platform Actions
swagger_dialect-0.0.3-py3-none-any.whl Python 3 none any Details for swagger_dialect-0.0.3-py3-none-any.whl

Release files / swagger_dialect-0.0.3-py3-none-any.whl

Download URL swagger_dialect-0.0.3-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
02aec99b7d29c8ad1f5f072ad865ca4172ca74cd556fbc9d364dc74c1e637c5a
BLAKE2b-256 checksum
How to use checksums
b5eedbb199f8d9b9f3c8eb2b00d3d7166e2bf3c818c004e07e77b9cf7966a008
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Security for swagger-dialect 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.3 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page