Browse Source

added reyaml script

Fabian Peter Hammerle 8 years ago
parent
commit
6ca29bf9b9
2 changed files with 118 additions and 0 deletions
  1. 49 0
      scripts/reyaml
  2. 69 0
      tests/scripts/test_reyaml.py

+ 49 - 0
scripts/reyaml

@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+# PYTHON_ARGCOMPLETE_OK
+
+import sys
+import yaml
+
+def reyaml(input_path, output_path):
+
+    if input_path:
+        with open(input_path, 'r') as input_file:
+            input_yaml = input_file.read()
+    else:
+        input_yaml = sys.stdin.read()
+
+    output_yaml = yaml.dump(
+        yaml.load(input_yaml),
+        default_flow_style = False,
+        )
+
+    if output_path:
+        with open(output_path, 'w') as output_file:
+            output_file.write(output_yaml)
+    else:
+        sys.stdout.write(output_yaml)
+
+def _init_argparser():
+
+    import argparse
+    argparser = argparse.ArgumentParser(description = None)
+    argparser.add_argument('-i', dest = 'input_path')
+    argparser.add_argument('-o', dest = 'output_path')
+    return argparser
+
+def main(argv):
+
+    argparser = _init_argparser()
+    try:
+        import argcomplete
+        argcomplete.autocomplete(argparser)
+    except ImportError:
+        pass
+    args = argparser.parse_args(argv)
+
+    reyaml(**vars(args))
+
+    return 0
+
+if __name__ == "__main__":
+    sys.exit(main(sys.argv[1:]))

+ 69 - 0
tests/scripts/test_reyaml.py

@@ -0,0 +1,69 @@
+# -*- coding: utf-8 -*-
+import pytest
+
+pytest.importorskip('yaml')
+import os
+import subprocess
+
+script_path = os.path.realpath(os.path.join(__file__, '..', '..', '..', 'scripts', 'reyaml'))
+
+@pytest.mark.parametrize(('stdin', 'params', 'expected_stdout'), [
+    ['a: b\n', [], 'a: b\n'],
+    ['{a: b}\n', [], 'a: b\n'],
+    ['[a, b, c]\n', [], '- a\n- b\n- c\n'],
+    ])
+def test_params(stdin, params, expected_stdout):
+    p = subprocess.Popen(
+            [script_path],
+            stdin = subprocess.PIPE,
+            stdout = subprocess.PIPE,
+            )
+    stdout, stderr = p.communicate(stdin)
+    assert expected_stdout == stdout
+
+def test_file_input(tmpdir):
+    input_file = tmpdir.join('in')
+    input_file.write('a: b')
+    assert 'a: b\n' == subprocess.check_output(
+            [script_path, '-i', input_file.strpath],
+            )
+
+def test_file_output(tmpdir):
+    output_file = tmpdir.join('out')
+    p = subprocess.Popen(
+            [script_path, '-o', output_file.strpath],
+            stdin = subprocess.PIPE,
+            stdout = subprocess.PIPE,
+            stderr = subprocess.PIPE,
+            )
+    stdout, stderr = p.communicate('a: b')
+    assert stdout == ''
+    assert stderr == ''
+    assert 'a: b\n' == output_file.read()
+
+def test_file_input_output(tmpdir):
+    input_file = tmpdir.join('in')
+    input_file.write('c: d')
+    output_file = tmpdir.join('out')
+    p = subprocess.Popen(
+            [script_path, '-i', input_file.strpath, '-o', output_file.strpath],
+            stdout = subprocess.PIPE,
+            stderr = subprocess.PIPE,
+            )
+    stdout, stderr = p.communicate()
+    assert stdout == ''
+    assert stderr == ''
+    assert 'c: d\n' == output_file.read()
+
+def test_file_input_output_same(tmpdir):
+    io_file = tmpdir.join('io')
+    io_file.write('{b: 3}')
+    p = subprocess.Popen(
+            [script_path, '-i', io_file.strpath, '-o', io_file.strpath],
+            stdout = subprocess.PIPE,
+            stderr = subprocess.PIPE,
+            )
+    stdout, stderr = p.communicate()
+    assert stdout == ''
+    assert stderr == ''
+    assert 'b: 3\n' == io_file.read()