Skip to main content

Parser for Different Python Models (Pydantic, Enums, ORMs: Tortoise, SqlAlchemy, GinoORM, PonyORM, Pydal tables) to extract information about columns(attrs), model, table args,etc in one format.

Project description

Py-Models-Parser

badge1 badge2 badge3 workflow

It’s as second Parser that done by me, first is a https://github.com/xnuinside/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 ‘TODO’ section, if you want to have support of different models types - please open the issue.

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

Py-Models-Parser take as input different Python code with Models and provide output in standard form:

[
    'name': 'ModelName',
    'parents': ['BaseModel'], # class parents that defined in (), for example: `class MaterialType(str, Enum):` parents - str, Enum
    'attrs':
{
    'type': 'integer',
    'name': 'attr_name',
    'default': 'default_value',
    'properties': {
        ...
    }
},
'properties': {
    'table_name': ...
}
]

For ORM models ‘attrs’ contains Columns of course.

3 keys - ‘type’, ‘name’, ‘default’ exists in parse result ‘attrs’ of all Models ‘properties’ key contains additional information for attribut or column depend on Model type, for example, in ORM models it can contains ‘foreign_key’ key if this column used ForeignKey, or ‘server_default’ if it is a SqlAlchemy model or GinoORM.

Model level ‘properties’ contains information relative to model, for example, if it ORM model - table_name

NOTE: it’s is a text parser, so it don’t import or load your code, parser work with source code as text, not objects in Python. So to run parser you DO NOT NEED install dependencies for models, that you tries to parse - only models.

How to install

pip install py-models-parser

How to use

Library detect automaticaly that type of models you tries to parse. You can check a lot of examples in test/ folder on the GitHub

  1. You can parse models from python 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)
    type = db.Column(db.Integer(), db.ForeignKey('types.type_id'), ondelete="RESTRICT", onupdate="CASCADE")

    """
result = parse(models_str)
  1. Parse models from file:

from py_models_parser import parse_from_file


file_path = "path/to/your/models.py"
# for example: tests/data/dataclass_defaults.py
result = parse_from_file(file_path)
  1. Parse models from file with command line

pmp path_to_models.py

# for example: pmp tests/data/dataclass_defaults.py

Output from cli can be dumped in ‘output_models.json’ file - use flag ‘-d’ ‘–dump’ if you want to change target file name, provide it after argument like ‘-d target_file.json’

# example how to dump output from cli

pmp path_to_models.py -d target_file.json

Output example

You can find a lot of output examples in tests - https://github.com/xnuinside/py-models-parser/tree/main/tests

For model from point 1 (above) library will produce the result:

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

TODO: in next Release

  1. Add more tests for supported models (and fix existed not covered cases): Django ORM, Pydantic, Enums, Dataclasses, SQLAlchemy Models, GinoORM models, TortoiseORM models, PonyORM, for lists

  2. Add support for SQLAlchemy Core Tables

  3. Add support for Piccolo ORM models

Changelog

v0.5.0

  1. Added base support for Pydal tables definition

  2. Added support for python list syntax like []

v0.4.0

  1. return tuples (multiple values) is parsed correctly now

  2. symbols like *&^%$#!±~§<>` now does not cause any errors

  3. classes without any args does not cause an error anymore

v0.3.0

  1. Added cli - pmp command with args -d, –dump

  2. Added support for simple Django ORM models

  3. Added base support for pure Python Classes

v0.2.0

  1. Added support for Dataclasses

  2. Added parse_from_file method

  3. Added correct work with types with comma inside, like: Union[dict, list] or Union[dict, list, tuple, anything]

v0.1.1

  1. Added base parser logic & tests for Pydantic, Enums, SQLAlchemy Models, GinoORM models, TortoiseORM models

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py-models-parser-0.5.0.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

py_models_parser-0.5.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file py-models-parser-0.5.0.tar.gz.

File metadata

  • Download URL: py-models-parser-0.5.0.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.11 Darwin/19.6.0

File hashes

Hashes for py-models-parser-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e6e0553463aa0560eb4d849a25d852d7a926427b7e70698dcde1661b0e758ac0
MD5 0d2ba8dcd8983aa30f56165102b6f530
BLAKE2b-256 7eac01983e615903396d89a4bc777ddf94716b9b09659e0c70b26fb943c47649

See more details on using hashes here.

File details

Details for the file py_models_parser-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for py_models_parser-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7319b05598288a8588dcd57497572f51ea11a4dae57ed5597a749b2e63127525
MD5 1246d4c8681427c6c85b9102cf25b8dc
BLAKE2b-256 8fa4c7375affdee08a8d2ae3941f1456879659edf48f42dc99da4e71989c964e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page