def test_lconf_prepare_and_parse_section_ok1():
   """ Tests: test_lconf_prepare_and_parse_section_ok1
   """
   print('::: TEST: test_lconf_prepare_and_parse_section_ok1()')

   lconf_section__template_obj = Root([
      ('keyvalue_mapping', KVMap([
         ('#1', '# Comment-Line:  Key-Value-Mapping items: `Key :: Value Pairs`'),
         ('mapping_key1', 'default'),
         ('mapping_key2', 'default'),
         ('mapping_key3', 'default'),
      ])),
   ])

   lconf_section_raw_str = r'''___SECTION :: Test Example1

. keyvalue_mapping
   mapping_key1 :: Something
   mapping_key2 :: Something2
   mapping_key3 :: Something3
___END
'''
   lconf_obj = lconf_prepare_and_parse_section(
      lconf_section_raw_str,
      lconf_section__template_obj,
      with_comments=False,
      validate=True
   )
   ok_(isinstance(lconf_obj, LconfRoot), msg=None)
   ok_(isinstance(lconf_obj['keyvalue_mapping'], LconfKVMap), msg=None)
   eq_(lconf_obj['keyvalue_mapping']['mapping_key1'], 'Something', msg=None)
   eq_(lconf_obj['keyvalue_mapping']['mapping_key3'], 'Something3', msg=None)
   eq_(lconf_obj['keyvalue_mapping'].key_order, ['mapping_key1', 'mapping_key2', 'mapping_key3'], msg=None)
def test_lconf_emit_default_obj__ok6():
    """ Tests: test_lconf_emit_default_obj__ok6
   """
    print('::: TEST: test_lconf_emit_default_obj__ok6()')

    lconf_section__template_obj = Root([
        ('keyvalue_mapping',
         KVMap([
             ('#1',
              '# Comment-Line:  Key-Value-Mapping items: `Key :: Value Pairs`'
              ),
             ('mapping_key1', 'Some long sentence'),
         ])),
    ])
    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Example1',
                                                   onelinelists=LCONF_NO,
                                                   with_comments=True)

    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=True)
    eq_(section_lines[1], '. keyvalue_mapping', msg=None)
    eq_(section_lines[3], '   mapping_key1 :: Some long sentence', msg=None)

    lconf_validate_one_section_str(lconf_section_raw_str)
def test_lconf_structure_classes9():
   """ Tests: test_lconf_structure_classes9
   """
   print('::: TEST: test_lconf_structure_classes9()')

   obj_ = KVMap([
      ('key1', 'value1'),
      ('key2', 'value2', None, 'NOT-DEFINED'),
      ('key3', '', None, 'NOT-DEFINED')
   ])
   obj_.set_class__dict__item('mydata', 'new value')
   eq_(obj_.mydata, 'new value', msg=None)

   obj_.__reduce__()

   eq_(obj_.mydata, 'new value', msg=None)
   eq_(obj_.key_order, ['key1', 'key2', 'key3'], msg=None)
   eq_(obj_.key_empty_replacementvalue, {'key3': 'NOT-DEFINED', 'key2': 'NOT-DEFINED'}, msg=None)
Beispiel #4
0
def test_lconf_prepare_default_obj__ok2():
    """ Tests: test_lconf_prepare_default_obj__ok2
   """
    print('::: TEST: test_lconf_prepare_default_obj__ok2()')

    lconf_section__template_obj = Root([
        # Default Empty Line
        ('#1', ''),
        # Default Comment Line
        ('#2', '# Comment-Line: `Key :: Value Pair`'),
        ('first', ''),
        ('last', ''),
        ('sex', ''),
        ('age', '', lconf_to_int),
        ('salary', 0.0, lconf_to_float),
        ('#3', '# Comment-Line: `Key-Value-Mapping`'),
        ('favorites',
         KVMap([
             ('food', ''),
             ('sport', '', None, 'Not-Defined'),
             ('color', ''),
         ])),
        ('#4', '# Comment-Line: `Key :: Value Pair`'),
        ('registered', True, lconf_to_bool),
    ])

    default_lconf_obj = lconf_prepare_default_obj(lconf_section__template_obj,
                                                  with_comments=False)
    eq_(default_lconf_obj.key_order,
        ['first', 'last', 'sex', 'age', 'salary', 'favorites', 'registered'],
        msg=None)
    eq_(default_lconf_obj['favorites'].key_order, ['food', 'sport', 'color'],
        msg=None)
    eq_(default_lconf_obj['favorites'].key_empty_replacementvalue,
        {'sport': 'Not-Defined'},
        msg=None)

    default_lconf_obj = lconf_prepare_default_obj(lconf_section__template_obj,
                                                  with_comments=True)
    eq_(default_lconf_obj.key_order, [
        '#1', '#2', 'first', 'last', 'sex', 'age', 'salary', '#3', 'favorites',
        '#4', 'registered'
    ],
        msg=None)
    eq_(default_lconf_obj['favorites'].key_order, ['food', 'sport', 'color'],
        msg=None)
    eq_(default_lconf_obj['favorites'].key_empty_replacementvalue,
        {'sport': 'Not-Defined'},
        msg=None)
def test_lconf_key_value_mapping_ok():
    """ Tests: test_lconf_key_value_mapping_ok
   """
    print('::: TEST: test_lconf_key_value_mapping_ok()')

    lconf_section_raw_str = r'''___SECTION :: Example LCONF `Key-Value-Mapping`
# Comment-Line: below is a Main `Key-Value-Mapping`
. key_value_mapping
   key1 :: value1
   key2 :: value2
   key3 :: value3
   key4 ::
___END'''

    example_lconf_template = Root([
        # Comment-Line: below is a Main `Key-Value-Mapping`
        ('key_value_mapping',
         KVMap([
             ('#1', '# Comment-Line: Root/Main key value pair'),
             ('key1', 'default_value1'),
             ('key2', 'default_value2', transform_function),
             ('#2',
              '# Comment: Root/Main key value pair with transform_function and  `Empty-KeyValuePair-ReplacementValue`'
              ),
             ('key3', 'default_value3', transform_function,
              'empty_replacement_value3'),
             ('key4', 'default_value4', transform_function,
              'empty_replacement_value4'),
         ])),
    ])

    default_lconf_obj = lconf_prepare_default_obj(example_lconf_template,
                                                  with_comments=True)
    lconf_obj = lconf_parse_section(default_lconf_obj,
                                    lconf_section_raw_str,
                                    example_lconf_template,
                                    validate=True)
    default_lconf_obj = lconf_prepare_default_obj(example_lconf_template,
                                                  with_comments=False)
    lconf_obj = lconf_parse_section(default_lconf_obj,
                                    lconf_section_raw_str,
                                    example_lconf_template,
                                    validate=False)
def test_lconf_section_splitlines_expect_failure2():
    """ Tests: test_lconf_section_splitlines_expect_failure2
   """
    print('::: TEST: test_lconf_section_splitlines_expect_failure2()')

    # Main `Section-Template OBJ: type: Root
    lconf_section__template_obj = Root([
        ('#1',
         '# Comment-Line: below is a Main `List-Of-Tuples` with 4 columns: |Color Name|Red|Green|Blue|'
         ),
        ('list_of_color_tuples',
         ListOT(('Color Name', 'Red', 'Green', 'Blue'), [],
                column_replace_missing=('Not Defined', '-1', '-1', '-1')),
         (None, lconf_to_int, lconf_to_int, lconf_to_int)),
        ('mapping', KVMap([
            ('key1', ''),
            ('key2', ''),
        ])),
    ])

    lconf_section_raw_str = r'''___SECTION :: TestExample

# Comment-Line: below is a Main `List-Of-Tuples` with 4 items: |Color Name|Red|Green|Blue|
- list_of_color_tuples |Color Name|Red|Green|Blue|
   # Comment-Line: `List-Of-Tuples` item lines (rows)
   forestgreen,   ,   139,  34
   brick,         156,  102,  31
. mapping
   key1 ::

      # Wrong comment Indent
   key2 :: value
___END
'''
    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=True)
    default_lconf_obj = lconf_prepare_default_obj(lconf_section__template_obj,
                                                  with_comments=False)
    lconf_obj = lconf_parse_section_lines(default_lconf_obj, section_lines,
                                          section_name,
                                          lconf_section__template_obj)
Beispiel #7
0
def test_lconf_structure_classes9():
    """ Tests: test_lconf_structure_classes9
   """
    print('::: TEST: test_lconf_structure_classes9()')

    obj_ = KVMap([('key1', 'value1'), ('key2', 'value2', None, 'NOT-DEFINED'),
                  ('key3', '', None, 'NOT-DEFINED')])
    obj_.set_class__dict__item('mydata', 'new value')
    eq_(obj_.mydata, 'new value', msg=None)

    obj_.__reduce__()

    eq_(obj_.mydata, 'new value', msg=None)
    eq_(obj_.key_order, ['key1', 'key2', 'key3'], msg=None)
    eq_(obj_.key_empty_replacementvalue, {
        'key3': 'NOT-DEFINED',
        'key2': 'NOT-DEFINED'
    },
        msg=None)
def test_lconf_prepare_default_obj__parse_section_lines_ok1():
    """ Tests: test_lconf_prepare_default_obj__parse_section_lines_ok1
   """
    print(
        '::: TEST: test_lconf_prepare_default_obj__parse_section_lines_ok1()')

    lconf_section__template_obj = Root([
        ('keyvalue_mapping',
         KVMap([
             ('#1',
              '# Comment-Line:  Key-Value-Mapping items: `Key :: Value Pairs`'
              ),
             ('mapping1_key1', 'default'),
             ('mapping1_key2', 'default'),
             ('mapping1_key3', 'default'),
         ])),
        ('keyvalue_mapping2', KVMap([
            ('mapping2_key1', 'default'),
        ])),
        ('keyvalue_mapping3', KVMap([
            ('mapping3_key1', 'default'),
        ])),
    ])

    lconf_section_raw_str = r'''___SECTION :: Test Example1

# Comment: Key-Value-Mapping
. keyvalue_mapping
   mapping1_key1 :: Something
   mapping1_key2 :: Something2
   mapping1_key3 :: Something3

# Comment: empty Key-Value-Mapping: uses all default values similar if it would not be defined
. keyvalue_mapping2
___END
'''
    lconf_validate_one_section_str(lconf_section_raw_str)
    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=True)
    default_lconf_obj = lconf_prepare_default_obj(lconf_section__template_obj,
                                                  with_comments=False)
    lconf_obj = lconf_parse_section_lines(default_lconf_obj, section_lines,
                                          section_name,
                                          lconf_section__template_obj)

    ok_(isinstance(lconf_obj, LconfRoot), msg=None)
    ok_(isinstance(lconf_obj['keyvalue_mapping'], LconfKVMap), msg=None)
    eq_(lconf_obj['keyvalue_mapping']['mapping1_key1'], 'Something', msg=None)
    eq_(lconf_obj['keyvalue_mapping']['mapping1_key3'], 'Something3', msg=None)
    eq_(lconf_obj['keyvalue_mapping'].key_order,
        ['mapping1_key1', 'mapping1_key2', 'mapping1_key3'],
        msg=None)

    ok_(isinstance(lconf_obj['keyvalue_mapping2'], LconfKVMap), msg=None)
    eq_(lconf_obj['keyvalue_mapping2']['mapping2_key1'], 'default', msg=None)

    ok_(isinstance(lconf_obj['keyvalue_mapping3'], LconfKVMap), msg=None)
    eq_(lconf_obj['keyvalue_mapping3']['mapping3_key1'], 'default', msg=None)

    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)
    default_lconf_obj = lconf_prepare_default_obj(lconf_section__template_obj,
                                                  with_comments=True)
    lconf_obj = lconf_parse_section_lines(default_lconf_obj, section_lines,
                                          section_name,
                                          lconf_section__template_obj)
Beispiel #9
0
def test_lconf_structure_classes8_expect_failure():
    """ Tests: test_lconf_structure_classes8_expect_failure
   """
    print('::: TEST: test_lconf_structure_classes8_expect_failure()')

    KVMap([])
Beispiel #10
0
def test_lconf_structure_classes0_expect_failure7():
    """ Tests: test_lconf_structure_classes0_expect_failure7
   """
    print('::: TEST: test_lconf_structure_classes0_expect_failure7()')

    KVMap(('1', '2'))
        KVMap([
            ('#8',
             '# Comment-Line:  Key-Value-Mapping items: are `Key :: Value Pairs`'
             ),
            ('mapping10_key1', ''),
            ('mapping10_key2', False, lconf_to_bool),
            ('mapping10_key3', 0, lconf_to_int),

            # `Key :: Value-List` or `Key-Value-List`: type: KVList: set default_is_oneline: True
            ('#9a', ''),
            ('#9b',
             '# Comment-Line:  Key-Value-Mapping item: `Key :: Value-List` or `Key-Value-List`'
             ),
            ('mapping10_key4_list', KVList(True, [555, 9999]), lconf_to_int),

            # `Key :: Value-List` or `Key-Value-List`: type: KVList: set default_is_oneline: False
            ('#10', ''),
            ('#11',
             '# Comment-Line:  Key-Value-Mapping item: `Key :: Value-List` or `Key-Value-List`'
             ),
            ('mapping10_key5_list', KVList(False, [555, 9999]), lconf_to_int),

            # `List-Of-Tuples`: type: ListOT: one transform function for all values
            ('#12', ''),
            ('#13',
             '# Comment-Line:  Key-Value-Mapping item: `List-Of-Tuples`'),
            ('mapping10_key6_list',
             ListOT(('x', 'y'), [(9999, 9999), (9999, 9999)]), lconf_to_int),

            # `List-Of-Tuples`: type: ListOT: for each column a separate transform function
            ('#14', ''),
            ('#15',
             '# Comment-Line:  Key-Value-Mapping item: `List-Of-Tuples`'),
            ('mapping10_key7_list',
             ListOT(('name', 'b', 'c'), [('something', 11, 1234),
                                         ('something2', 9999, 9999)]),
             (None, lconf_to_float, lconf_to_int)),
        ])),
Beispiel #12
0
)

lconf_section__example_3__template_obj = Root([
    # Default Empty Line
    ('#1', ''),
    # Default Comment Line
    ('#2', '# Comment-Line: `Key :: Value Pair`'),
    ('first', ''),
    ('last', ''),
    ('sex', ''),
    ('age', ''),
    ('salary', ''),
    ('#3', '# Comment-Line: `Key-Value-Mapping`'),
    ('favorites', KVMap([
        ('food', ''),
        ('sport', ''),
        ('color', ''),
    ])),
    ('#4', '# Comment-Line: `Key :: Value Pair`'),
    ('registered', ''),
])

lconf_section__example_3_lconf_section_raw_str = r'''___SECTION :: EXAMPLE 3
first :: John
last :: Doe
sex :: M
age :: 39
salary :: 70000
# Comment-Line: `Key-Value-Mapping`
. favorites
   food :: Spaghetti
lconf_section__example_5b__template_obj = Root([
    # Default Empty Line
    ('#1', ''),
    # Default Comment Line
    ('#2', '# Comment-Line: `Repeated-Block-Identifier`'),
    (
        'categories',
        BlkI(
            2,
            2,
            Blk([
                # `Key-Value-Mapping: type: KVMap
                ('#3', ''),
                ('#4', '# Comment-Line: `Key-Value-Mapping`'),
                ('test1', KVMap([
                    ('name', ''),
                    ('score', -1, lconf_to_int),
                ])),
                ('test2', KVMap([
                    ('name', ''),
                    ('score', -1, lconf_to_int),
                ])),
            ]))),
])

lconf_section__example_5b_lconf_section_raw_str = r'''___SECTION :: EXAMPLE 5 b
# Repeated-Block-Identifier
* categories
   # using `Block-Names`
   PHP
      # Key-Value-Mapping
      . test1
Beispiel #14
0
example_template_no_cast = Root([
   ('key1value_pair', ''),
   ('key2value_pair', ''),
   ('key3value_pair', ''),
   ('key4value_pair', ''),
   ('key5value_pair', ''),
   ('key6value_pair', ''),
   ('key7value_pair', ''),
   ('key8value_pair', ''),
   ('key9value_pair', ''),

   # Main `Key-Value-Mapping:
   ('key10value_mapping', KVMap([
      ('mapping10_key1', ''),
      ('mapping10_key2', ''),
      ('mapping10_key3', ''),
      ('mapping10_key4', ''),
      ('mapping10_key5', ''),
      ('mapping10_key6', ''),
   ])),

   # Main `Key-Value-Mapping:
   ('key11value_mapping', KVMap([
      ('mapping11_key1', ''),
      ('mapping11_key2', ''),
   ])),

   ('key12list', KVList(True, [])),
   ('key13value_pairlist', KVList(False, [])),
   ('key14value_pairlist', KVList(False, ['1', '2', '3'])),

    ('key1value_pair', ''),
    ('key2value_pair', ''),
    ('key3value_pair', ''),
    ('key4value_pair', ''),
    ('key5value_pair', ''),
    ('key6value_pair', ''),
    ('key7value_pair', ''),
    ('key8value_pair', ''),
    ('key9value_pair', ''),

    # Main `Key-Value-Mapping:
    ('key10value_mapping',
     KVMap([
         ('mapping10_key1', ''),
         ('mapping10_key2', ''),
         ('mapping10_key3', ''),
         ('mapping10_key4', ''),
         ('mapping10_key5', ''),
         ('mapping10_key6', ''),
     ])),

    # Main `Key-Value-Mapping:
    ('key11value_mapping',
     KVMap([
         ('mapping11_key1', ''),
         ('mapping11_key2', ''),
     ])),
    ('key12list', KVList(True, [])),
    ('key13value_pairlist', KVList(False, [])),
    ('key14value_pairlist', KVList(False, ['1', '2', '3'])),

    # Repeated Mapping-Block Identifier:
from LCONF.transform import lconf_to_int


lconf_section__example_4a__template_obj = Root([
   # Default Empty Line
   ('#1', ''),
   ('registered_employees', 0, lconf_to_int),
   ('#2', '# Comment-Line: `Repeated-Block-Identifier`'),
   ('Employee', BlkI(-1, -1,
      Blk([
         ('first', ''),
         ('last', ''),
         ('sex', ''),
         ('age', ''),
         ('past_salary', KVMap([
            ('year2012', ''),
            ('year2013', -1, lconf_to_int),
         ])),
         ('emails', KVList(True, [])),
      ]),
   )),
   ('registered_customer', 0, lconf_to_int),
   ('#3', '# Comment-Line: `List-Of-Tuples`'),
   ('accounting', ListOT(('item1', 'item2', 'item3', 'item4'), [])),
])

lconf_section__example_4a_lconf_section_raw_str = r'''___SECTION :: EXAMPLE 4 a
registered_employees :: 28594
* Employee
   Person1
      first :: John
      last :: Doe