Markus Bergholz 2 years ago
parent
commit
430cb5141c
  1. 5
      Makefile
  2. 31
      README.md
  3. 32
      bin/ycat
  4. 19
      setup.py
  5. 43
      ycat/__init__.py

5
Makefile

@ -0,0 +1,5 @@
dist:
python setup.py sdist
publish:
twine upload dist/*

31
README.md

@ -1,2 +1,31 @@
# ycat
# Install
`pip install ycat`
# example
```
~$ cat /tmp/test2.yml
---
mappings:
templates:
- fields:
mapping:
norms: false
type: text
fields:
keyword:
ignore_above: 256
type: keyword
~$ ycat
USAGE:
cat some.yaml | ycat
ycat some.yaml
~$ ycat /tmp/test2.yml
.mappings.templates[0].fields.mapping.norms = False
.mappings.templates[0].fields.mapping.type = "text"
.mappings.templates[0].fields.mapping.fields.keyword.ignore_above = 256
.mappings.templates[0].fields.mapping.fields.keyword.type = "keyword"
```

32
bin/ycat

@ -0,0 +1,32 @@
#!/usr/bin/env python
import ycat
import select
import yaml
import sys
if __name__ == "__main__":
if len(sys.argv) == 2:
try:
with open(sys.argv[1]) as f:
YAML = yaml.safe_load(f)
except:
print("ERROR: {FILE} is a not a valid yaml object".format(
FILE=sys.argv[1]))
sys.exit(1)
ycat.ycat(YAML)
elif select.select([sys.stdin, ], [], [], 30)[0]:
try:
YAML = yaml.safe_load(sys.stdin.read())
except:
print("ERROR: stdin data is not a valid yaml object")
sys.exit(1)
ycat.ycat(YAML)
else:
print("""USAGE:
cat some.yaml | ycat
ycat some.yaml
""")
sys.exit(0)

19
setup.py

@ -0,0 +1,19 @@
from setuptools import setup
from os import path
this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
setup(name='ycat',
version='1',
description='display yaml in flat format',
long_description=long_description,
long_description_content_type='text/markdown',
url='https://git.osuv.de/m/ycat',
author='Markus Bergholz',
author_email='markuman@gmail.com ',
license='WTFPL',
packages=['ycat'],
scripts=['bin/ycat'],
zip_safe=True)

43
ycat/__init__.py

@ -0,0 +1,43 @@
def list_handler(VALUE, PARENT_PATH):
for idx in range(len(VALUE)):
value = VALUE[idx]
if isinstance(value, dict):
dict_handler(value, "{KEY}[{IDX}]".format(
KEY=PARENT_PATH, IDX=idx))
elif isinstance(value, list):
list_handler(value, "{KEY}[{IDX}]".format(
KEY=PARENT_PATH, IDX=idx))
elif isinstance(value, str):
print('.{KEY}[{IDX}] = "{VALUE}"'.format(
KEY=PARENT_PATH, IDX=idx, VALUE=value))
else:
print(".{KEY}[{IDX}] = {VALUE}".format(
KEY=PARENT_PATH, IDX=idx, VALUE=value))
def dict_handler(VALUE, PARENT_PATH):
for key in VALUE:
value = VALUE.get(key)
if isinstance(value, dict):
dict_handler(value, PARENT_PATH + "." + key)
elif isinstance(value, list):
list_handler(value, PARENT_PATH + "." + key)
elif isinstance(value, str):
print('.{PARENT_PATH}.{KEY} = "{VALUE}"'.format(
PARENT_PATH=PARENT_PATH, KEY=key, VALUE=value))
else:
print(".{PARENT_PATH}.{KEY} = {VALUE}".format(
PARENT_PATH=PARENT_PATH, KEY=key, VALUE=value))
def ycat(YAML):
for key in YAML:
value = YAML.get(key)
if isinstance(value, dict):
dict_handler(value, key)
elif isinstance(value, list):
list_handler(value, key)
elif isinstance(value, str):
print('.{KEY} = "{VALUE}"'.format(KEY=key, VALUE=value))
else:
print(".{KEY} = {VALUE}".format(KEY=key, VALUE=value))
Loading…
Cancel
Save