|
| 1 | + |
| 2 | +Py-Models-Parser |
| 3 | +---------------- |
| 4 | + |
| 5 | +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. |
| 6 | +Py-Models-Parser supports now ORM Sqlalchemy, Gino, Tortoise; Pydantic, Python Enum models & in nearest feature I plan to add Dataclasses & pure pyton classes. And next will be added other ORMs models. |
| 7 | + |
| 8 | +Py-Models-Parser written with PEG parser and it's python implementation - parsimonious. |
| 9 | +Py-Models-Parser take as input different Python code with Models and provide output in standard form: |
| 10 | + |
| 11 | +.. code-block:: python |
| 12 | +
|
| 13 | +
|
| 14 | + [ |
| 15 | + 'name': 'ModelName', |
| 16 | + 'parents': ['BaseModel'], # class parents that defined in (), for example: `class MaterialType(str, Enum):` parents - str, Enum |
| 17 | + 'attrs': |
| 18 | + { |
| 19 | + 'type': 'integer', |
| 20 | + 'name': 'attr_name', |
| 21 | + 'default': 'default_value', |
| 22 | + 'properties': { |
| 23 | + ... |
| 24 | + } |
| 25 | + }, |
| 26 | + 'properties': { |
| 27 | + 'table_name': ... |
| 28 | + } |
| 29 | + ] |
| 30 | +
|
| 31 | +For ORM models 'attrs' contains Columns of course. |
| 32 | + |
| 33 | +3 keys - 'type', 'name', 'default' exists in parse result 'attrs' of all Models |
| 34 | +'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. |
| 35 | + |
| 36 | +Model level 'properties' contains information relative to model, for example, if it ORM model - table_name |
| 37 | + |
| 38 | +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. |
| 39 | + |
| 40 | +How to install |
| 41 | +-------------- |
| 42 | + |
| 43 | +.. code-block:: bash |
| 44 | +
|
| 45 | +
|
| 46 | + pip install py-models-parser |
| 47 | +
|
| 48 | +How to use |
| 49 | +---------- |
| 50 | + |
| 51 | +Library detect automaticaly that type of models you tries to parse. You can check a lot of examples in test/ folder on the GitHub |
| 52 | + |
| 53 | +You can parse models from python string: |
| 54 | + |
| 55 | +.. code-block:: python |
| 56 | +
|
| 57 | +
|
| 58 | + from py_models_parser.core import parse |
| 59 | +
|
| 60 | + models_str = """from gino import Gino |
| 61 | +
|
| 62 | + db = Gino() |
| 63 | +
|
| 64 | +
|
| 65 | + class OrderItems(db.Model): |
| 66 | +
|
| 67 | + __tablename__ = 'order_items' |
| 68 | +
|
| 69 | + product_no = db.Column(db.Integer(), db.ForeignKey('products.product_no'), ondelete="RESTRICT", primary_key=True) |
| 70 | + order_id = db.Column(db.Integer(), db.ForeignKey('orders.order_id'), ondelete="CASCADE", primary_key=True) |
| 71 | + type = db.Column(db.Integer(), db.ForeignKey('types.type_id'), ondelete="RESTRICT", onupdate="CASCADE") |
| 72 | +
|
| 73 | + """ |
| 74 | + result = parse(models_str) |
| 75 | +
|
| 76 | +It will produce the result: |
| 77 | + |
| 78 | +.. code-block:: python |
| 79 | +
|
| 80 | +
|
| 81 | + [ |
| 82 | + { |
| 83 | + "attrs": [ |
| 84 | + { |
| 85 | + "default": None, |
| 86 | + "name": "product_no", |
| 87 | + "properties": { |
| 88 | + "foreign_key": "'products.product_no'", |
| 89 | + "ondelete": '"RESTRICT"', |
| 90 | + "primary_key": "True", |
| 91 | + }, |
| 92 | + "type": "db.Integer()", |
| 93 | + }, |
| 94 | + { |
| 95 | + "default": None, |
| 96 | + "name": "order_id", |
| 97 | + "properties": { |
| 98 | + "foreign_key": "'orders.order_id'", |
| 99 | + "ondelete": '"CASCADE"', |
| 100 | + "primary_key": "True", |
| 101 | + }, |
| 102 | + "type": "db.Integer()", |
| 103 | + }, |
| 104 | + { |
| 105 | + "default": None, |
| 106 | + "name": "type", |
| 107 | + "properties": { |
| 108 | + "foreign_key": "'types.type_id'", |
| 109 | + "ondelete": '"RESTRICT"', |
| 110 | + "onupdate": '"CASCADE"', |
| 111 | + }, |
| 112 | + "type": "db.Integer()", |
| 113 | + }, |
| 114 | + ], |
| 115 | + "name": "OrderItems", |
| 116 | + "parents": ["db.Model"], |
| 117 | + "properties": {"table_name": "'order_items'"}, |
| 118 | + } |
| 119 | + ] |
| 120 | +
|
| 121 | +TODO: in next Release |
| 122 | +--------------------- |
| 123 | + |
| 124 | + |
| 125 | +#. Parse from file method |
| 126 | +#. Add cli |
| 127 | +#. Add more tests for supported models (and fix existed not covered cases): Pydantic, Enums, Dataclasses, SQLAlchemy Models, GinoORM models, TortoiseORM models |
| 128 | +#. Add support for pure Python classes |
| 129 | +#. Add support for pure SQLAlchemy Core Tables |
| 130 | + |
| 131 | +Changelog |
| 132 | +--------- |
| 133 | + |
| 134 | +**v0.1.0** |
| 135 | + |
| 136 | + |
| 137 | +#. Added base parser logic & tests for Pydantic, Enums, SQLAlchemy Models, GinoORM models, TortoiseORM models |
0 commit comments