Skip to content

Commit 49a377f

Browse files
authored
Update CLI with output options arguments (#5)
1 parent b455b02 commit 49a377f

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,30 @@ and generate a JSON with column types that can be used with `terraform` to creat
3434

3535
Take the following pydantic class
3636

37-
```python
37+
```python title="example.py"
3838
from pydantic import BaseModel
3939
from typing import List
4040

41+
4142
class Bar(BaseModel):
4243
name: str
4344
age: int
4445

46+
4547
class Foo(BaseModel):
4648
nums: List[int]
4749
bars: List[Bar]
4850
other: str
51+
4952
```
5053

5154
Running `pydantic-glue`
5255

5356
```bash
54-
pydantic-glue example.py Foo
57+
pydantic-glue -f example.py -c Foo
5558
```
5659

57-
you get this JSON
60+
you get this JSON in the terminal:
5861

5962
```json
6063
{
@@ -91,6 +94,12 @@ resource "aws_glue_catalog_table" "table" {
9194
}
9295
```
9396

97+
Alternatively you can run CLI with `-o` flag to set output file location:
98+
99+
```bash
100+
pydantic-glue -f example.py -c Foo -o example.json -l
101+
```
102+
94103
## How it works?
95104

96105
* `pydantic` gets converted to JSON Schema

pydantic_glue/cli.py

+31-12
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
import json
2+
import logging
23
import os
34
import sys
4-
from datetime import datetime
5+
from argparse import ArgumentParser
6+
from datetime import datetime, timezone
7+
from pathlib import Path
58

69
from pydantic_glue import convert
710

11+
logging.basicConfig(format="{asctime} - {levelname} - {message}", level=logging.INFO, style="{", stream=sys.stdout)
12+
log = logging.getLogger(__name__)
13+
814

915
def cli() -> None:
10-
if len(sys.argv) != 3:
11-
print(f"Usage: {sys.argv[0]} <module file> <class name>", file=sys.stderr)
12-
sys.exit(1)
16+
parser = ArgumentParser()
17+
parser.add_argument("-f", dest="source_file", required=True, type=str, help="Path to the python file")
18+
parser.add_argument("-c", dest="class_name", required=True, type=str, help="Python class name")
19+
parser.add_argument("-o", dest="output_file", required=False, type=str, help="Path to the output file")
20+
parser.add_argument(
21+
"-l", dest="log_result", action="store_true", default=False, help="Flag if need to print result in log"
22+
)
23+
args = parser.parse_args(sys.argv[1:])
1324

14-
module_file = sys.argv[1].replace(".py", "")
15-
class_name = sys.argv[2]
25+
module_file = args.source_file.removesuffix(".py")
1626

1727
sys.path.append(os.path.dirname(module_file))
1828

1929
imported = __import__(os.path.basename(module_file))
20-
imported = getattr(imported, class_name)
30+
imported = getattr(imported, args.class_name)
2131
input_schema = json.dumps(imported.model_json_schema())
2232
converted = convert(input_schema)
23-
output = {
24-
"//": f"Generated by pydantic-glue at {datetime.now()}. DO NOT EDIT",
25-
"columns": {k: v for (k, v) in converted},
26-
}
33+
output = json.dumps(
34+
{
35+
"//": f"Generated by pydantic-glue at {datetime.now(tz=timezone.utc)}. DO NOT EDIT",
36+
"columns": {k: v for (k, v) in converted},
37+
},
38+
indent=2,
39+
)
40+
41+
if args.log_result or not args.output_file:
42+
log.info(f"Generated file content:\n{output}")
2743

28-
print(json.dumps(output, indent=2))
44+
if args.output_file:
45+
ouf = Path(args.output_file)
46+
ouf.parent.mkdir(parents=True, exist_ok=True)
47+
ouf.write_text(output)

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tool.poetry]
22
name = "pydantic-glue"
33
keywords = ["pydantic", "glue", "athena", "types", "convert"]
4-
version = "0.3.0"
4+
version = "0.4.0"
55
description = "Convert pydantic model to aws glue schema for terraform"
66
authors = ["Serhii Dimchenko <[email protected]>"]
77
readme = "README.md"

0 commit comments

Comments
 (0)