Skip to main content

Py-Models-Parser

PyPI version License Python versions Tests

It's a second parser that I've created. The first is simple-ddl-parser for SQL DDL with different dialects.

Py-Models-Parser can parse & extract information from models & table definitions:

Number of supported models will be increased. Check the 'TODO' section. If you want support for different model types, please open an issue.

Py-Models-Parser is written with PEG parser and its Python implementation - parsimonious. It's pretty new and I did not cover all possible test cases, so if you have an issue - please just open an issue with an example, I will fix it as soon as possible.

NOTE: This is a text parser, so it doesn't import or load your code. The parser works with source code as text, not objects in Python. So to run the parser you DO NOT NEED to install dependencies for models that you're trying to parse - only the models themselves.

Output Format

Py-Models-Parser takes different Python code with Models as input and provides output in a standard form:

[
    {
        'name': 'ModelName',
        'parents': ['BaseModel'],  # class parents defined in ()
        'attrs': [
            {
                'name': 'attr_name',
                'type': 'integer',
                'default': 'default_value',
                'properties': {
                    # additional info like foreign_key, server_default, etc.
                }
            }
        ],
        'properties': {
            'table_name': '...'  # model-level properties
        }
    }
]

For ORM models, 'attrs' contains columns. Three keys - 'type', 'name', 'default' - exist in all parsed model attrs. The 'properties' key contains additional information depending on Model type.

Installation

pip install py-models-parser

Usage

Parse from string

from py_models_parser import parse

models_str = """
from gino import Gino

db = Gino()

class OrderItems(db.Model):
    __tablename__ = 'order_items'

    product_no = db.Column(db.Integer(), db.ForeignKey('products.product_no'), ondelete="RESTRICT", primary_key=True)
    order_id = db.Column(db.Integer(), db.ForeignKey('orders.order_id'), ondelete="CASCADE", primary_key=True)
"""

result = parse(models_str)

Parse from file

from py_models_parser import parse_from_file

result = parse_from_file("path/to/your/models.py")

Parse OpenAPI/Swagger specifications

from py_models_parser import parse_openapi, parse_openapi_file

# Parse from string
openapi_spec = """
openapi: "3.0.0"
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
"""
result = parse_openapi(openapi_spec)

# Or parse from file (supports both YAML and JSON)
result = parse_openapi_file("path/to/openapi.yaml")

CLI

pmp path_to_models.py

# Dump output to JSON file
pmp path_to_models.py -d target_file.json

Output Example

For the GinoORM model above, the library produces:

[
    {
        "name": "OrderItems",
        "parents": ["db.Model"],
        "attrs": [
            {
                "name": "product_no",
                "type": "db.Integer()",
                "default": None,
                "properties": {
                    "foreign_key": "'products.product_no'",
                    "ondelete": '"RESTRICT"',
                    "primary_key": "True",
                },
            },
            {
                "name": "order_id",
                "type": "db.Integer()",
                "default": None,
                "properties": {
                    "foreign_key": "'orders.order_id'",
                    "ondelete": '"CASCADE"',
                    "primary_key": "True",
                },
            },
        ],
        "properties": {"table_name": "'order_items'"},
    }
]

You can find more output examples in tests.

TODO

  1. Add more tests for supported models
  2. Add support for SQLAlchemy Core Tables

Metadata

Release files for py-models-parser 1.0.0

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

Source distribution (sdist)

Source distribution for py-models-parser 1.0.0
File Size Uploaded
py_models_parser-1.0.0.tar.gz 13.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-models-parser 1.0.0
File Interpreter ABI Platform
py_models_parser-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.1 kB

Release files / py_models_parser-1.0.0.tar.gz

Download URL py_models_parser-1.0.0.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
38b40aeb2c04d0933e6ed1313ce7ae847ef2ddedf2d29e0069329e5442f21f5b
BLAKE2b-256 checksum
How to use checksums
3f5389de1ca534a5994e2e9db2d8e1cd2753ef02804fa492afdb9b62497b065c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.12

Release files / py_models_parser-1.0.0-py3-none-any.whl

Download URL py_models_parser-1.0.0-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
30d83bd7f13604f8868031b4e7d3c229234d6f0d0b0190fc978fef7cfbcaa848
BLAKE2b-256 checksum
How to use checksums
ef7661ed9c814aae90eefbb193e6bad9de4093e7fe9c7f6cd6b38e19bd187966
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.12

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

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