Skip to content

Commit

Permalink
Merge pull request #30 from anevis/chore/format-and-dep-update
Browse files Browse the repository at this point in the history
Format and dependency update
  • Loading branch information
anevis authored Jul 31, 2024
2 parents de682e5 + 2ad5d8a commit bc1abc9
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 151 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: Installing Devbox ⚙️
uses: jetpack-io/devbox-install-action@v0.8.0
uses: jetify-com/devbox-install-action@v0.11.0
- name: Install all dependencies 📦
run: devbox run install
- name: 🧹 Linting & Formatting
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: Installing Devbox ⚙️
uses: jetpack-io/devbox-install-action@v0.8.0
uses: jetify-com/devbox-install-action@v0.11.0
- name: Install all dependencies 📦
run: devbox run install
- name: Release Version 🏷
Expand Down
35 changes: 10 additions & 25 deletions devbox.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/jetpack-io/devbox/0.10.3/.schema/devbox.schema.json",
"packages": ["[email protected].2"],
"packages": ["[email protected]"],
"env": {
"VENV_DIR": "$HOME/MyFiles/programming/OpenSource/yaml-to-markdown/.devbox/virtenv/python/.venv",
"TWINE_USERNAME": "__token__"
Expand All @@ -10,30 +10,15 @@
". $VENV_DIR/bin/activate"
],
"scripts": {
"install": [
"pip install -r requirements.txt"
],
"test": [
"pytest src/"
],
"test-cov": [
"pytest --cov=src/ --cov-report=xml"
],
"lint": [
"flake8 src/"
],
"format-check": [
"black --check src/"
],
"format": [
"black src/"
],
"build": [
"rm -rf dist/* && python setup.py sdist bdist_wheel"
],
"publish": [
"twine upload dist/*"
]
"audit": "safety check --short-report -r requirements.txt",
"build": "rm -rf dist/* && python setup.py sdist bdist_wheel",
"format": "ruff check --fix src/ && ruff format src/",
"install": "pip install -r requirements.txt",
"lint": "ruff check src/",
"publish": "twine upload dist/*",
"test": "pytest src/",
"test-cov": "pytest --cov=src/ --cov-report=xml",
"type-check": "mypy --config-file=src/pyproject.toml src/"
}
}
}
28 changes: 14 additions & 14 deletions devbox.lock
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
{
"lockfile_version": "1",
"packages": {
"[email protected].2": {
"last_modified": "2024-03-22T11:26:23Z",
"[email protected]": {
"last_modified": "2024-07-07T07:43:47Z",
"plugin_version": "0.0.3",
"resolved": "github:NixOS/nixpkgs/a3ed7406349a9335cb4c2a71369b697cecd9d351#python312",
"resolved": "github:NixOS/nixpkgs/b60793b86201040d9dee019a05089a9150d08b5b#python3",
"source": "devbox-search",
"version": "3.12.2",
"version": "3.12.4",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/jc5jlynlx561ibqxd6sy12hcqc8p39c9-python3-3.12.2",
"path": "/nix/store/3swy1vadi125g0c1vxqp8ykdr749803j-python3-3.12.4",
"default": true
}
],
"store_path": "/nix/store/jc5jlynlx561ibqxd6sy12hcqc8p39c9-python3-3.12.2"
"store_path": "/nix/store/3swy1vadi125g0c1vxqp8ykdr749803j-python3-3.12.4"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/n5yvl08kxz5llrdiwwxfxyy6wiq2g6lc-python3-3.12.2",
"path": "/nix/store/sz2facg15yq3ziqkidb1dkkglwzkkg8a-python3-3.12.4",
"default": true
},
{
"name": "debug",
"path": "/nix/store/bihg62nz0vqqski18cpyppwgqz62blrq-python3-3.12.2-debug"
"path": "/nix/store/19vjjqg7jbfblqapf63nm9ich1xdq9dx-python3-3.12.4-debug"
}
],
"store_path": "/nix/store/n5yvl08kxz5llrdiwwxfxyy6wiq2g6lc-python3-3.12.2"
"store_path": "/nix/store/sz2facg15yq3ziqkidb1dkkglwzkkg8a-python3-3.12.4"
},
"x86_64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/41yqb3sxsx22drhza74icn4x1gfh3h8m-python3-3.12.2",
"path": "/nix/store/3y5wy1i9nq5293knm23mxsj5l6w41h2l-python3-3.12.4",
"default": true
}
],
"store_path": "/nix/store/41yqb3sxsx22drhza74icn4x1gfh3h8m-python3-3.12.2"
"store_path": "/nix/store/3y5wy1i9nq5293knm23mxsj5l6w41h2l-python3-3.12.4"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/7yh2ax34jd7fgf17mjfd3c6niw1h2hsj-python3-3.12.2",
"path": "/nix/store/z7xxy35k7620hs6fn6la5fg2lgklv72l-python3-3.12.4",
"default": true
},
{
"name": "debug",
"path": "/nix/store/mq8jh0sl1lcpk592whzw96n52grhq8wl-python3-3.12.2-debug"
"path": "/nix/store/3x6jqv5yw212v8rlwql88cn94dginq32-python3-3.12.4-debug"
}
],
"store_path": "/nix/store/7yh2ax34jd7fgf17mjfd3c6niw1h2hsj-python3-3.12.2"
"store_path": "/nix/store/z7xxy35k7620hs6fn6la5fg2lgklv72l-python3-3.12.4"
}
}
}
Expand Down
30 changes: 11 additions & 19 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
click==8.1.7
jsonschema[format]==4.21.1
jsonschema[format]==4.23.0
pyyaml==6.0.1

# Dev dependencies
coverage==7.4.4
coverage==7.6.0
mock==5.1.0
pytest==8.1.1
pytest==8.3.2
pytest-cov==5.0.0

bandit==1.7.8
black==24.4.0
flake8==7.0.0
flake8-bandit==4.1.1
flake8-black==0.3.6
flake8-bugbear==24.4.21
flake8-functions==0.0.8
isort==5.13.2
mypy==1.9.0
pep8-naming==0.13.3
safety
mypy==1.11.1
ruff==0.5.5
safety==3.2.4

# Packaging
setuptools==69.5.1
twine==5.0.0
setuptools==72.1.0
twine==5.1.1
wheel==0.43.0

# Typing
types-mock==5.1.0.20240311
types-mock==5.1.0.20240425
types-orjson==3.6.2
types-PyYAML==6.0.12.20240311
types-jsonschema==4.21.0.20240331
types-PyYAML==6.0.12.20240724
types-jsonschema==4.23.0.20240712
13 changes: 13 additions & 0 deletions scripts/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
#
# This script is used to verify python code by linting, type checking and checking dependencies.
set -euo pipefail

echo "Linting python code ..."
devbox run lint

echo "Type checking python code ..."
devbox run type-check

echo "Checking dependencies of python code ..."
devbox run audit
1 change: 1 addition & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ sonar.projectName=yaml-to-markdown
sonar.sources=src/
sonar.coverage.exclusions=./.pytest_cache/**,**_test.py,**.xml,**.yaml,**.yml
sonar.python.coverage.reportPaths=coverage.xml
sonar.python.version=3.12
76 changes: 76 additions & 0 deletions src/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
[project]
name = "yaml-to-markdown"
requires-python = ">=3.12"

[tool.coverage.run]
branch = true
source = ["."]
omit = [
# omit anything in a .local directory anywhere
"*/.local/*",
# Omit any test files
"*/*_test.py",
"setup.py",
]

# [tool.coverage.xml]
# output = "./coverage.xml"

[tool.mypy]
python_version="3.12"
platform="linux"

show_column_numbers=true
show_error_context=false
error_summary=true

# treat Optional per PEP 484
strict_optional=true
strict_equality=true

# https://mypy.readthedocs.io/en/stable/config_file.html#configuring-warnings
warn_redundant_casts=true
warn_unused_ignores=true
warn_unreachable=true
warn_no_return=true
warn_return_any=true

# Be real strict on using types
check_untyped_defs=true
disallow_incomplete_defs=true
disallow_untyped_calls=true
disallow_untyped_decorators=false
disallow_untyped_defs=true

[[tool.mypy.overrides]]
# Support for types is not yet available
ignore_missing_imports = true

[tool.pytest.ini_options]
markers = [
"integration: marks tests as integration (these are not run by default)."
]

[tool.ruff]
line-length = 90
preview = true

[tool.ruff.lint]
select = [
"F", "E", "W", "C90", "I", "N", "D2", "UP", "YTT", "ANN", "ASYNC", "S",
"B", "A", "C4", "DTZ", "T10", "FA", "ISC", "ICN",
"LOG", "G", "INP", "PIE", "PT", "Q", "RSE", "RET", "SLF", "SLOT", "SIM",
"TID", "TCH", "ARG", "PTH", "TD", "FIX", "ERA", "PL", "TRY", "FLY",
"PERF", "FURB", "RUF"
]

ignore = ["E203", "E501", "S101", "S311", "D203", "D211", "D213", "ISC001", "ANN401", "TRY003", "TCH003"]

[tool.ruff.lint.mccabe]
max-complexity = 10

[tool.ruff.lint.pylint]
max-args = 6

[tool.ruff.lint.per-file-ignores]
"*_test.py" = ["S105", "FBT", "PLR2004", "PLR6301", "PLC2801"]
37 changes: 24 additions & 13 deletions src/yaml_to_markdown/convert.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import io
import json
from typing import Dict, Any, Optional
import sys
from pathlib import Path
from typing import Any

import click
import yaml

from yaml_to_markdown.md_converter import MDConverter


def _get_json_data(json_file: str) -> Dict[str, Any]:
with io.open(json_file, "r", encoding="utf-8") as j_file:
def _get_json_data(json_file: str) -> dict[str, Any]:
with Path(json_file).open("r", encoding="utf-8") as j_file:
return json.load(j_file)


def _get_yaml_data(yaml_file: str) -> Dict[str, Any]:
with io.open(yaml_file, "r", encoding="utf-8") as y_file:
def _get_yaml_data(yaml_file: str) -> dict[str, Any]:
with Path(yaml_file).open("r", encoding="utf-8") as y_file:
return yaml.safe_load(y_file)


Expand Down Expand Up @@ -46,8 +47,8 @@ def _help() -> None:
@click.option("-h", "--help", "show_help", default=False, is_flag=True)
def main(
output_file: str,
yaml_file: Optional[str],
json_file: Optional[str],
yaml_file: str | None,
json_file: str | None,
show_help: bool,
) -> None:
if show_help:
Expand All @@ -59,20 +60,30 @@ def main(


def _verify_inputs(
output_file: str, yaml_file: Optional[str], json_file: Optional[str]
output_file: str, yaml_file: str | None, json_file: str | None
) -> None:
if (yaml_file is None and json_file is None) or output_file is None:
_help()
exit(1)
sys.exit(1)


def _get_data(yaml_file: str | None, json_file: str | None) -> dict[str, Any]:
if json_file:
return _get_json_data(json_file)
if yaml_file:
return _get_yaml_data(yaml_file)

_help()
sys.exit(1)


def convert(
output_file: str, yaml_file: Optional[str] = None, json_file: Optional[str] = None
output_file: str, yaml_file: str | None = None, json_file: str | None = None
) -> None:
_verify_inputs(output_file=output_file, yaml_file=yaml_file, json_file=json_file)

data = _get_json_data(json_file) if json_file else _get_yaml_data(yaml_file)
with io.open(output_file, "w", encoding="utf-8") as md_file:
data = _get_data(yaml_file=yaml_file, json_file=json_file)
with Path(output_file).open("w", encoding="utf-8") as md_file:
MDConverter().convert(data=data, output_writer=md_file)


Expand Down
15 changes: 8 additions & 7 deletions src/yaml_to_markdown/convert_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from io import StringIO
from unittest.mock import mock_open, patch, Mock
from pathlib import Path
from unittest.mock import Mock, mock_open, patch

import pytest

Expand All @@ -15,7 +16,7 @@ def test_convert_with_no_file() -> None:
convert(output_file="some.md")


@patch("io.open", new_callable=mock_open(read_data=_JSON_DATA))
@patch.object(Path, "open", new_callable=mock_open(read_data=_JSON_DATA))
def test_convert_with_json_data(mock_open_file: Mock) -> None:
# Prepare
mock_open_file.return_value.__enter__.return_value = StringIO(_JSON_DATA)
Expand All @@ -24,11 +25,11 @@ def test_convert_with_json_data(mock_open_file: Mock) -> None:
convert(output_file=_OUTPUT_FILE_NAME, json_file="test.json")

# Assert
mock_open_file.assert_any_call("test.json", "r", encoding="utf-8")
mock_open_file.assert_any_call(_OUTPUT_FILE_NAME, "w", encoding="utf-8")
mock_open_file.assert_any_call("r", encoding="utf-8")
mock_open_file.assert_any_call("w", encoding="utf-8")


@patch("io.open", new_callable=mock_open())
@patch.object(Path, "open", new_callable=mock_open())
def test_convert_with_yaml_data(mock_open_file: Mock) -> None:
# Prepare
data = "key: value"
Expand All @@ -38,5 +39,5 @@ def test_convert_with_yaml_data(mock_open_file: Mock) -> None:
convert(output_file=_OUTPUT_FILE_NAME, yaml_file="test.yaml")

# Assert
mock_open_file.assert_any_call("test.yaml", "r", encoding="utf-8")
mock_open_file.assert_any_call(_OUTPUT_FILE_NAME, "w", encoding="utf-8")
mock_open_file.assert_any_call("r", encoding="utf-8")
mock_open_file.assert_any_call("w", encoding="utf-8")
Loading

0 comments on commit bc1abc9

Please sign in to comment.