Beispiel #1
0
def test_dump_untyped() -> None:
    dumps = yatiml.dumps_function(Document6)
    data = Document6(24, 'LMitAY')
    text = dumps(data)
    assert text == (
            'attr1: 24\n'
            'attr2: LMitAY\n')
Beispiel #2
0
def test_remove_defaulted_attribute() -> None:
    dumps = yatiml.dumps_function(
            Document3, Color2, Shape, Rectangle, Circle, Vector2D)
    data = Document3(Vector2D(1.2, 3.4))
    data.another_number = 13
    text = dumps(data)
    assert text == 'cursor_at:\n  x: 1.2\n  y: 3.4\nanother_number: 13\n'

    data.color = 'blue'
    data.age = 8
    data.has_siblings = True
    data.score = 5.5
    data.extra_shape = Circle(Vector2D(1.0, 2.0), 3.0)
    data.another_number = 42
    text = dumps(data)
    assert text == (
            'cursor_at:\n'
            '  x: 1.2\n'
            '  y: 3.4\n'
            'color: blue\n'
            'age: 8\n'
            'has_siblings: true\n'
            'score: 5.5\n'
            'extra_shape:\n'
            '  center:\n'
            '    x: 1.0\n'
            '    y: 2.0\n'
            '  radius: 3.0\n')
Beispiel #3
0
def test_dump_any() -> None:
    dumps = yatiml.dumps_function(Document5)
    data = Document5(42, 'YAtiML')
    text = dumps(data)
    assert text == (
            'attr1: 42\n'
            'attr2: YAtiML\n')
Beispiel #4
0
def test_dump_custom_attributes() -> None:
    dumps = yatiml.dumps_function(Extensible)
    extra_attributes = OrderedDict([('b', 5), ('c', 3)])
    data = Extensible(10, _yatiml_extra=extra_attributes)
    text = dumps(data)
    assert text == (
            'a: 10\n'
            'b: 5\n'
            'c: 3\n')
def test_reference_io() -> None:
    load_reference = yatiml.load_function(Reference, Identifier)

    text = 'test[12]'
    doc = load_reference(text)
    assert str(doc[0]) == 'test'
    assert doc[1] == 12

    dump_reference = yatiml.dumps_function(Identifier, Reference)

    doc = Reference('test[12].testing.ok.index[3][5]')
    text = dump_reference(doc)
    assert text == 'test[12].testing.ok.index[3][5]\n...\n'
def test_dump_settings() -> None:
    dump_settings = yatiml.dumps_function(Identifier, Reference, Settings)

    settings = Settings(
        OrderedDict([('domain1._muscle_grain', [0.01]),
                     ('domain1._muscle_extent', [1.5]),
                     ('submodel1._muscle_timestep', 0.001),
                     ('submodel1._muscle_total_time', 100.0),
                     ('test_str', 'value'), ('test_int', 12),
                     ('test_bool', True), ('test_list', [12.3, 1.3])]))

    text = dump_settings(settings)
    assert text == ('domain1._muscle_grain: [0.01]\n'
                    'domain1._muscle_extent: [1.5]\n'
                    'submodel1._muscle_timestep: 0.001\n'
                    'submodel1._muscle_total_time: 100.0\n'
                    'test_str: value\n'
                    'test_int: 12\n'
                    'test_bool: true\n'
                    'test_list: [12.3, 1.3]\n')
Beispiel #7
0
def test_dump_complex_document() -> None:
    dumps = yatiml.dumps_function(
            Document2, Color2, Shape, Rectangle, Circle, Vector2D)
    shape1 = Circle(Vector2D(5.0, 6.0), 12.0)
    shape2 = Rectangle(Vector2D(-2.0, -5.0), 3.0, 7.0)
    data = Document2(Vector2D(3.0, 4.0), [shape1, shape2])
    text = dumps(data)
    assert text == (
            'cursor_at:\n'
            '  x: 3.0\n'
            '  y: 4.0\n'
            'shapes:\n'
            '- center:\n'
            '    x: 5.0\n'
            '    y: 6.0\n'
            '  radius: 12.0\n'
            '- center:\n'
            '    x: -2.0\n'
            '    y: -5.0\n'
            '  width: 3.0\n'
            '  height: 7.0\n'
            'color: red\n'
            'extra_shape:\n'
            )
Beispiel #8
0
def test_dump_with_path() -> None:
    dumps = yatiml.dumps_function(WithPath)
    data = WithPath(Path('dir/subdir/test.txt'))
    text = dumps(data)
    assert text == 'path: dir/subdir/test.txt\n'
Beispiel #9
0
def test_dump_rel_path() -> None:
    dumps = yatiml.dumps_function()
    text = dumps(Path('dir/testing.txt'))
    assert text == 'dir/testing.txt\n...\n'
Beispiel #10
0
def test_dump_abs_path() -> None:
    dumps = yatiml.dumps_function()
    text = dumps(Path('/tmp/testing.txt'))
    assert text == '/tmp/testing.txt\n...\n'
Beispiel #11
0
        self.an_attribute = an_attribute
        self.another_attribute = another_attribute

    @classmethod
    def _yatiml_savorize(cls, node: yatiml.Node) -> None:
        node.dashes_to_unders_in_keys()

    @classmethod
    def _yatiml_sweeten(cls, node: yatiml.Node) -> None:
        node.unders_to_dashes_in_keys()


# Create loader
load = yatiml.load_function(Dashed)

# Create dumper
dumps = yatiml.dumps_function(Dashed)

# Load YAML
yaml_text = ('an-attribute: 42\n' 'another-attribute: with-dashes\n')
doc = load(yaml_text)

print(type(doc))
print(doc.an_attribute)
print(doc.another_attribute)

# Dump YAML

dumped_text = dumps(doc)
print(dumped_text)
Beispiel #12
0
def test_dump_user_string() -> None:
    dumps = yatiml.dumps_function(ConstrainedString)
    text = dumps(ConstrainedString('abc'))
    assert text == 'abc\n...\n'
def dump_configuration() -> Callable:
    return yatiml.dumps_function(
            Configuration, Document, Identifier, Implementation,
            PartialConfiguration, Reference, Resources, Settings)
Beispiel #14
0
def test_dump_str() -> None:
    dumps = yatiml.dumps_function()
    text = dumps('test')
    assert text == 'test\n...\n'
Beispiel #15
0
def test_dump_object_int_fix() -> None:
    dumps = yatiml.dumps_function(BoolFixTester)
    text = dumps(BoolFixTester(21))
    assert text == 'x: 21\n'
Beispiel #16
0
    Args:
        source: A string containing yMMSL data, a pathlib Path to a
                file containing yMMSL data, or an open file-like
                object containing from which yMMSL data can be read.

    Returns:
        A PartialConfiguration object corresponding to the input data.

    """
    # This wrapper is just here to render the documentation.
    return _load(source)


_dump = yatiml.dumps_function(Component, Conduit, Configuration, Document,
                              Identifier, Implementation, Model,
                              ModelReference, PartialConfiguration, Reference,
                              Resources, Settings)


def dump(config: PartialConfiguration) -> str:
    """Converts a Ymmsl to a string containing YAML.

    Args:
        config: The configuration to be saved to yMMSL.

    Returns:
        A yMMSL YAML description of the given document.

    """
    # This wrapper is just here to render the documentation.
    return _dump(config)
Beispiel #17
0
 def test_dump_data_class() -> None:
     dumps = yatiml.dumps_function(DataClass)
     text = dumps(DataClass('test'))
     assert text == (
             'attr1: test\n'
             'attr2: 42\n')
Beispiel #18
0
def test_dump_parsed_class() -> None:
    dumps = yatiml.dumps_function(Postcode)
    text = dumps(Postcode(1098, 'XG'))
    assert text == '1098 XG\n...\n'
Beispiel #19
0
def test_dump_string_like_key() -> None:
    dumps = yatiml.dumps_function(StringLike)
    data = {StringLike('abcd'): 13}
    text = dumps(data)
    assert text == 'abcd: 13\n'
Beispiel #20
0
def test_dump_user_string_key() -> None:
    dumps = yatiml.dumps_function(ConstrainedString)
    data = {ConstrainedString('abcd'): 10}
    text = dumps(data)
    assert text == 'abcd: 10\n'
Beispiel #21
0
def test_dump_user_string_like() -> None:
    dumps = yatiml.dumps_function(StringLike)
    text = dumps(StringLike('abcd'))
    assert text == 'abcd\n...\n'
Beispiel #22
0
from typing import Optional, Union
import yatiml


# Create document class
class Submission:
    def __init__(self,
                 name: str,
                 age: Union[int, str],
                 tool: Optional[str] = None) -> None:
        self.name = name
        self.age = age
        self.tool = tool


# Create dumper
dumps = yatiml.dumps_function(Submission)

# Dump YAML
doc = Submission('Youssou', 7, 'pencils')
yaml_text = dumps(doc)

print(yaml_text)
def dump_document() -> Callable:
    return yatiml.dumps_function(Document)
Beispiel #24
0
def test_dump_enum2() -> None:
    # check that we don't generate cross-references for enum values
    # regression test
    dumps = yatiml.dumps_function(Color)
    text = dumps([Color.blue, Color.blue])
    assert text == '- blue\n- blue\n'
Beispiel #25
0
def test_dump_ordereddict() -> None:
    dumps = yatiml.dumps_function()
    data = collections.OrderedDict([('x', 1), ('y', 2)])
    text = dumps(data)
    assert text == 'x: 1\ny: 2\n'
Beispiel #26
0
def dump_model() -> Callable:
    return yatiml.dumps_function(Component, Conduit, Identifier, Model,
                                 Reference)
Beispiel #27
0
def test_dump_object_bool_fix() -> None:
    dumps = yatiml.dumps_function(BoolFixTester)
    text = dumps(BoolFixTester(True))
    assert text == 'x: true\n'
Beispiel #28
0
def test_enum_sweeten() -> None:
    dumps = yatiml.dumps_function(Color2)
    text = dumps(Color2.YELLOW)
    assert text == 'yellow\n...\n'
Beispiel #29
0
        self.references = references
        self.contact = contact
        self.doi = doi
        self.commit = commit
        self.license = license
        self.license_url = license_url
        self.repository = repository
        self.repository_code = repository_code
        self.repository_artifact = repository_artifact
        self.url = url

    @classmethod
    def _yatiml_savorize(cls, node: yatiml.Node) -> None:
        node.dashes_to_unders_in_keys()

    @classmethod
    def _yatiml_sweeten(cls, node: yatiml.Node) -> None:
        node.unders_to_dashes_in_keys()


_all_classes = (CitationCFF, Identifier, Person, Entity, Reference)


load = yatiml.load_function(*_all_classes)


dump = yatiml.dump_function(*_all_classes)


dumps = yatiml.dumps_function(*_all_classes)
def test_dump_to_string() -> None:
    dumps = yatiml.dumps_function()
    yaml_text = dumps({'x': 1})
    assert yaml_text == 'x: 1\n'