Skip to content

Commit dd02598

Browse files
committed
added cli with command dump
1 parent 03f78cc commit dd02598

File tree

6 files changed

+126
-14
lines changed

6 files changed

+126
-14
lines changed

CHANGELOG.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
**v0.3.0**
2+
1. Added cli - `pmp` command with args -d, --dump
3+
2. Add support for pure Python classes
4+
15
**v0.2.0**
26
1. Added support for Dataclasses
37
2. Added parse_from_file method

README.md

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44

55

66
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.
7-
Py-Models-Parser supports now ORM Sqlalchemy, Gino, Tortoise; Pydantic, Python Enum models, Dataclasses & in nearest feature I plan to add pure pyton classes. And next will be added other ORMs models.
7+
8+
Py-Models-Parser can parse & extract information from models:
9+
10+
- Sqlalchemy ORM,
11+
- Gino ORM,
12+
- Tortoise ORM,
13+
- Pydantic,
14+
- Python Enum,
15+
- Python Dataclasses
16+
17+
Number of supported models will be increased, check 'TODO' section.
818

919
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.
1020

@@ -51,7 +61,7 @@ NOTE: it's is a text parser, so it don't import or load your code, parser work w
5161

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

54-
You can parse models from python string:
64+
1. You can parse models from python string:
5565

5666
```python
5767

@@ -75,7 +85,7 @@ result = parse(models_str)
7585

7686
```
7787

78-
or just provide the path to file:
88+
2. Parse models from file:
7989

8090
```python
8191

@@ -87,7 +97,31 @@ or just provide the path to file:
8797
result = parse_from_file(file_path)
8898
```
8999

90-
It will produce the result:
100+
3. Parse models from file with command line
101+
102+
```bash
103+
104+
pmp path_to_models.py
105+
106+
# for example: pmp tests/data/dataclass_defaults.py
107+
108+
```
109+
110+
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'
111+
112+
```bash
113+
114+
# example how to dump output from cli
115+
116+
pmp path_to_models.py -d target_file.json
117+
118+
```
119+
120+
### Output example
121+
122+
You can find a lot of output examples in tests - https://github.com/xnuinside/py-models-parser/tree/main/tests
123+
124+
For model from point 1 (above) library will produce the result:
91125

92126
```python
93127

@@ -132,20 +166,17 @@ It will produce the result:
132166
]
133167
```
134168

135-
136169
## TODO: in next Release
137170

138-
1. Parse from file method
139-
2. Add cli
140-
3. Add more tests for supported models (and fix existed not covered cases): Pydantic, Enums, Dataclasses, SQLAlchemy Models, GinoORM models, TortoiseORM models
141-
4. Add support for pure Python classes
142-
5. Add support for pure SQLAlchemy Core Tables
171+
1. Add more tests for supported models (and fix existed not covered cases): Pydantic, Enums, Dataclasses, SQLAlchemy Models, GinoORM models, TortoiseORM models
172+
2. Add support for pure SQLAlchemy Core Tables
173+
3. Add support for Pony ORM models
143174

144175
## Changelog
145176
**v0.2.0**
146177
1. Added support for Dataclasses
147178
2. Added parse_from_file method
148-
3. Added correct work with types with comma inside, like: Union[dict, list] or Union[dict, list, tuple, anything]
179+
3. Added correct work with types with comma inside, like: Union[dict, list] or Union[dict, list, tuple, anything]
149180

150181
**v0.1.1**
151182
1. Added base parser logic & tests for Pydantic, Enums, SQLAlchemy Models, GinoORM models, TortoiseORM models

py_models_parser/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from py_models_parser.core import parse, parse_from_file
1+
from py_models_parser.core import dump_result, parse, parse_from_file
22

3-
__all__ = ["parse", "parse_from_file"]
3+
__all__ = ["parse", "parse_from_file", "dump_result"]

py_models_parser/cli.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import argparse
2+
import os
3+
import pprint
4+
import sys
5+
6+
from py_models_parser import dump_result, parse_from_file
7+
8+
9+
def version(**kwargs):
10+
return "0.2.0"
11+
12+
13+
def cli():
14+
pmp_cli = argparse.ArgumentParser(description="Python Models Parser")
15+
16+
pmp_cli.add_argument(
17+
"file_path", type=str, help="The path to .py file with models to parse"
18+
)
19+
20+
pmp_cli.add_argument(
21+
"-d",
22+
"--dump",
23+
nargs="?",
24+
type=str,
25+
const="output_models.json",
26+
help="Target path to save parse results in .json files",
27+
)
28+
29+
return pmp_cli
30+
31+
32+
def run_for_file(args):
33+
print(f"Start parsing file {args.file_path} \n")
34+
result = parse_from_file(args.file_path)
35+
if args.dump:
36+
dump_result(result, args.dump)
37+
print(f"File with result was saved to >> {args.dump} file")
38+
if not args.dump:
39+
pprint.pprint(result)
40+
41+
42+
def correct_extension(file_name: str) -> bool:
43+
ext = ["ddl", "sql", "hql", ""]
44+
split_name = file_name.split(".")
45+
if len(split_name) >= 2:
46+
ext_file = split_name[1]
47+
if ext_file in ext:
48+
return True
49+
return False
50+
51+
52+
def main():
53+
sdb_cli = cli()
54+
args = sdb_cli.parse_args()
55+
if not os.path.exists(args.file_path):
56+
print("The file path specified does not exist")
57+
sys.exit()
58+
if os.path.isfile(args.file_path):
59+
run_for_file(args)
60+
else:
61+
files = [
62+
os.path.join(args.ddl_file_path, file_name)
63+
for file_name in os.listdir(args.ddl_file_path)
64+
if correct_extension(file_name)
65+
]
66+
for file_path in files:
67+
args.ddl_file_path = file_path
68+
run_for_file(args)

py_models_parser/core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
from typing import Dict, List
34

@@ -76,3 +77,8 @@ def parse_from_file(file_path: str) -> List[Dict]:
7677
with open(file_path, "r") as f:
7778
models = f.read()
7879
return parse(models)
80+
81+
82+
def dump_result(output: str, file_path: str) -> None:
83+
with open(file_path, "w+") as f:
84+
json.dump(output, f, indent=1)

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "py-models-parser"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
description = "Parser for Different Python Models (Pydantic, Enums, ORMs: Tortoise, SqlAlchemy, GinoORM) to extract information about columns(attrs), model, table args,etc in one format."
55
authors = ["Iuliia Volkova <xnuinside@gmail.com>"]
66
license = "MIT"
@@ -26,6 +26,9 @@ pytest = "^5.2"
2626
twine = "^3.4.1"
2727
m2r = "^0.2.1"
2828

29+
[tool.poetry.scripts]
30+
pmp = 'py_models_parser.cli:main'
31+
2932
[build-system]
3033
requires = ["poetry-core>=1.0.0"]
3134
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)