Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dist_check:
options:
exclude:
- ["v:3.6", "pkg:old"] ## old version is breaking python 3.6 pkg_resources
- ["v:3.7", "pkg:old"] ## old version is breaking python 3.7 pkg_resources
tests:
- label: install
matrix:
Expand Down Expand Up @@ -80,7 +81,7 @@ dist_check:
EOF
- |
cd /tmp
echo PYTHONPATH: $PYTHONPATH
echo PYTHONPATH: "${PYTHONPATH:-<empty>}"
python -c 'import shyaml'
- |
[ -e /tmp/not-installed ] || {
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2012-2018, Valentin Lab
Copyright (c) 2012-2019, Valentin Lab
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
126 changes: 100 additions & 26 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ implementation but some examples will fail depending on the
implementation. To make things clear, I'll use some annotation and you
can yourself check which version you are using with::

$ shyaml -V | grep "^libyaml used:" ## shtest: if-success-set LIBYAML
$ shyaml -V | grep "^libyaml used:" ## docshtest: if-success-set LIBYAML
libyaml used: True


Expand Down Expand Up @@ -121,25 +121,41 @@ Simple query of simple attribute::
$ cat test.yaml | shyaml get-value name
MyName !!

$ shyaml --file test.yaml get-value name
MyName !!

Query nested attributes by using '.' between key labels::

$ cat test.yaml | shyaml get-value subvalue.how-much
1.1

$ shyaml --file test.yaml get-value subvalue.how-much
1.1

Get type of attributes::

$ cat test.yaml | shyaml get-type name
str
$ cat test.yaml | shyaml get-type subvalue.how-much
float

$ shyaml --file test.yaml get-type name
str
$ shyaml --file test.yaml get-type subvalue.how-much
float

Get length of structures or sequences::

$ cat test.yaml | shyaml get-length subvalue
5
$ cat test.yaml | shyaml get-length subvalue.things
3

$ shyaml --file test.yaml get-length subvalue
5
$ shyaml --file test.yaml get-length subvalue.things
3

But this won't work on other types::

$ cat test.yaml | shyaml get-length name
Expand All @@ -153,7 +169,7 @@ Get sub YAML from a structure attribute::

$ cat test.yaml | shyaml get-type subvalue
struct
$ cat test.yaml | shyaml get-value subvalue ## shtest: ignore-if LIBYAML
$ cat test.yaml | shyaml get-value subvalue ## docshtest: ignore-if LIBYAML
how-much: 1.1
how-many: 2
things:
Expand All @@ -178,6 +194,13 @@ Iteration through keys only::
subvalue.how-much\more
subvalue.how-much\.more

$ shyaml --file test.yaml keys
name
subvalue
subvalue.how-much
subvalue.how-much\more
subvalue.how-much\.more

Iteration through keys only (``\0`` terminated strings)::

$ cat test.yaml | shyaml keys-0 subvalue | xargs -0 -n 1 echo "VALUE:"
Expand All @@ -187,6 +210,13 @@ Iteration through keys only (``\0`` terminated strings)::
VALUE: maintainer
VALUE: description

$ shyaml --file test.yaml keys-0 subvalue | xargs -0 -n 1 echo "VALUE:"
VALUE: how-much
VALUE: how-many
VALUE: things
VALUE: maintainer
VALUE: description

Iteration through values only (``\0`` terminated string highly recommended)::

$ cat test.yaml | shyaml values-0 subvalue |
Expand Down Expand Up @@ -267,6 +297,11 @@ Query a sequence with ``get-value``::
- second
- third

$ shyaml --file test.yaml get-value subvalue.things
- first
- second
- third

And access individual elements with python-like indexing::

$ cat test.yaml | shyaml get-value subvalue.things.0
Expand All @@ -289,6 +324,11 @@ More usefull, parse a list in one go with ``get-values``::
second
third

$ shyaml --file test.yaml get-values subvalue.things
first
second
third

Note that the action is called ``get-values``, and that output is
separated by newline char(s) (which is os dependent), this can bring
havoc if you are parsing values containing newlines itself. Hopefully,
Expand Down Expand Up @@ -363,7 +403,7 @@ So::
break
fi
echo "---"
done | shyaml get-value -y ingests.0.id
done | shyaml -y get-value ingests.0.id
tag-1
...
---
Expand Down Expand Up @@ -426,7 +466,7 @@ With the ``-L``, if we kill our shyaml process before the end::
fi
echo "---"
sleep 10
done 2>/dev/null | shyaml get-value -L ingests.0.id & pid=$! ; sleep 2; kill $pid
done 2>/dev/null | shyaml -L get-value ingests.0.id & pid=$! ; sleep 2; kill $pid
tag-1


Expand All @@ -443,7 +483,7 @@ which could help you chain shyaml calls::
fi
echo "---"
sleep 0.2
done | shyaml get-value ingests.0 -L -y | shyaml get-value id | tr '\0' '\n'
done | shyaml -L -y get-value ingests.0 | shyaml get-value id | tr '\0' '\n'
tag-1
tag-2
tag-3
Expand All @@ -469,13 +509,24 @@ uses single quotes)::
$ cat test.yaml | shyaml get-value 'subvalue\.how-much\\.more' default
default

$ shyaml --file test.yaml get-value 'subvalue\.how-much'
1.2
$ shyaml --file test.yaml get-value 'subvalue\.how-much\\more'
1.3
$ shyaml --file test.yaml get-value 'subvalue\.how-much\\.more' default
default

This last one didn't escape correctly the last ``.``, this is the
correct version::

$ cat test.yaml | shyaml get-value 'subvalue\.how-much\\\.more' default
1.4


$ shyaml --file test.yaml get-value 'subvalue\.how-much\\\.more' default
1.4


empty string keys
-----------------

Expand Down Expand Up @@ -535,7 +586,7 @@ an error message will be printed::
$ echo "a: 3" | shyaml get-value b
Error: invalid path 'b', missing key 'b' in struct.

You can emulate pre v0.3 behavior by specifying explicitely an empty
You can emulate pre v0.3 behavior by specifying explicitly an empty
string as third argument::

$ echo "a: 3" | shyaml get-value b ''
Expand Down Expand Up @@ -582,6 +633,11 @@ For ``shyaml`` version before ``0.4.0``::
b: 3
c: 2

# shyaml --file test.yaml get-value mapping
a: 1
b: 3
c: 2

For ``shyaml`` version including and after ``0.4.0``::

$ shyaml get-value mapping < test.yaml
Expand All @@ -590,6 +646,12 @@ For ``shyaml`` version including and after ``0.4.0``::
b: 3


$ shyaml --file test.yaml get-value mapping
a: 1
c: 2
b: 3


Strict YAML for further processing
----------------------------------

Expand All @@ -603,7 +665,7 @@ when processing YAML coming out of shyaml, you should probably think
about using the ``--yaml`` (or ``-y``) option to output only strict YAML.

With the drawback that when you'll want to output string, you'll need to
call a last time ``shyaml get-value`` to explicitely unquote the YAML.
call a last time ``shyaml get-value`` to explicitly unquote the YAML.


Object Tag
Expand All @@ -629,7 +691,7 @@ allow parsing their internal structure.
``get-value`` with ``-y`` (see section Strict YAML) will give you the
complete yaml tagged value::

$ shyaml get-value -y 0 < test.yaml ## shtest: ignore-if LIBYAML
$ shyaml -y get-value 0 < test.yaml ## docshtest: ignore-if LIBYAML
!<tag:example.com,2000:app/foo> 'bar'


Expand All @@ -656,7 +718,7 @@ Another example::

And you can still traverse internal value::

$ shyaml get-value -y 2.start < test.yaml
$ shyaml -y get-value 2.start < test.yaml
x: 73
y: 129

Expand All @@ -678,7 +740,7 @@ Note that all global tags will be resolved and simplified (as
}
EOF

$ shyaml get-value < test.yaml ## shtest: ignore-if LIBYAML
$ shyaml get-value < test.yaml ## docshtest: ignore-if LIBYAML
sequence:
- one
- two
Expand All @@ -693,7 +755,7 @@ Empty documents
When provided with an empty document, ``shyaml`` will consider the
document to hold a ``null`` value::

$ echo | shyaml get-value -y
$ echo | shyaml -y get-value
null
...

Expand All @@ -704,30 +766,26 @@ Usage string
A quick reminder of what is available will be printed when calling
``shyaml`` without any argument::

$ shyaml
Error: Bad number of arguments.
Usage:
$ shyaml
usage:

shyaml {-h|--help}
shyaml {-V|--version}
shyaml [-y|--yaml] [-q|--quiet] ACTION KEY [DEFAULT]
<BLANKLINE>
shyaml [-y|--yaml] [-q|--quiet] [--file FILE] ACTION KEY [DEFAULT]

The full help is available through the usage of the standard ``-h`` or
``-help``::


$ shyaml --help

Parses and output chosen subpart or values from YAML input.
It reads YAML in stdin and will output on stdout it's return value.

Usage:
usage:

shyaml {-h|--help}
shyaml {-V|--version}
shyaml [-y|--yaml] [-q|--quiet] ACTION KEY [DEFAULT]
shyaml [-y|--yaml] [-q|--quiet] [--file FILE] ACTION KEY [DEFAULT]

Parses and output chosen subpart or values from YAML input.
It reads YAML in stdin and will output on stdout it's return value.

Options:

Expand All @@ -745,6 +803,10 @@ The full help is available through the usage of the standard ``-h`` or
standard error.
(Default: no quiet mode)

--file FILE
Read from FILE
(Default: read from stdin)

-L, --line-buffer
Force parsing stdin line by line allowing to process
streamed YAML as it is fed instead of buffering
Expand Down Expand Up @@ -803,17 +865,29 @@ The full help is available through the usage of the standard ``-h`` or
## get YAML config part of 'myhost'
cat hosts_config.yaml | shyaml get-value cfgs.myhost

<BLANKLINE>
positional arguments:
action
key
default

optional arguments:
-h, --help show this help message and exit
--file FILE
-y, --yaml
-q, --quiet
-L, --line-buffer
-V, --version show program's version number and exit


Using invalid keywords will issue an error and the usage message::

$ shyaml get-foo
Error: 'get-foo' is not a valid action.
Usage:


shyaml {-h|--help}
shyaml {-V|--version}
shyaml [-y|--yaml] [-q|--quiet] ACTION KEY [DEFAULT]
shyaml [-y|--yaml] [-q|--quiet] [--file FILE] ACTION KEY [DEFAULT]
<BLANKLINE>


Expand Down Expand Up @@ -905,7 +979,7 @@ would show you how to deal with your issue.
License
=======

Copyright (c) 2018 Valentin Lab.
Copyright (c) 2019 Valentin Lab.

Licensed under the `BSD License`_.

Expand Down
9 changes: 5 additions & 4 deletions bin/test
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
#!/bin/bash

shtest_opts=()
docshtest_opts=()

if [ -z "$DOVIS" -o "$PKG_COVERAGE" ]; then ## with coverage
echo "With coverage support enabled"
python="coverage run --include ./shyaml.py -a"
shtest_opts+=("-r" '#\bshyaml\b#'"$coverage"' shyaml#')
else
echo "No coverage support"
python=python
fi

docshtest_opts+=("-r" '#\bshyaml\b#'"$python"' ./shyaml.py#')

$python -m doctest shyaml.py || exit 1
$python -m doctest README.rst || exit 1

if python -c 'import yaml; exit(0 if yaml.__with_libyaml__ else 1)' 2>/dev/null; then
echo "PyYAML has C libyaml bindings available... Testing with libyaml"
export FORCE_PYTHON_YAML_IMPLEMENTATION=
time ./shtest.py README.rst -r '#\bshyaml\b#'"$python"' ./shyaml.py#' || exit 1
time docshtest README.rst "${docshtest_opts[@]}" || exit 1
else
echo "PyYAML has NOT any C libyaml bindings available..."
fi
echo "Testing with python implementation"
export FORCE_PYTHON_YAML_IMPLEMENTATION=1
time ./shtest.py README.rst -r '#\bshyaml\b#'"$python"' ./shyaml.py#' || exit 1
time docshtest README.rst "${docshtest_opts[@]}" || exit 1
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@

setup(
setup_requires=['d2to1'],
extras_require={'test': []},
extras_require={'test': [
"docshtest==0.0.2",
]},
d2to1=True
)
Loading