From 33e8bff64166553abd930fd11bb1f41e80869c45 Mon Sep 17 00:00:00 2001 From: Antony Nevis Date: Sat, 6 Apr 2024 15:23:13 +1100 Subject: [PATCH] Added publishing to PyPI --- .github/workflows/cd.yaml | 30 +++++++++++++ .github/workflows/{ci-cd.yaml => ci.yaml} | 6 ++- devbox.json | 6 +++ requirements-dev.txt | 5 +++ setup.py | 31 +++++++++++++ src/json_to_markdown/convert.py | 55 ++++++++++++++++------- 6 files changed, 114 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/cd.yaml rename .github/workflows/{ci-cd.yaml => ci.yaml} (90%) create mode 100644 setup.py diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml new file mode 100644 index 0000000..8933bc2 --- /dev/null +++ b/.github/workflows/cd.yaml @@ -0,0 +1,30 @@ +name: CD Pipeline +run-name: CD ๐Ÿ“ฆ๐Ÿš€ + +on: + workflow_run: + workflows: ['CI'] + types: [completed] + branches: + - 'main' + +jobs: + CD: + runs-on: ubuntu-latest + steps: + - run: echo "๐ŸŽ‰ The job was automatically triggered by a ${{ github.event_name }} event." + - run: echo "๐Ÿง This job is now running on a ${{ runner.os }} server hosted by GitHub!" + - run: echo "๐Ÿ”Ž The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." + - name: Check out repository code + uses: actions/checkout@v4 + - 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 + - name: Install all dependencies ๐Ÿ“ฆ + run: devbox run install-dev + - name: ๐Ÿ“ฆ Package + run: devbox run build + - name: ๐Ÿš€ Publish to PyPI + run: devbox run publish + - run: echo "๐Ÿ This job's status is ${{ job.status }}." diff --git a/.github/workflows/ci-cd.yaml b/.github/workflows/ci.yaml similarity index 90% rename from .github/workflows/ci-cd.yaml rename to .github/workflows/ci.yaml index a58dff3..0cc330f 100644 --- a/.github/workflows/ci-cd.yaml +++ b/.github/workflows/ci.yaml @@ -1,5 +1,5 @@ -name: CI/CD Pipeline -run-name: CI/CD ๐Ÿš€ +name: CI Pipeline +run-name: CI ๐Ÿš€ on: [push] jobs: CI: @@ -18,4 +18,6 @@ jobs: run: devbox run install-dev - name: ๐Ÿงน Linting & Formatting run: devbox run lint && devbox run format + - name: ๐Ÿงช Running Tests + run: devbox run test - run: echo "๐Ÿ This job's status is ${{ job.status }}." diff --git a/devbox.json b/devbox.json index 9d536eb..e14dc68 100644 --- a/devbox.json +++ b/devbox.json @@ -26,6 +26,12 @@ ], "format": [ "black src/" + ], + "build": [ + "python setup.py sdist bdist_wheel" + ], + "publish": [ + "twine upload dist/*" ] } } diff --git a/requirements-dev.txt b/requirements-dev.txt index 021f871..dcc8e95 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -16,6 +16,11 @@ mypy==1.9.0 pep8-naming==0.13.3 safety +# Packaging +setuptools==69.2.0 +twine==5.0.0 +wheel==0.43.0 + # Typing types-mock==5.1.0.20240311 types-orjson==3.6.2 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..d198078 --- /dev/null +++ b/setup.py @@ -0,0 +1,31 @@ +from setuptools import setup, find_packages +import calendar +import time + +gmt = time.gmtime() +ts = calendar.timegm(gmt) + +with open("README.md", "r") as fh: + long_description = fh.read() + +setup( + name="json-to-markdown", + version=f"0.1.{ts}", + packages=find_packages(where="src"), + package_dir={"": "src"}, + url="https://anevis.github.io/json-to-markdown/", + license="MIT", + author="anevis", + install_requires=[ + "click==8.1.7", + "jsonschema[format]==4.21.1", + "pyyaml==6.0.1", + ], + entry_points={ + "console_scripts": [ + "json-to-markdown=json_to_markdown.convert:main", + ], + }, + long_description=long_description, + long_description_content_type="text/markdown", +) diff --git a/src/json_to_markdown/convert.py b/src/json_to_markdown/convert.py index 4583087..c7c684d 100644 --- a/src/json_to_markdown/convert.py +++ b/src/json_to_markdown/convert.py @@ -18,36 +18,57 @@ def _get_yaml_data(yaml_file: str) -> Dict[str, Any]: return yaml.safe_load(y_file) +def _help() -> None: + click.echo("Convert JSON or YAML to Markdown.") + click.echo( + "Usage: json-to-markdown -o [-y | -j ]" + ) + click.echo( + " -o, --output-file : Path to the output file as a string [Mandatory]." + ) + click.echo( + " -y, --yaml-file : Path to the YAML file as a string [Optional]" + ) + click.echo( + " -j, --json-file : Path to the JSON file as a string [Optional]" + ) + click.echo(" -h, --help: Show this message and exit.") + click.echo( + "Note: Either yaml_file or json_file is required along with output_file." + ) + click.echo("Example: json-to-markdown -o output.md -y data.yaml") + + @click.command() @click.option("-o", "--output-file", "output_file", type=str) @click.option("-y", "--yaml-file", "yaml_file", type=str, default=None) @click.option("-j", "--json-file", "json_file", type=str, default=None) -def main(output_file: str, yaml_file: Optional[str], json_file: Optional[str]) -> 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], + show_help: bool, +) -> None: + if show_help or (yaml_file is None and json_file is None) or output_file is None: + _help() + return + convert(output_file=output_file, yaml_file=yaml_file, json_file=json_file) def convert( output_file: str, yaml_file: Optional[str] = None, json_file: Optional[str] = None ) -> None: - if yaml_file is None and json_file is None: - raise RuntimeError("One of yaml_file or json_file is required.") + if yaml_file is None and json_file is None or output_file is None: + _help() + raise RuntimeError( + "One of yaml_file or json_file is required along with output_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: - converter = MDConverter() - converter.set_selected_sections( - sections=[ - "assessment-date", - "assessors", - "description", - "diagram", - "external-dependencies", - "roles", - "entry-points", - "threats", - ] - ) - converter.convert(data=data, output_writer=md_file) + MDConverter().convert(data=data, output_writer=md_file) if __name__ == "__main__":