From 8d362abd7e38ff336717926ef420afc4c8b42e54 Mon Sep 17 00:00:00 2001 From: Krishna Miriyala Date: Wed, 31 May 2023 10:54:17 -0700 Subject: [PATCH] Add yml2cli basic functionality ``` $ yml2cli -i a.yml b.yml -a1 -b"1" "2" -c3 -d4 miriyalak@krishna-dev:~/yml2cli/test$ cat a.yml b.yml --- a: 1 b: - 3 - 4 c: 6 --- b: - 1 - 2 c: 3 d: 4 ``` --- .github/workflows/pylint.yml | 20 +++++++++ .github/workflows/python-publish.yml | 42 +++++++++++++++++++ Makefile | 41 ++++++++++++++++++ README.md | 2 +- requirements.txt | 3 ++ setup.py | 53 +++++++++++++++++++++++ test/a.yml | 6 +++ test/b.yml | 6 +++ yml2cli/__init__.py | 0 yml2cli/yml2cli.py | 63 ++++++++++++++++++++++++++++ 10 files changed, 235 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/pylint.yml create mode 100644 .github/workflows/python-publish.yml create mode 100644 Makefile create mode 100644 requirements.txt create mode 100755 setup.py create mode 100644 test/a.yml create mode 100644 test/b.yml create mode 100644 yml2cli/__init__.py create mode 100644 yml2cli/yml2cli.py diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml new file mode 100644 index 0000000..7975440 --- /dev/null +++ b/.github/workflows/pylint.yml @@ -0,0 +1,20 @@ +name: Linting Source Code + +on: [push] + +jobs: + lint: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11"] + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: make install_tools + - name: Analysing the code with pylint + run: make lint diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml new file mode 100644 index 0000000..1011b33 --- /dev/null +++ b/.github/workflows/python-publish.yml @@ -0,0 +1,42 @@ +# This workflow will upload a Python Package using Twine when a release is created +# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries + +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +name: Upload Python Package + +on: + release: + types: [published] + +permissions: + contents: read + +jobs: + publish: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v3 + with: + python-version: '3.x' + - name: Install dependencies + run: make install_tools + - name: Extract tag name + id: tag + run: echo ::set-output name=TAG_NAME::$(echo $GITHUB_REF | cut -d / -f 3) + - name: Update version in setup.py + run: >- + sed -i "s/{{VERSION_PLACEHOLDER}}/${{ steps.tag.outputs.TAG_NAME }}/g" setup.py + - name: Build the package + run: make build + - name: Publish distribution 📦 to PyPI + uses: pypa/gh-action-pypi-publish@master + with: + password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f2e20fc --- /dev/null +++ b/Makefile @@ -0,0 +1,41 @@ +.PHONY: all clean test reinstall + +PROJECT=yml2cli + +version: + TZ=UTC git --no-pager show --quiet --abbrev=12 --date='format-local:%Y%m%d%H%M%S' --format="%cd" > VERSION + sed -i "s/{{VERSION_PLACEHOLDER}}/v0.0.1-`cat VERSION`/g" setup.py + +build: lint + python3 -m build + +reinstall: uninstall clean install + +install_tools: + python3 -m pip install --upgrade pip + pip3 install build pycodestyle pyflakes pylint twine + pip3 install -r requirements.txt + +install: clean build + sudo pip3 install dist/*.whl + +uninstall: + sudo pip3 uninstall -y dist/*.whl + +addlicense: + docker run -it -v ${PWD}:/src ghcr.io/google/addlicense -c "Krishna Miriyala" -l mit **/*.py + +lint: + pyflakes ${PROJECT}/*.py + pycodestyle ${PROJECT}/*.py --ignore=E501 + pylint ${PROJECT}/*.py -d C0116,C0114,W0703 + yamllint -s */*.yml + +lint_fix: + autopep8 -i ${PROJECT}/*.py + +clean: + rm -rf VERSION ./dist ./*egg-info* + +publish: clean build install + twine upload dist/* diff --git a/README.md b/README.md index bb12df1..9595054 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# yaml2cli +# yml2cli Converts yaml configuration files to cli params diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2185e87 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +build==0.10.0 +PyYAML==6.0 +setuptools==66.1.1 diff --git a/setup.py b/setup.py new file mode 100755 index 0000000..b56a36b --- /dev/null +++ b/setup.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# Copyright (c) 2023 Krishna Miriyala +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +import argparse +import os +import sys + +import setuptools + +TOP_DIR = os.path.dirname(__file__) +README = os.path.join(TOP_DIR, "README.md") +REQUIREMENTS = [ + "pyyaml", +] + + +setuptools.setup( + name="yml2cli", + version="{{VERSION_PLACEHOLDER}}", + + author="Krishna Miriyala", + author_email="krishnambm@gmail.com", + url="https://github.com/krishnamiriyala/yml2cli", + description="Yaml to cli params converter", + long_description=open(README, "r").read(), + packages=setuptools.find_packages(), + license="AS IS", + entry_points={ + 'console_scripts': [ + 'yml2cli=yml2cli:yml2cli.main', + ], + }, + install_requires=REQUIREMENTS, +) diff --git a/test/a.yml b/test/a.yml new file mode 100644 index 0000000..fb22492 --- /dev/null +++ b/test/a.yml @@ -0,0 +1,6 @@ +--- +a: 1 +b: + - 3 + - 4 +c: 6 diff --git a/test/b.yml b/test/b.yml new file mode 100644 index 0000000..9248488 --- /dev/null +++ b/test/b.yml @@ -0,0 +1,6 @@ +--- +b: + - 1 + - 2 +c: 3 +d: 4 diff --git a/yml2cli/__init__.py b/yml2cli/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/yml2cli/yml2cli.py b/yml2cli/yml2cli.py new file mode 100644 index 0000000..71ad0e9 --- /dev/null +++ b/yml2cli/yml2cli.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +# Copyright (c) 2023 Krishna Miriyala +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +import argparse +import yaml + + +def parse_args(): + parser = argparse.ArgumentParser( + description='Yaml to cli parames converter', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument( + '-i', '--input-files', action='extend', nargs='+', default=[], + help='Yaml configuration files in order of overrides') + args = parser.parse_args() + return args + + +def main(): + args = parse_args() + params = {} + cli_params = "" + + for input_file in args.input_files: + with open(input_file, encoding='UTF-8') as filep: + data = yaml.safe_load(filep) + for key, val in data.items(): + params[key] = val + + for key, value in params.items(): + cli_params += " " + if len(key) == 1: + cli_params += f"-{key}" + else: + cli_params += f"--{key}" + if isinstance(value, list): + cli_params += " ".join([f"\"{val}\"" for val in value]) + else: + cli_params += f"{value}" + print(cli_params) + + +if __name__ == '__main__': + main()