Skip to content

Commit

Permalink
Add yml2cli basic functionality
Browse files Browse the repository at this point in the history
```
$ 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
```
  • Loading branch information
krishnamiriyala committed May 31, 2023
1 parent 3dc68c1 commit 7eff924
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 1 deletion.
20 changes: 20 additions & 0 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -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 }}
41 changes: 41 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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.0-alpha-`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<krishnambm@gmail.com>" -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/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# yaml2cli
# yml2cli
Converts yaml configuration files to cli params
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build==0.10.0
PyYAML==6.0
setuptools==66.1.1
53 changes: 53 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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,
)
6 changes: 6 additions & 0 deletions test/a.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
a: 1
b:
- 3
- 4
c: 6
6 changes: 6 additions & 0 deletions test/b.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
b:
- 1
- 2
c: 3
d: 4
Empty file added yml2cli/__init__.py
Empty file.
63 changes: 63 additions & 0 deletions yml2cli/yml2cli.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 7eff924

Please sign in to comment.