Beispiel #1
0
def test_flexible_group():
    config = hieropt.Group('config')
    config.register(hieropt.Group('strings', Child=hieropt.Value))
    config.register(hieropt.Group('ints', Child=hieropt.Int))
    assert_write_then_read_equivalence(config)
    config.strings.s1.set('foo')
    assert_write_then_read_equivalence(config)
    assert_equals(config.strings.s1(), 'foo')
    config.strings.s2.set('bar')
    assert_write_then_read_equivalence(config)
    assert_equals(config.strings.s2(), 'bar')
    config.ints.x.set(1)
    assert_equals(config.ints.x(), 1)
    assert_write_then_read_equivalence(config)
    config.ints.y.set(2)
    assert_equals(config.ints.y(), 2)
    assert_write_then_read_equivalence(config)
    fp = sio("""
config.strings.s3: baz
config.ints.z: 3
""")
    config.readfp(fp)
    assert_equals(config.strings.s3(), 'baz')
    assert_equals(config.ints.z(), 3)
    assert_write_then_read_equivalence(config)
Beispiel #2
0
def test_writefp_annotate():
    config = hieropt.Group('config')
    config.register(hieropt.Int('x', default=1, comment='config int x'))
    fp = sio()
    config.writefp(fp)
    assert_equals(fp.getvalue().strip(), """
# config int x
# config.x: 1

""".strip())

    fp = sio()
    config.writefp(fp, annotate=False)
    assert_equals(fp.getvalue().strip(), """
# config.x: 1
""".strip())

    config.x.set(2)
    fp = sio()
    config.writefp(fp)
    assert_equals(fp.getvalue().strip(), """
# config int x
config.x: 2
""".strip())

    fp = sio()
    config.writefp(fp, annotate=False)
    assert_equals(fp.getvalue().strip(), """
config.x: 2
""".strip())
Beispiel #3
0
def test_inflexible_group():
    config = hieropt.Group('config')
    config.register(hieropt.Int('x', default=1))
    assert_raises(AttributeError, getattr, config, 'y')
    fp = sio("""
config.x: 2
config.y: 2
""")
    assert_raises(hieropt.UnregisteredName, config.readfp, fp)
Beispiel #4
0
def test_parent_supplies_default():
    config = hieropt.Group('config')
    config.register(hieropt.Int('x', default=1))
    config.x.register(hieropt.Int('y', default=hieropt.parent))
    assert_equals(config.x(), 1)
    assert_equals(config.x.y(), 1)
    assert_write_then_read_equivalence(config)
    config.x.set(2)
    assert_equals(config.x(), 2)
    assert_equals(config.x.y(), 2)
    assert_write_then_read_equivalence(config)
    config.x.y.set(3)
    assert_equals(config.x(), 2)
    assert_equals(config.x.y(), 3)
    assert_write_then_read_equivalence(config)
Beispiel #5
0
def makeSimpleWithDefaultsAndComments(**kwargs):
    simple = hieropt.Group('simple', comment='simple group', **kwargs)
    simple.register(hieropt.Int('int', 1, comment='simple int'))
    simple.register(hieropt.Bool('bool', True, comment='simple bool'))
    simple.register(hieropt.Float('float', 1.0, comment='simple float'))
    return simple
Beispiel #6
0
def makeSimpleWithDefaults(**kwargs):
    simple = hieropt.Group('simple', **kwargs)
    simple.register(hieropt.Int('int', 1))
    simple.register(hieropt.Bool('bool', True))
    simple.register(hieropt.Float('float', 1.0))
    return simple
Beispiel #7
0
def makeSimple(**kwargs):
    simple = hieropt.Group('simple', **kwargs)
    simple.register(hieropt.Int('int'))
    simple.register(hieropt.Bool('bool'))
    simple.register(hieropt.Float('float'))
    return simple
Beispiel #8
0
def test_callable_as_default():
    config = hieropt.Group('config')
    def callable_default():
        return 1.5
    config.register(hieropt.Float('float', default=callable_default))
    assert_equals(config.float(), 1.5)