{"id":16135284,"url":"https://github.com/xnuinside/py-models-parser","last_synced_at":"2025-03-18T15:31:22.945Z","repository":{"id":47697191,"uuid":"364658199","full_name":"xnuinside/py-models-parser","owner":"xnuinside","description":"Parser for Different Python Models (pure Python classes, Pydantic, Enums, ORMs: DjangoORM, Tortoise, SqlAlchemy, GinoORM, PonyORM, Pydal) to extract information about columns(attrs), model, table args,etc in one format.","archived":false,"fork":false,"pushed_at":"2023-08-17T08:22:00.000Z","size":134,"stargazers_count":18,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-14T09:05:36.919Z","etag":null,"topics":["classes","dataclasses","django-orm","gino-orm","hacktoberfest","models","orm","parser","ponyorm","pydal","pydantic","python","sqlalchemy","tortoise-orm"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xnuinside.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.txt","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-05T17:38:04.000Z","updated_at":"2024-12-30T22:26:01.000Z","dependencies_parsed_at":"2024-06-21T20:22:27.680Z","dependency_job_id":"6e0ac159-5448-4f40-b355-ad29aacf6cfc","html_url":"https://github.com/xnuinside/py-models-parser","commit_stats":{"total_commits":17,"total_committers":1,"mean_commits":17.0,"dds":0.0,"last_synced_commit":"c89f12f0c33e28dc86f74172246e4e4d9a7037a8"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fpy-models-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fpy-models-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fpy-models-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fpy-models-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xnuinside","download_url":"https://codeload.github.com/xnuinside/py-models-parser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243936375,"owners_count":20371504,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["classes","dataclasses","django-orm","gino-orm","hacktoberfest","models","orm","parser","ponyorm","pydal","pydantic","python","sqlalchemy","tortoise-orm"],"created_at":"2024-10-09T23:06:44.360Z","updated_at":"2025-03-18T15:31:22.569Z","avatar_url":"https://github.com/xnuinside.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Py-Models-Parser\n\n![badge1](https://img.shields.io/pypi/v/py-models-parser) ![badge2](https://img.shields.io/pypi/l/py-models-parser) ![badge3](https://img.shields.io/pypi/pyversions/py-models-parser) ![workflow](https://github.com/xnuinside/py-models-parser/actions/workflows/main.yml/badge.svg)\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 \u0026 extract information from models \u0026 table definitions:\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```python\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```\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\n## How to install\n\n```bash\n\n    pip install py-models-parser\n\n```\n\n## How to use\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\n1. You can parse models from python string:\n\n```python\n\nfrom py_models_parser import parse\n\nmodels_str =  \"\"\"from gino import Gino\n\ndb = Gino()\n\n\nclass 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    \"\"\"\nresult = parse(models_str)\n\n```\n\n2. Parse models from file:\n\n```python\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\n3. Parse models from file with command line\n\n```bash\n\n    pmp path_to_models.py \n\n    # for example: pmp tests/data/dataclass_defaults.py\n\n```\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```bash\n\n    # example how to dump output from cli\n\n    pmp path_to_models.py -d target_file.json\n\n```\n\n### Output example\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```python\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```\n\n## TODO: in next Release\n\n1. Add more tests for supported models\n2. Add support for SQLAlchemy Core Tables\n\n\n## Changelog\n**v0.7.0**\nUpdates:\n1. Added support for latest version of parsimonious.\nLibrary now works with Python 3.11.\n\n\n**v0.6.0**\nFeatures:\n1. Added support for Encode ORM models https://github.com/encode/orm\n2. Added support for Piccolo ORM models https://piccolo-orm.readthedocs.io/en/latest/piccolo/schema/defining.html\n\n\n**v0.5.1**\nFixes:\n1. Sometimes multiple parents names in \"parents\" output was joined in one string - fixed.\n\n**v0.5.0**\n1. Added base support for Pydal tables definitions\n2. Added support for python list syntax like []\n\n**v0.4.0**\n1. return tuples (multiple values) is parsed correctly now\n2. symbols like `*\u0026^%$#!±~`§\u003c\u003e` now does not cause any errors\n3. classes without any args does not cause an error anymore\n\n**v0.3.0**\n1. Added cli - `pmp` command with args -d, --dump  \n2. Added support for simple Django ORM models\n3. Added base support for pure Python Classes\n\n**v0.2.0**\n1. Added support for Dataclasses\n2. Added parse_from_file method\n3. Added correct work with types with comma inside, like: Union[dict, list] or Union[dict, list, tuple, anything] \n\n**v0.1.1**\n1. Added base parser logic \u0026 tests for Pydantic, Enums, SQLAlchemy Models, GinoORM models, TortoiseORM models \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxnuinside%2Fpy-models-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxnuinside%2Fpy-models-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxnuinside%2Fpy-models-parser/lists"}