Skip to content

Commit

Permalink
chore: pyproject, pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
timurbazhirov committed Apr 8, 2024
1 parent 9485f40 commit 9e27f12
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
12 changes: 7 additions & 5 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ then
echo "Re-building JS and PY assets using JS script."
npm run transpile-and-build-assets
fi
pwd

if -d .venv/bin/activate
if test -f .venv/bin/activate
then
echo "Python virtual environment not found. Running build scripts."
echo "Python virtual environment found. Running build scripts"
echo "Re-building JS and PY assets using JS script."
source .venv/bin/activate
# Note: resolved (no include statements) schemas have to exist by this time
datamodel-codegen --input ./dist/js/schema/ --input-file-type jsonschema --output ./dist/py --class-name ESSE --output-model-type pydantic_v2.BaseModel --use-field-description --use-double-quotes --enable-version-header --disable-timestamp --use-title-as-nam
pre-commit run --all-files
#git add dist/py
else
echo "Python virtual environment found. Create one in '.venv'."
echo "ERROR: Python virtual environment found. Create one in '.venv' and install '.[all]'"
exit 1
fi

npx lint-staged --allow-empty

npm run lint:fix
npm run transpile
git add dist
#git add dist/js

24 changes: 17 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@ repos:
rev: 2023.6.13
hooks:
- id: ruff
exclude: ^src/py/mat3ra/esse/data
exclude: ^src/py/mat3ra/esse/data|^dist*
- id: black
exclude: ^src/py/mat3ra/esse/data
exclude: ^src/py/mat3ra/esse/data|^dist*
- repo: local
hooks:
# Note: only python 3.8.12 is tested for the model generation
- id: generate-python-modules
name: Regenerate data modules classes from static assets
# yamllint disable rule:line-length
entry: |
bash -c 'datamodel-codegen --input ./dist/js/schema/ --input-file-type jsonschema --output ./dist/py --class-name ESSE --output-model-type pydantic_v2.BaseModel --use-field-description --use-double-quotes --enable-version-header --disable-timestamp'
# Move 3rd party modules to a separate directory with a PEP8 compliant name
mkdir -p ./dist/py/third_party
mv ./dist/py/3pse/* ./dist/py/third_party/
rmdir ./dist/py/3pse
bash -c 'if ! [ "$(git diff --cached --name-only | grep "schema/")" ]; then echo "Skipping model generation because schemas were not changed."; exit 0; fi && \
datamodel-codegen \
--input ./dist/js/schema/ \
--input-file-type jsonschema \
--output ./dist/py \
--output-model-type pydantic_v2.BaseModel \
--use-field-description \
--use-double-quotes \
--enable-version-header \
--use-title-as-name \
--class-name ESSE \
--disable-timestamp && \
# Move 3rd party modules to a separate directory with a PEP8 compliant name
rm -rf ./dist/py/third_party && \
mv ./dist/py/3pse ./dist/py/third_party'
language: system
pass_filenames: false
verbose: true
1 change: 0 additions & 1 deletion build_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,3 @@
Path(full_path).parent.mkdir(parents=True, exist_ok=True)
with open(full_path, "w") as f:
f.write(json.dumps(example, sort_keys=True, indent=4, separators=(",", ": ")))

19 changes: 17 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ classifiers = [
dependencies = [
"exabyte-json-include>=2023.12.23.post0",
"jsonschema>=2.6.0",
"datamodel-code-generator==0.21.5",
"datamodel-code-generator>=0.25.5",
# "python-slugify==2.0.1",
# PyYAML install could be problematic, see https://github.com/yaml/pyyaml/issues/601
# To avoid "AttributeError: cython_sources", consider "cython<3.0.0", the below might be needed:
Expand Down Expand Up @@ -59,7 +59,22 @@ git_describe_command = "git describe --tags --long"
# local_scheme = "no-local-version"

[tool.setuptools.packages.find]
where = ["src/py"]
where = [
"src/py",
]

[tool.setuptools.package-data]
"*" = [
# NOTE: may be needed to allow for the `models` link in the `src/py` directory
# "dist/py/**"
]

[tool.setuptools.exclude-package-data]
"*" = [
# Since models inside `src/py` is a link, exclusion is needed
# to avoid `doesn't exist or not a regular file` error
"models"
]

[tool.black]
line-length = 120
Expand Down

0 comments on commit 9e27f12

Please sign in to comment.