py-models-parser


Namepy-models-parser JSON
Version 0.7.0 PyPI version JSON
download
home_pagehttps://github.com/xnuinside/py-models-parser
SummaryParser 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.
upload_time2023-08-17 08:23:08
maintainer
docs_urlNone
authorIuliia Volkova
requires_python>=3.7,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
Py-Models-Parser
----------------


.. image:: https://img.shields.io/pypi/v/py-models-parser
   :target: https://img.shields.io/pypi/v/py-models-parser
   :alt: badge1
 
.. image:: https://img.shields.io/pypi/l/py-models-parser
   :target: https://img.shields.io/pypi/l/py-models-parser
   :alt: badge2
 
.. image:: https://img.shields.io/pypi/pyversions/py-models-parser
   :target: https://img.shields.io/pypi/pyversions/py-models-parser
   :alt: badge3
 
.. image:: https://github.com/xnuinside/py-models-parser/actions/workflows/main.yml/badge.svg
   :target: https://github.com/xnuinside/py-models-parser/actions/workflows/main.yml/badge.svg
   :alt: 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:


* Sqlalchemy ORM (https://docs.sqlalchemy.org/en/14/orm/),
* Gino ORM (https://python-gino.org/),
* Tortoise ORM (https://tortoise-orm.readthedocs.io/en/latest/),
* Encode ORM (https://github.com/encode/orm)
* Django ORM Model (https://docs.djangoproject.com/en/3.2/topics/db/queries/),
* Pydantic (https://pydantic-docs.helpmanual.io/),
* Python Enum (https://docs.python.org/3/library/enum.html),
* Pony ORM (https://ponyorm.org/),
* Piccolo ORM models (https://piccolo-orm.readthedocs.io/en/latest/piccolo/schema/defining.html),
* Pydal Tables definitions (http://www.web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#The-DAL-A-quick-tour),
* Python Dataclasses (https://docs.python.org/3/library/dataclasses.html),
* pure Python Classes (https://docs.python.org/3/tutorial/classes.html#class-objects)

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:

.. code-block:: python


       [
           '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
--------------

.. code-block:: bash


       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


#. You can parse models from python string:

.. code-block:: python


   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)


#. Parse models from file:

.. code-block:: python


       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)


#. Parse models from file with command line

.. code-block:: bash


       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'

.. code-block:: bash


       # 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:

.. code-block:: python


       [
           {
               "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
---------------------


#. Add more tests for supported models
#. Add support for SQLAlchemy Core Tables

Changelog
---------

**v0.7.0**
Updates:


#. Added support for latest version of parsimonious.
   Library now works with Python 3.11.

**v0.6.0**
Features:


#. Added support for Encode ORM models https://github.com/encode/orm
#. Added support for Piccolo ORM models https://piccolo-orm.readthedocs.io/en/latest/piccolo/schema/defining.html

**v0.5.1**
Fixes:


#. Sometimes multiple parents names in "parents" output was joined in one string - fixed.

**v0.5.0**


#. Added base support for Pydal tables definitions
#. Added support for python list syntax like []

**v0.4.0**


#. return tuples (multiple values) is parsed correctly now
#. symbols like ``*&^%$#!±~``\ §<>` now does not cause any errors
#. classes without any args does not cause an error anymore

**v0.3.0**


#. Added cli - ``pmp`` command with args -d, --dump  
#. Added support for simple Django ORM models
#. Added base support for pure Python Classes

**v0.2.0**


#. Added support for Dataclasses
#. Added parse_from_file method
#. Added correct work with types with comma inside, like: Union[dict, list] or Union[dict, list, tuple, anything] 

**v0.1.1**


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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/xnuinside/py-models-parser",
    "name": "py-models-parser",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Iuliia Volkova",
    "author_email": "xnuinside@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/9d/9d/b5f2980db6d1dd64cab3db27687ef93343fcba07d5c67b9cf185bbe2e439/py_models_parser-0.7.0.tar.gz",
    "platform": null,
    "description": "\nPy-Models-Parser\n----------------\n\n\n.. image:: https://img.shields.io/pypi/v/py-models-parser\n   :target: https://img.shields.io/pypi/v/py-models-parser\n   :alt: badge1\n \n.. image:: https://img.shields.io/pypi/l/py-models-parser\n   :target: https://img.shields.io/pypi/l/py-models-parser\n   :alt: badge2\n \n.. image:: https://img.shields.io/pypi/pyversions/py-models-parser\n   :target: https://img.shields.io/pypi/pyversions/py-models-parser\n   :alt: badge3\n \n.. image:: https://github.com/xnuinside/py-models-parser/actions/workflows/main.yml/badge.svg\n   :target: https://github.com/xnuinside/py-models-parser/actions/workflows/main.yml/badge.svg\n   :alt: workflow\n\n\nIt's as second Parser that done by me, first is a https://github.com/xnuinside/simple-ddl-parser for SQL DDL with different dialects.\n\nPy-Models-Parser can parse & extract information from models & table definitions:\n\n\n* Sqlalchemy ORM (https://docs.sqlalchemy.org/en/14/orm/),\n* Gino ORM (https://python-gino.org/),\n* Tortoise ORM (https://tortoise-orm.readthedocs.io/en/latest/),\n* Encode ORM (https://github.com/encode/orm)\n* Django ORM Model (https://docs.djangoproject.com/en/3.2/topics/db/queries/),\n* Pydantic (https://pydantic-docs.helpmanual.io/),\n* Python Enum (https://docs.python.org/3/library/enum.html),\n* Pony ORM (https://ponyorm.org/),\n* Piccolo ORM models (https://piccolo-orm.readthedocs.io/en/latest/piccolo/schema/defining.html),\n* Pydal Tables definitions (http://www.web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#The-DAL-A-quick-tour),\n* Python Dataclasses (https://docs.python.org/3/library/dataclasses.html),\n* pure Python Classes (https://docs.python.org/3/tutorial/classes.html#class-objects)\n\nNumber of supported models will be increased, check 'TODO' section, if you want to have support of different models types - please open the issue.\n\nPy-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.\n\nPy-Models-Parser take as input different Python code with Models and provide output in standard form:\n\n.. code-block:: python\n\n\n       [\n           'name': 'ModelName',\n           'parents': ['BaseModel'], # class parents that defined in (), for example: `class MaterialType(str, Enum):` parents - str, Enum\n           'attrs':\n       {\n           'type': 'integer',\n           'name': 'attr_name',\n           'default': 'default_value',\n           'properties': {\n               ...\n           }\n       },\n       'properties': {\n           'table_name': ...\n       }\n       ]\n\nFor ORM models 'attrs' contains Columns of course.\n\n3 keys - 'type', 'name', 'default' exists in parse result 'attrs' of all Models\n'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.\n\nModel level 'properties' contains information relative to model, for example, if it ORM model - table_name\n\nNOTE: 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.\n\nHow to install\n--------------\n\n.. code-block:: bash\n\n\n       pip install py-models-parser\n\nHow to use\n----------\n\nLibrary detect automaticaly that type of models you tries to parse. You can check a lot of examples in test/ folder on the GitHub\n\n\n#. You can parse models from python string:\n\n.. code-block:: python\n\n\n   from py_models_parser import parse\n\n   models_str =  \"\"\"from gino import Gino\n\n   db = Gino()\n\n\n   class OrderItems(db.Model):\n\n       __tablename__ = 'order_items'\n\n       product_no = db.Column(db.Integer(), db.ForeignKey('products.product_no'), ondelete=\"RESTRICT\", primary_key=True)\n       order_id = db.Column(db.Integer(), db.ForeignKey('orders.order_id'), ondelete=\"CASCADE\", primary_key=True)\n       type = db.Column(db.Integer(), db.ForeignKey('types.type_id'), ondelete=\"RESTRICT\", onupdate=\"CASCADE\")\n\n       \"\"\"\n   result = parse(models_str)\n\n\n#. Parse models from file:\n\n.. code-block:: python\n\n\n       from py_models_parser import parse_from_file\n\n\n       file_path = \"path/to/your/models.py\"\n       # for example: tests/data/dataclass_defaults.py\n       result = parse_from_file(file_path)\n\n\n#. Parse models from file with command line\n\n.. code-block:: bash\n\n\n       pmp path_to_models.py \n\n       # for example: pmp tests/data/dataclass_defaults.py\n\nOutput 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'\n\n.. code-block:: bash\n\n\n       # example how to dump output from cli\n\n       pmp path_to_models.py -d target_file.json\n\nOutput example\n^^^^^^^^^^^^^^\n\nYou can find a lot of output examples in tests - https://github.com/xnuinside/py-models-parser/tree/main/tests\n\nFor model from point 1 (above) library will produce the result:\n\n.. code-block:: python\n\n\n       [\n           {\n               \"attrs\": [\n                   {\n                       \"default\": None,\n                       \"name\": \"product_no\",\n                       \"properties\": {\n                           \"foreign_key\": \"'products.product_no'\",\n                           \"ondelete\": '\"RESTRICT\"',\n                           \"primary_key\": \"True\",\n                       },\n                       \"type\": \"db.Integer()\",\n                   },\n                   {\n                       \"default\": None,\n                       \"name\": \"order_id\",\n                       \"properties\": {\n                           \"foreign_key\": \"'orders.order_id'\",\n                           \"ondelete\": '\"CASCADE\"',\n                           \"primary_key\": \"True\",\n                       },\n                       \"type\": \"db.Integer()\",\n                   },\n                   {\n                       \"default\": None,\n                       \"name\": \"type\",\n                       \"properties\": {\n                           \"foreign_key\": \"'types.type_id'\",\n                           \"ondelete\": '\"RESTRICT\"',\n                           \"onupdate\": '\"CASCADE\"',\n                       },\n                       \"type\": \"db.Integer()\",\n                   },\n               ],\n               \"name\": \"OrderItems\",\n               \"parents\": [\"db.Model\"],\n               \"properties\": {\"table_name\": \"'order_items'\"},\n           }\n       ]\n\nTODO: in next Release\n---------------------\n\n\n#. Add more tests for supported models\n#. Add support for SQLAlchemy Core Tables\n\nChangelog\n---------\n\n**v0.7.0**\nUpdates:\n\n\n#. Added support for latest version of parsimonious.\n   Library now works with Python 3.11.\n\n**v0.6.0**\nFeatures:\n\n\n#. Added support for Encode ORM models https://github.com/encode/orm\n#. Added support for Piccolo ORM models https://piccolo-orm.readthedocs.io/en/latest/piccolo/schema/defining.html\n\n**v0.5.1**\nFixes:\n\n\n#. Sometimes multiple parents names in \"parents\" output was joined in one string - fixed.\n\n**v0.5.0**\n\n\n#. Added base support for Pydal tables definitions\n#. Added support for python list syntax like []\n\n**v0.4.0**\n\n\n#. return tuples (multiple values) is parsed correctly now\n#. symbols like ``*&^%$#!\u00b1~``\\ \u00a7<>` now does not cause any errors\n#. classes without any args does not cause an error anymore\n\n**v0.3.0**\n\n\n#. Added cli - ``pmp`` command with args -d, --dump  \n#. Added support for simple Django ORM models\n#. Added base support for pure Python Classes\n\n**v0.2.0**\n\n\n#. Added support for Dataclasses\n#. Added parse_from_file method\n#. Added correct work with types with comma inside, like: Union[dict, list] or Union[dict, list, tuple, anything] \n\n**v0.1.1**\n\n\n#. Added base parser logic & tests for Pydantic, Enums, SQLAlchemy Models, GinoORM models, TortoiseORM models \n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "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.",
    "version": "0.7.0",
    "project_urls": {
        "Homepage": "https://github.com/xnuinside/py-models-parser",
        "Repository": "https://github.com/xnuinside/py-models-parser"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bf6c02707b22b225ea778cd1b7ac4536c40d0580698e0bbd6ac883a7c046d339",
                "md5": "542f6ca7d3292d67ea74f26453fa2055",
                "sha256": "68929f903a8c70f1dfe429a49ee71852caf663d1560bb09012342bfbf925ba71"
            },
            "downloads": -1,
            "filename": "py_models_parser-0.7.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "542f6ca7d3292d67ea74f26453fa2055",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7,<4.0",
            "size": 12396,
            "upload_time": "2023-08-17T08:23:06",
            "upload_time_iso_8601": "2023-08-17T08:23:06.258455Z",
            "url": "https://files.pythonhosted.org/packages/bf/6c/02707b22b225ea778cd1b7ac4536c40d0580698e0bbd6ac883a7c046d339/py_models_parser-0.7.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9d9db5f2980db6d1dd64cab3db27687ef93343fcba07d5c67b9cf185bbe2e439",
                "md5": "3292a7d2715e10a4f8a77deebb768c0e",
                "sha256": "ee34fb27f79e9158cb44c7df93fa99ac67475b16458eef9583d4ed39a5350608"
            },
            "downloads": -1,
            "filename": "py_models_parser-0.7.0.tar.gz",
            "has_sig": false,
            "md5_digest": "3292a7d2715e10a4f8a77deebb768c0e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7,<4.0",
            "size": 12636,
            "upload_time": "2023-08-17T08:23:08",
            "upload_time_iso_8601": "2023-08-17T08:23:08.086722Z",
            "url": "https://files.pythonhosted.org/packages/9d/9d/b5f2980db6d1dd64cab3db27687ef93343fcba07d5c67b9cf185bbe2e439/py_models_parser-0.7.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-17 08:23:08",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "xnuinside",
    "github_project": "py-models-parser",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "py-models-parser"
}
        
Elapsed time: 0.12379s