Skip to content

Commit

Permalink
Add Makefile for python code generation from jsonschema
Browse files Browse the repository at this point in the history
  • Loading branch information
elchupanebrej committed Sep 4, 2024
1 parent 13cd1ce commit effdd2b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
32 changes: 32 additions & 0 deletions python/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
schemas = $(shell find ../jsonschema -name "*.json")

.DEFAULT_GOAL = help

MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
HERE := $(dir $(MKFILE_PATH))

help: ## Show this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make <target>\n\nWhere <target> is one of:\n"} /^[$$()% a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

generate:
datamodel-codegen \
--output-model-type "pydantic_v2.BaseModel" \
--input $(HERE)../jsonschema/Envelope.json \
--output $(HERE)src/_messages.py \
--use-generic-container-types \
--allow-population-by-field-name \
--snake-case-field \
--input-file-type=jsonschema \
--class-name Envelope \
--use-double-quotes \
--use-union-operator

require: ## Check requirements for the code generation (python is required)
@python --version >/dev/null 2>&1 || (echo "ERROR: python is required."; exit 1)

clean: ## Remove automatically generated files and related artifacts
rm -rf $(HERE)src/_messages.py

install-deps: ## Install generation dependencies
python -m ensurepip --upgrade
pip install $(HERE)[generation]
4 changes: 4 additions & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ test = [
"tox"
]

generation = [
"datamodel-code-generator"
]

[project.scripts]

[tool.black]
Expand Down
4 changes: 2 additions & 2 deletions python/src/_messages.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# generated by datamodel-codegen:
# filename: Envelope.json
# timestamp: 2024-01-13T14:45:09+00:00
# timestamp: 2024-09-04T20:46:37+00:00

from __future__ import annotations

from enum import Enum
from typing import Optional, Sequence
from typing import Sequence

from pydantic import BaseModel, ConfigDict, Field

Expand Down

0 comments on commit effdd2b

Please sign in to comment.