zerwes/hiyapyco

RFE: Support multiple documents in the same file

morxa opened this issue · 5 comments

morxa commented

Currently, only one document per file is supported.

Consider this simple example:
In c1.yaml:

v: 1
---
v: 2

Actual behavior

Running python -c "import hiyapyco; conf = hiyapyco.load('c1.yaml')" will result in:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/thofmann/.local/lib/python2.7/site-packages/hiyapyco/__init__.py", line 426, in load
    hiyapyco = HiYaPyCo(*args, **kwargs)
  File "/home/thofmann/.local/lib/python2.7/site-packages/hiyapyco/__init__.py", line 205, in __init__
    ydata = odyldo.safe_load(f)
  File "/home/thofmann/.local/lib/python2.7/site-packages/hiyapyco/odyldo.py", line 70, in safe_load
    return yaml.load(stream, ODYL)
  File "/usr/lib64/python2.7/site-packages/yaml/__init__.py", line 71, in load
    return loader.get_single_data()
  File "/usr/lib64/python2.7/site-packages/yaml/constructor.py", line 37, in get_single_data
    node = self.get_single_node()
  File "/usr/lib64/python2.7/site-packages/yaml/composer.py", line 43, in get_single_node
    event.start_mark)
yaml.composer.ComposerError: expected a single document in the stream
  in "/home/thofmann/test/hiyapyco/c1.yaml", line 1, column 1
but found another document
  in "/home/thofmann/test/hiyapyco/c1.yaml", line 2, column 1

Expected behavior

I expected the same behavior as if those documents were in separate files.

have to use yaml.safe_load_all

---
a: aaa 
b: bbb 
d:
        a: a
        b: b
        c: c

...
---
a: xxx 
d:
        a: x
        z: z

and load

>>> import yaml
>>> import os
>>> fd=open('/tmp/2.yaml', 'r')
>>> for ydata in yaml.safe_load_all(fd):
...     print ydata
... 
{'a': 'aaa', 'b': 'bbb', 'd': {'a': 'a', 'c': 'c', 'b': 'b'}}
{'a': 'xxx', 'd': {'a': 'x', 'z': 'z'}}
morxa commented

I'm not sure if this was a note to yourself or intended for me. I want to merge those documents, so obviously just using safe_load_all won't work, and there is no safe_load_all in hiyapyco.

Of course I can still load the yaml myself and then feed the yaml strings into hiyapyco, but having safe_load_all in hiyapyco would make it much simpler.

sorry ... was something like a start progress for the issue and a note for me how to implement this ...stay tuned, will be released soon

release 0.4.13 is out w/ greeting to Öcher

morxa commented

Great, thank you!