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

    # Main `Section-Template OBJ: type: Root
    lconf_section__template_obj = get_lconf_section__base_example_template_obj(
    )

    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Example1',
                                                   onelinelists=LCONF_DEFAULT,
                                                   with_comments=True)

    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=True)
    eq_(section_lines[26], '   - mapping10_key4_list :: 555,9999', msg=None)
    eq_(section_lines[104],
        '            nested_mapping_key1 :: franz',
        msg=None)
    eq_(section_lines[114],
        '                  - block-item_key3_list |name|height|weight|',
        msg=None)

    lconf_validate_one_section_str(lconf_section_raw_str)

    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)

    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Example1',
                                                   onelinelists=LCONF_NO,
                                                   with_comments=True)
    lconf_validate_one_section_str(lconf_section_raw_str)

    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Example1',
                                                   onelinelists=LCONF_YES,
                                                   with_comments=True)
    lconf_validate_one_section_str(lconf_section_raw_str)

    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Example1',
                                                   onelinelists=LCONF_DEFAULT,
                                                   with_comments=False)
    lconf_validate_one_section_str(lconf_section_raw_str)

    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Example1',
                                                   onelinelists=LCONF_NO,
                                                   with_comments=False)
    lconf_validate_one_section_str(lconf_section_raw_str)

    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Example1',
                                                   onelinelists=LCONF_YES,
                                                   with_comments=False)
    lconf_validate_one_section_str(lconf_section_raw_str)
Exemplo n.º 2
0
def test__parse_a_regular_nested_dict_to_lconf1():
   """ Tests: test__parse_a_regular_nested_dict_to_lconf1
   """
   print('::: TEST: test__parse_a_regular_nested_dict_to_lconf1()')

   nested_dict = {
      "description": "A trivial terminal emulator",
      "maintainer": "Timothy Hobbs < (at)  dot cz>",
      "last_update_time": "2014_02_12_12:59",
      "dependency": "libx11",
      "executable": "/usr/bin/xterm",
      "x11": [
         123.0,
         123
      ],
      "sound_card": False,
      "inherit_working_directory": None,
      "allow_network_access": True,
      "test_mapping": {
         "test_mapping_key1": None,
         "test_mapping_key2": 234,
         "test_mapping_key3": 'value',
         "test_mapping_key4": '',
      }
   }

   result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict, 'PERMISSIONS', onelinelists=True, skip_none_value=True)
   lconf_validate_source(result_dict_to_lconf_str)
   section_lines, section_name = lconf_section_splitlines(result_dict_to_lconf_str, validate_first_line=False)
   ok_('- x11 :: 123.0,123' in section_lines, msg=None)
   ok_('   test_mapping_key1 ::' in section_lines, msg=None)
   ok_('   test_mapping_key3 ::' in section_lines, msg=None)
   ok_('   test_mapping_key4 ::' in section_lines, msg=None)

   result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict, 'PERMISSIONS', onelinelists=False, skip_none_value=True)
   lconf_validate_source(result_dict_to_lconf_str)
   section_lines, section_name = lconf_section_splitlines(result_dict_to_lconf_str, validate_first_line=False)
   ok_('- x11' in section_lines, msg=None)
   ok_('   test_mapping_key1 ::' in section_lines, msg=None)
   ok_('   test_mapping_key3 ::' in section_lines, msg=None)
   ok_('   test_mapping_key4 ::' in section_lines, msg=None)

   result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict, 'PERMISSIONS', onelinelists=True, skip_none_value=False)
   lconf_validate_source(result_dict_to_lconf_str)
   section_lines, section_name = lconf_section_splitlines(result_dict_to_lconf_str, validate_first_line=False)
   ok_('- x11 :: 123.0,123' in section_lines, msg=None)
   ok_('   test_mapping_key1 :: None' in section_lines, msg=None)
   ok_('   test_mapping_key3 ::' in section_lines, msg=None)
   ok_('   test_mapping_key4 ::' in section_lines, msg=None)

   result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict, 'PERMISSIONS', onelinelists=False, skip_none_value=False)
   lconf_validate_source(result_dict_to_lconf_str)
   section_lines, section_name = lconf_section_splitlines(result_dict_to_lconf_str, validate_first_line=False)
   ok_('- x11' in section_lines, msg=None)
   ok_('   test_mapping_key1 :: None' in section_lines, msg=None)
   ok_('   test_mapping_key3 ::' in section_lines, msg=None)
   ok_('   test_mapping_key4 ::' in section_lines, msg=None)
def test_extract_lconf_section_splitlines_expect_failure2():
   """ Tests: test_extract_lconf_section_splitlines_expect_failure2
   """
   print('::: TEST: test_extract_lconf_section_splitlines_expect_failure2()')
   example_lconf_section_str = r'''___SECTION ::BadSection
test :: No
___END
'''
   lconf_section_splitlines(example_lconf_section_str, validate_first_line=True)
Exemplo n.º 4
0
def test_lconf_prepare_default_obj__parse_section_lines_ok0():
   """ Tests: test_lconf_prepare_default_obj__parse_section_lines_ok0
   """
   print('::: TEST: test_lconf_prepare_default_obj__parse_section_lines_ok0()')

   lconf_section__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-List`'),
      ('interests', KVList(True, [])),
      ('#4', '# Comment-Line: `Key :: Value Pair`'),
      ('registered', ''),
   ])

   lconf_section_raw_str = r'''___SECTION :: Test Example1

# Comment-Line: `Key :: Value Pair`
first :: Joe
last :: Smith
sex :: m
age :: 18
salary :: 12500
# Comment-Line
- interests
   soccer
   tennis

# Comment-Line: `Key :: Value Pair`
registered :: False
___END
'''
   lconf_validate_one_section_str(lconf_section_raw_str)
   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)

   ok_(isinstance(lconf_obj, LconfRoot), msg=None)
   eq_(lconf_obj['first'], 'Joe', msg=None)
   eq_(lconf_obj['last'], 'Smith', msg=None)
   eq_(lconf_obj['sex'], 'm', msg=None)
   eq_(lconf_obj['age'], '18', msg=None)
   eq_(lconf_obj['salary'], '12500', msg=None)

   ok_(isinstance(lconf_obj['interests'], LconfKVList), msg=None)
   eq_(lconf_obj['interests'], ['soccer', 'tennis'], msg=None)
   eq_(lconf_obj['interests'].use_oneline, True, msg=None)

   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)
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
def test_lconf_prepare_and_parse_section_lines_ok1():
    """ Tests: test_lconf_prepare_and_parse_section_lines_ok1
   """
    print('::: TEST: test_lconf_prepare_and_parse_section_lines_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_validate_one_section_str(lconf_section_raw_str)
    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=True)
    lconf_obj = lconf_prepare_and_parse_section_lines(
        section_lines,
        section_name,
        lconf_section__template_obj,
        with_comments=False)

    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)

    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)
    lconf_obj = lconf_prepare_and_parse_section_lines(
        section_lines,
        section_name,
        lconf_section__template_obj,
        with_comments=True)
def test_lconf_emit_default_obj__ok2():
    """ Tests: test_lconf_emit_default_obj__ok2
   """
    print('::: TEST: test_lconf_emit_default_obj__ok2()')

    lconf_section__template_obj = Root([
        ('first', 'Paul'),
        ('last', 'Smith'),
        ('sex', 'm'),
        ('age', '39', lconf_to_int),
        ('salary', '7000', lconf_to_float),
        ('interests', KVList(False, ['golf', 'reading', 'investments'])),
        ('registered', 'true'),
    ])
    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_DEFAULT,
                                                   with_comments=False)

    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)
    eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
    eq_(section_lines[4], 'age :: 39', msg=None)
    eq_(section_lines[6], '- interests', msg=None)
    eq_(section_lines[7], '   golf', msg=None)
    eq_(section_lines[10], 'registered :: true', msg=None)

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

   lconf_section__template_obj = Root([
      ('first', 'Paul'),
      ('last', 'Smith'),
      ('sex', 'm'),
      ('age', '39', lconf_to_int),
      ('salary', '7000', lconf_to_float),
      ('interests', KVList(False, ['golf', 'reading', 'investments'])),
      ('registered', 'true'),
   ])
   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_DEFAULT,
      with_comments=False
   )

   section_lines, section_name = lconf_section_splitlines(lconf_section_raw_str, validate_first_line=False)
   eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
   eq_(section_lines[4], 'age :: 39', msg=None)
   eq_(section_lines[6], '- interests', msg=None)
   eq_(section_lines[7], '   golf', msg=None)
   eq_(section_lines[10], 'registered :: true', msg=None)

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

   lconf_section__template_obj = Root([
      ('first', 'Paul'),
      ('#1', '# Comment-Line: `Key :: Value Pair` using an `Empty-KeyValuePair-ReplacementValue "NOT-DEFINED"'),
      ('last', 'Smith', None, 'NOT-DEFINED'),
      ('#2', '# Comment-Line: `Key :: Value Pair` using an `Empty-KeyValuePair-ReplacementValue "NOT-DEFINED"'),
      ('sex', '', None, 'NOT-DEFINED'),
      ('age', '39', lconf_to_int),
      ('#3', '# Comment-Line: `Key :: Value Pair` using an `Empty-KeyValuePair-ReplacementValue "-1"'),
      ('salary', '', lconf_to_float, -1),
      ('interests', KVList(False, ['golf', 'reading', 'investments'])),
      ('registered', 'true'),
   ])
   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_DEFAULT,
      with_comments=False
   )
   section_lines, section_name = lconf_section_splitlines(lconf_section_raw_str, validate_first_line=False)
   eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
   eq_(section_lines[1], 'first :: Paul', msg=None)
   eq_(section_lines[2], 'last :: Smith', msg=None)
   eq_(section_lines[3], 'sex ::', msg=None)
   eq_(section_lines[4], 'age :: 39', msg=None)
   eq_(section_lines[5], 'salary ::', msg=None)
   eq_(section_lines[6], '- interests', msg=None)
   eq_(section_lines[7], '   golf', msg=None)
   eq_(section_lines[10], 'registered :: true', msg=None)

   lconf_validate_one_section_str(lconf_section_raw_str)
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
def test_lconf_section_splitlines__trailing_space__expect_failure():
    """ Tests: test_lconf_section_splitlines__trailing_space__expect_failure
   """
    print(
        '::: TEST: test_lconf_section_splitlines__trailing_space__expect_failure()'
    )

    # Main `Section-Template OBJ: type: Root
    lconf_section__template_obj = Root([('#1', '# Comment-Line'),
                                        ('key1value_pair', ''),
                                        ('key2value_pair',
                                         9999.999, lconf_to_float),
                                        ('key3value_pair', '')])

    path_to_lconf_file = path_join(SCRIPT_PATH,
                                   'example_to_validate_with_err6.lconf')
    with open(path_to_lconf_file, 'r') as file_:
        lconf_section_raw_str = lconf_extract_one_section_by_name(
            file_.read(), 'Example')

    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)
    for line in section_lines:
        print(('<{}>'.format(line)))
    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)
Exemplo n.º 12
0
def test_lconf_emit_default_obj__ok1():
   """ Tests: test_lconf_emit_default_obj__ok1
   """
   print('::: TEST: test_lconf_emit_default_obj__ok1()')

   lconf_section__template_obj = Root([
      ('first', ''),
      ('last', ''),
      ('sex', ''),
      ('age', ''),
      ('salary', ''),
      ('interests', KVList(False, [])),
      ('registered', ''),
   ])
   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_DEFAULT,
      with_comments=False
   )

   section_lines, section_name = lconf_section_splitlines(lconf_section_raw_str, validate_first_line=False)
   eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
   # empty multi line list: `Key-Value-List`
   eq_(section_lines[6], '- interests', msg=None)
   eq_(section_lines[8], '___END', msg=None)

   lconf_validate_one_section_str(lconf_section_raw_str)

   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_DEFAULT,
      with_comments=True
   )
   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_NO,
      with_comments=True
   )
   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_YES,
      with_comments=True
   )
   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_NO,
      with_comments=False
   )
   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_YES,
      with_comments=False
   )
Exemplo n.º 13
0
def test_lconf_section_splitlines_expect_failure3():
   """ Tests: test_lconf_section_splitlines_expect_failure3
   """
   print('::: TEST: test_lconf_section_splitlines_expect_failure3()')

   path_to_lconf_file = path_join(SCRIPT_PATH, 'example_to_validate_with_err5.lconf')
   with open(path_to_lconf_file, 'r') as file_:
      section_lines, section_name = lconf_section_splitlines(file_.read(), validate_first_line=False)
Exemplo n.º 14
0
def test_lconf_section_splitlines_expect_failure3():
    """ Tests: test_lconf_section_splitlines_expect_failure3
   """
    print('::: TEST: test_lconf_section_splitlines_expect_failure3()')

    path_to_lconf_file = path_join(SCRIPT_PATH,
                                   'example_to_validate_with_err5.lconf')
    with open(path_to_lconf_file, 'r') as file_:
        section_lines, section_name = lconf_section_splitlines(
            file_.read(), validate_first_line=False)
Exemplo n.º 15
0
def test_lconf_emit_default_obj__ok1():
    """ Tests: test_lconf_emit_default_obj__ok1
   """
    print('::: TEST: test_lconf_emit_default_obj__ok1()')

    lconf_section__template_obj = Root([
        ('first', ''),
        ('last', ''),
        ('sex', ''),
        ('age', ''),
        ('salary', ''),
        ('interests', KVList(False, [])),
        ('registered', ''),
    ])
    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_DEFAULT,
                                                   with_comments=False)

    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)
    eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
    # empty multi line list: `Key-Value-List`
    eq_(section_lines[6], '- interests', msg=None)
    eq_(section_lines[8], '___END', msg=None)

    lconf_validate_one_section_str(lconf_section_raw_str)

    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_DEFAULT,
                                                   with_comments=True)
    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_NO,
                                                   with_comments=True)
    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_YES,
                                                   with_comments=True)
    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_NO,
                                                   with_comments=False)
    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_YES,
                                                   with_comments=False)
Exemplo n.º 16
0
def test_lconf_section_splitlines__missing_identifier__expect_failure():
    """ Tests: test_lconf_section_splitlines__missing_identifier__expect_failure
   """
    print(
        '::: TEST: test_lconf_section_splitlines__missing_identifier__expect_failure()'
    )

    lconf_section__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-List`'),
        ('interests', KVList(True, [])),
        ('#4', '# Comment-Line: `Key :: Value Pair`'),
        ('registered', ''),
    ])

    lconf_section_raw_str = r'''___SECTION :: Test Example1

# Comment-Line: `Key :: Value Pair`
first :: Joe
last :: Smith
sex :: m
age :: 18
salary :: 12500
# Comment-Line
interests
   soccer
   tennis

# Comment-Line: `Key :: Value Pair`
registered :: False
___END
'''
    lconf_validate_one_section_str(lconf_section_raw_str)
    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)
Exemplo n.º 17
0
def test_lconf_emit_default_obj__ok4():
    """ Tests: test_lconf_emit_default_obj__ok4
   """
    print('::: TEST: test_lconf_emit_default_obj__ok4()')

    lconf_section__template_obj = Root([
        ('interests', KVList(False, ['golf', 'reading', 'investments'])),
    ])
    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Example1',
                                                   onelinelists=LCONF_YES,
                                                   with_comments=False)

    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=True)
    eq_(section_lines[1], '- interests :: golf,reading,investments', msg=None)

    lconf_validate_one_section_str(lconf_section_raw_str)
Exemplo n.º 18
0
def test_lconf_emit_default_obj__ok4():
   """ Tests: test_lconf_emit_default_obj__ok4
   """
   print('::: TEST: test_lconf_emit_default_obj__ok4()')

   lconf_section__template_obj = Root([
      ('interests', KVList(False, ['golf', 'reading', 'investments'])),
   ])
   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Example1',
      onelinelists=LCONF_YES,
      with_comments=False
   )

   section_lines, section_name = lconf_section_splitlines(lconf_section_raw_str, validate_first_line=True)
   eq_(section_lines[1], '- interests :: golf,reading,investments', msg=None)

   lconf_validate_one_section_str(lconf_section_raw_str)
Exemplo n.º 19
0
def test_lconf_section_splitlines__limited_number_of_blocks__expect_failure2():
    """ Tests: test_lconf_section_splitlines__limited_number_of_blocks__expect_failure2
   """
    print(
        '::: TEST: test_lconf_section_splitlines__limited_number_of_blocks__expect_failure2()'
    )

    # Main `Section-Template OBJ: type: Root
    lconf_section__template_obj = Root([
        ('RepeatedBlk1',
         BlkI(
             0, 1,
             Blk([
                 ('#1', '# Comment-Line: below Block-Item'),
                 ('blk_key1', ''),
                 ('blk_key2', 9999.999, lconf_to_float),
             ]))), ('key', '')
    ])

    lconf_section_raw_str = r'''___SECTION :: TestExample

# test comment

* RepeatedBlk1
   BLK0
      blk_key1 :: value
      blk_key2 :: -1

   BLK1
      blk_key1 :: value
      blk_key2 :: -1

key :: value
___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)
Exemplo n.º 20
0
def test_lconf_section_splitlines__missing_identifier__expect_failure():
   """ Tests: test_lconf_section_splitlines__missing_identifier__expect_failure
   """
   print('::: TEST: test_lconf_section_splitlines__missing_identifier__expect_failure()')

   lconf_section__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-List`'),
      ('interests', KVList(True, [])),
      ('#4', '# Comment-Line: `Key :: Value Pair`'),
      ('registered', ''),
   ])

   lconf_section_raw_str = r'''___SECTION :: Test Example1

# Comment-Line: `Key :: Value Pair`
first :: Joe
last :: Smith
sex :: m
age :: 18
salary :: 12500
# Comment-Line
interests
   soccer
   tennis

# Comment-Line: `Key :: Value Pair`
registered :: False
___END
'''
   lconf_validate_one_section_str(lconf_section_raw_str)
   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)
Exemplo n.º 21
0
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)
Exemplo n.º 22
0
def test_lconf_emit_default_obj__ok3():
    """ Tests: test_lconf_emit_default_obj__ok3
   """
    print('::: TEST: test_lconf_emit_default_obj__ok3()')

    lconf_section__template_obj = Root([
        ('first', 'Paul'),
        ('#1',
         '# Comment-Line: `Key :: Value Pair` using an `Empty-KeyValuePair-ReplacementValue "NOT-DEFINED"'
         ),
        ('last', 'Smith', None, 'NOT-DEFINED'),
        ('#2',
         '# Comment-Line: `Key :: Value Pair` using an `Empty-KeyValuePair-ReplacementValue "NOT-DEFINED"'
         ),
        ('sex', '', None, 'NOT-DEFINED'),
        ('age', '39', lconf_to_int),
        ('#3',
         '# Comment-Line: `Key :: Value Pair` using an `Empty-KeyValuePair-ReplacementValue "-1"'
         ),
        ('salary', '', lconf_to_float, -1),
        ('interests', KVList(False, ['golf', 'reading', 'investments'])),
        ('registered', 'true'),
    ])
    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_DEFAULT,
                                                   with_comments=False)
    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)
    eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
    eq_(section_lines[1], 'first :: Paul', msg=None)
    eq_(section_lines[2], 'last :: Smith', msg=None)
    eq_(section_lines[3], 'sex ::', msg=None)
    eq_(section_lines[4], 'age :: 39', msg=None)
    eq_(section_lines[5], 'salary ::', msg=None)
    eq_(section_lines[6], '- interests', msg=None)
    eq_(section_lines[7], '   golf', msg=None)
    eq_(section_lines[10], 'registered :: true', msg=None)

    lconf_validate_one_section_str(lconf_section_raw_str)
Exemplo n.º 23
0
def test_lconf_section_splitlines__trailing_space__expect_failure():
   """ Tests: test_lconf_section_splitlines__trailing_space__expect_failure
   """
   print('::: TEST: test_lconf_section_splitlines__trailing_space__expect_failure()')

   # Main `Section-Template OBJ: type: Root
   lconf_section__template_obj = Root([
      ('#1', '# Comment-Line'),
      ('key1value_pair', ''),
      ('key2value_pair', 9999.999, lconf_to_float),
      ('key3value_pair', '')
   ])

   path_to_lconf_file = path_join(SCRIPT_PATH, 'example_to_validate_with_err6.lconf')
   with open(path_to_lconf_file, 'r') as file_:
      lconf_section_raw_str = lconf_extract_one_section_by_name(file_.read(), 'Example')

   section_lines, section_name = lconf_section_splitlines(lconf_section_raw_str, validate_first_line=False)
   for line in section_lines:
      print('<{}>'.format(line))
   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)
Exemplo n.º 24
0
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)
Exemplo n.º 25
0
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)
Exemplo n.º 26
0
def test_lconf_section_splitlines__limited_number_of_blocks__expect_failure2():
   """ Tests: test_lconf_section_splitlines__limited_number_of_blocks__expect_failure2
   """
   print('::: TEST: test_lconf_section_splitlines__limited_number_of_blocks__expect_failure2()')

   # Main `Section-Template OBJ: type: Root
   lconf_section__template_obj = Root([
      ('RepeatedBlk1', BlkI(0, 1,
         Blk([
            ('#1', '# Comment-Line: below Block-Item'),
            ('blk_key1', ''),
            ('blk_key2', 9999.999, lconf_to_float),
         ])
      )),
      ('key', '')
   ])

   lconf_section_raw_str = r'''___SECTION :: TestExample

# test comment

* RepeatedBlk1
   BLK0
      blk_key1 :: value
      blk_key2 :: -1

   BLK1
      blk_key1 :: value
      blk_key2 :: -1

key :: value
___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)
Exemplo n.º 27
0
def test__parse_a_regular_nested_dict_to_lconf12():
    """ Tests: test__parse_a_regular_nested_dict_to_lconf12
   """
    print('::: TEST: test__parse_a_regular_nested_dict_to_lconf12()')

    nested_dict = {
        "accounting": {
            "nested": {
                "Inner": 'something'
            },
            "nested key": None,
            "nested key2": False,
            "nested key3": '',
            "nested list1": [],
            "nested list2": [1, 2, 3],
        },
        "key": None,
        "key2": False,
        "key3": '',
        "list1": [],
        "list2": [1, 2, 3]
    }
    result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict,
                                                   'example',
                                                   onelinelists=True,
                                                   skip_none_value=True)
    lconf_validate_source(result_dict_to_lconf_str)
    section_lines, section_name = lconf_section_splitlines(
        result_dict_to_lconf_str, validate_first_line=False)
    ok_('   nested key ::' in section_lines, msg=None)
    ok_('   nested key2 :: False' in section_lines, msg=None)
    ok_('   nested key3 ::' in section_lines, msg=None)
    ok_('key ::' in section_lines, msg=None)
    ok_('key2 :: False' in section_lines, msg=None)
    ok_('key3 ::' in section_lines, msg=None)

    result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict,
                                                   'example',
                                                   onelinelists=True,
                                                   skip_none_value=False)
    lconf_validate_source(result_dict_to_lconf_str)
    section_lines, section_name = lconf_section_splitlines(
        result_dict_to_lconf_str, validate_first_line=False)
    ok_('   nested key :: None' in section_lines, msg=None)
    ok_('   nested key2 :: False' in section_lines, msg=None)
    ok_('   nested key3 ::' in section_lines, msg=None)
    ok_('key :: None' in section_lines, msg=None)
    ok_('key2 :: False' in section_lines, msg=None)
    ok_('key3 ::' in section_lines, msg=None)

    result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict,
                                                   'example',
                                                   onelinelists=False,
                                                   skip_none_value=True)
    lconf_validate_source(result_dict_to_lconf_str)
    section_lines, section_name = lconf_section_splitlines(
        result_dict_to_lconf_str, validate_first_line=False)
    ok_('   nested key ::' in section_lines, msg=None)
    ok_('   nested key2 :: False' in section_lines, msg=None)
    ok_('   nested key3 ::' in section_lines, msg=None)
    ok_('key ::' in section_lines, msg=None)
    ok_('key2 :: False' in section_lines, msg=None)
    ok_('key3 ::' in section_lines, msg=None)

    result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict,
                                                   'example',
                                                   onelinelists=False,
                                                   skip_none_value=False)
    lconf_validate_source(result_dict_to_lconf_str)
    section_lines, section_name = lconf_section_splitlines(
        result_dict_to_lconf_str, validate_first_line=False)
    ok_('   nested key :: None' in section_lines, msg=None)
    ok_('   nested key2 :: False' in section_lines, msg=None)
    ok_('   nested key3 ::' in section_lines, msg=None)
    ok_('key :: None' in section_lines, msg=None)
    ok_('key2 :: False' in section_lines, msg=None)
    ok_('key3 ::' in section_lines, msg=None)
Exemplo n.º 28
0
def test_lconf_emit_default_obj__ok0():
    """ Tests: test_lconf_emit_default_obj__ok0
   """
    print('::: TEST: test_lconf_emit_default_obj__ok0()')

    lconf_section__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-List`'),
        ('interests', KVList(True, [])),
        ('#4', '# Comment-Line: `Key :: Value Pair`'),
        ('registered', ''),
    ])
    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_DEFAULT,
                                                   with_comments=True)

    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)
    eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
    # empty comment line
    eq_(section_lines[1], '', msg=None)
    eq_(section_lines[8], '# Comment-Line: `Key-Value-List`', msg=None)
    eq_(section_lines[9], '- interests ::', msg=None)
    eq_(section_lines[12], '___END', msg=None)

    lconf_validate_one_section_str(lconf_section_raw_str)

    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_NO,
                                                   with_comments=True)
    lconf_validate_one_section_str(lconf_section_raw_str)

    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_YES,
                                                   with_comments=True)
    lconf_validate_one_section_str(lconf_section_raw_str)

    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_DEFAULT,
                                                   with_comments=False)
    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)
    eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
    # empty multi line list: `Key :: Value-List`
    eq_(section_lines[6], '- interests ::', msg=None)
    eq_(section_lines[8], '___END', msg=None)

    lconf_validate_one_section_str(lconf_section_raw_str)
    #
    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_NO,
                                                   with_comments=False)

    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)
    eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
    # empty multi line list: `Key :: Value-List`
    eq_(section_lines[6], '- interests', msg=None)
    eq_(section_lines[8], '___END', msg=None)

    lconf_validate_one_section_str(lconf_section_raw_str)

    lconf_section_raw_str = lconf_emit_default_obj(lconf_section__template_obj,
                                                   'Test Example1',
                                                   onelinelists=LCONF_YES,
                                                   with_comments=False)
    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)
    eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
    # empty multi line list: `Key :: Value-List`
    eq_(section_lines[6], '- interests ::', msg=None)
    eq_(section_lines[8], '___END', msg=None)

    lconf_validate_one_section_str(lconf_section_raw_str)
Exemplo n.º 29
0
def test_lconf_prepare_default_obj__parse_section_lines_ok3():
   """ Tests: test_lconf_prepare_default_obj__parse_section_lines_ok3
   """
   print('::: TEST: test_lconf_prepare_default_obj__parse_section_lines_ok3()')

   # 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)),

      ('#2', '# Comment-Line: below is a Main `List-Of-Tuples` with 3 columns: |a|b|c|'),
      ('list_of_tuples2', ListOT(
         ('a', 'b', 'c'),
         [('1', '2', '3'), ('44', '55', '66')],
         column_replace_missing=('-1', '-1', '-1')
      ), (lconf_to_int, lconf_to_int, lconf_to_int)),

      ('#3', '# Comment-Line: using one transform function for all'),
      ('list_of_tuples3', ListOT(
         ('a', 'b', 'c'),
         [('1.0', '2.0', '3.0'), ('44.0', '55.0', '66.0')],
         column_replace_missing=('-1.0', '-1.0', '-1.0')
      ), lconf_to_float),
   ])

   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

- list_of_tuples2 |a|b|c|
   100,,300
   400,500,600

- list_of_tuples3 |a|b|c|
   100.0,,300.0
   400.0,500.0,600.0

___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['list_of_color_tuples'], LconfListOT), msg=None)
   ok_(isinstance(lconf_obj['list_of_tuples2'], LconfListOT), msg=None)
   ok_(isinstance(lconf_obj['list_of_tuples3'], LconfListOT), msg=None)

   eq_(lconf_obj['list_of_color_tuples'][0], ('forestgreen', -1, 139, 34), msg=None)
   eq_(lconf_obj['list_of_tuples2'][0], (100, -1, 300), msg=None)
   eq_(lconf_obj['list_of_tuples3'][0], (100.0, -1.0, 300.0), 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)
Exemplo n.º 30
0
def test_lconf_emit_default_obj__ok7():
   """ Tests: test_lconf_emit_default_obj__ok7
   """
   print('::: TEST: test_lconf_emit_default_obj__ok7()')

   # Main `Section-Template OBJ: type: Root
   lconf_section__template_obj = get_lconf_section__base_example_template_obj()

   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Example1',
      onelinelists=LCONF_DEFAULT,
      with_comments=True
   )

   section_lines, section_name = lconf_section_splitlines(lconf_section_raw_str, validate_first_line=True)
   eq_(section_lines[26], '   - mapping10_key4_list :: 555,9999', msg=None)
   eq_(section_lines[104], '            nested_mapping_key1 :: franz', msg=None)
   eq_(section_lines[114], '                  - block-item_key3_list |name|height|weight|', msg=None)

   lconf_validate_one_section_str(lconf_section_raw_str)

   section_lines, section_name = lconf_section_splitlines(lconf_section_raw_str, validate_first_line=False)

   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Example1',
      onelinelists=LCONF_NO,
      with_comments=True
   )
   lconf_validate_one_section_str(lconf_section_raw_str)

   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Example1',
      onelinelists=LCONF_YES,
      with_comments=True
   )
   lconf_validate_one_section_str(lconf_section_raw_str)

   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Example1',
      onelinelists=LCONF_DEFAULT,
      with_comments=False
   )
   lconf_validate_one_section_str(lconf_section_raw_str)

   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Example1',
      onelinelists=LCONF_NO,
      with_comments=False
   )
   lconf_validate_one_section_str(lconf_section_raw_str)

   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Example1',
      onelinelists=LCONF_YES,
      with_comments=False
   )
   lconf_validate_one_section_str(lconf_section_raw_str)
Exemplo n.º 31
0
def test_lconf_prepare_default_obj__parse_section_lines_ok3():
    """ Tests: test_lconf_prepare_default_obj__parse_section_lines_ok3
   """
    print(
        '::: TEST: test_lconf_prepare_default_obj__parse_section_lines_ok3()')

    # 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)),
        ('#2',
         '# Comment-Line: below is a Main `List-Of-Tuples` with 3 columns: |a|b|c|'
         ),
        ('list_of_tuples2',
         ListOT(('a', 'b', 'c'), [('1', '2', '3'), ('44', '55', '66')],
                column_replace_missing=('-1', '-1', '-1')),
         (lconf_to_int, lconf_to_int, lconf_to_int)),
        ('#3', '# Comment-Line: using one transform function for all'),
        ('list_of_tuples3',
         ListOT(
             ('a', 'b', 'c'), [('1.0', '2.0', '3.0'),
                               ('44.0', '55.0', '66.0')],
             column_replace_missing=('-1.0', '-1.0', '-1.0')), lconf_to_float),
    ])

    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

- list_of_tuples2 |a|b|c|
   100,,300
   400,500,600

- list_of_tuples3 |a|b|c|
   100.0,,300.0
   400.0,500.0,600.0

___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['list_of_color_tuples'], LconfListOT), msg=None)
    ok_(isinstance(lconf_obj['list_of_tuples2'], LconfListOT), msg=None)
    ok_(isinstance(lconf_obj['list_of_tuples3'], LconfListOT), msg=None)

    eq_(lconf_obj['list_of_color_tuples'][0], ('forestgreen', -1, 139, 34),
        msg=None)
    eq_(lconf_obj['list_of_tuples2'][0], (100, -1, 300), msg=None)
    eq_(lconf_obj['list_of_tuples3'][0], (100.0, -1.0, 300.0), 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)
Exemplo n.º 32
0
def test_lconf_prepare_and_parse_section_lines_ok2():
    """ Tests: test_lconf_prepare_and_parse_section_lines_ok2
   """
    print('::: TEST: test_lconf_prepare_and_parse_section_lines_ok2()')

    lconf_section__template_obj = get_lconf_section__base_example_template_obj(
    )
    lconf_section_raw_str = get_lconf_section__base_example_lconf_section_raw_str(
    )

    lconf_validate_one_section_str(lconf_section_raw_str)
    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=True)
    lconf_obj = lconf_prepare_and_parse_section_lines(
        section_lines,
        section_name,
        lconf_section__template_obj,
        with_comments=False)

    ok_(isinstance(lconf_obj, LconfRoot), msg=None)
    ok_(isinstance(lconf_obj['key10value_mapping'], LconfKVMap), msg=None)
    ok_(isinstance(lconf_obj['key1value_pair'], str), msg=None)
    ok_(isinstance(lconf_obj['key10value_mapping']['mapping10_key4_list'],
                   LconfKVList),
        msg=None)
    ok_(isinstance(lconf_obj['key10value_mapping']['mapping10_key5_list'],
                   LconfKVList),
        msg=None)
    ok_(isinstance(lconf_obj['key10value_mapping']['mapping10_key6_list'],
                   LconfListOT),
        msg=None)
    ok_(isinstance(lconf_obj['key10value_mapping']['mapping10_key7_list'],
                   LconfListOT),
        msg=None)
    ok_(isinstance(lconf_obj['key11value_mapping'], LconfKVMap), msg=None)
    ok_(isinstance(lconf_obj['key11value_mapping']['mapping11_key2_mapping'],
                   LconfKVMap),
        msg=None)
    ok_(isinstance(
        lconf_obj['key11value_mapping']['mapping11_key2_mapping']
        ['mapping11_key2_nested_mapping_key2_block_identifier'], LconfBlkI),
        msg=None)
    ok_(isinstance(
        lconf_obj['key11value_mapping']['mapping11_key2_mapping']
        ['mapping11_key2_nested_mapping_key2_block_identifier']
        ['sky_blue_blk_name1'], LconfBlk),
        msg=None)
    eq_(lconf_obj['key11value_mapping']['mapping11_key2_mapping']
        ['mapping11_key2_nested_mapping_key2_block_identifier']
        ['sky_blue_blk_name1']['blk_item_green'],
        206,
        msg=None)

    eq_(lconf_obj.key_order, [
        'key1value_pair', 'key2value_pair', 'key3value_pair', 'key4value_pair',
        'key5value_pair', 'key6value_pair', 'key7value_pair', 'key8value_pair',
        'key9value_pair', 'key10value_mapping', 'key11value_mapping',
        'key12list', 'key13value_pairlist', 'key14list_of_color_tuples',
        'key15value_pairlist', 'key16value_pairlist', 'key17list_of_tuples',
        'RepeatedBlk1'
    ],
        msg=None)

    eq_(lconf_obj['key11value_mapping']['mapping11_key2_mapping']
        ['mapping11_key2_nested_mapping_key2_block_identifier']
        ['sky_blue_blk_name1'].key_order,
        ['blk_item_red', 'blk_item_green', 'blk_item_blue'],
        msg=None)

    eq_(lconf_obj['key11value_mapping']['mapping11_key1'],
        '/home/examples',
        msg=None)
    ok_(isinstance(
        lconf_obj['key11value_mapping']['mapping11_key2_mapping']
        ['mapping11_key2_nested_mapping_key1'], datetime),
        msg=None)
    eq_(lconf_obj['key1value_pair'], 'NOT-DEFINED',
        msg=None)  # `Empty-KeyValuePair-ReplacementValue` "NOT-DEFINED"
    eq_(lconf_obj['key7value_pair'], -94599.5,
        msg=None)  # `Empty-KeyValuePair-ReplacementValue` "-94599.5"

    eq_(lconf_obj['key11value_mapping']['mapping11_key2_mapping']
        ['mapping11_key2_nested_mapping_key3'],
        'car',
        msg=None)
    # `Empty-KeyValuePair-ReplacementValue` "0"
    eq_(lconf_obj['key11value_mapping']['mapping11_key2_mapping']
        ['mapping11_key2_nested_mapping_key2_block_identifier']
        ['sky_blue_blk_name1']['blk_item_red'],
        0,
        msg=None)

    ok_(isinstance(lconf_obj['key14list_of_color_tuples'], LconfListOT),
        msg=None)

    eq_(lconf_obj['key14list_of_color_tuples'].column_names,
        ('Color Name', 'Red', 'Green', 'Blue'),
        msg=None)
    eq_(lconf_obj['key14list_of_color_tuples'].column_names_idx_lookup, {
        'Color Name': 0,
        'Red': 1,
        'Green': 2,
        'Blue': 3
    },
        msg=None)

    eq_(lconf_obj['key14list_of_color_tuples'][0],
        ('forestgreen', 34, 139, 34),
        msg=None)

    ok_(isinstance(lconf_obj['RepeatedBlk1'], LconfBlkI), msg=None)

    eq_(lconf_obj['RepeatedBlk1'].key_order,
        ['BLK_OBJ1', 'BLK_OBJ2', 'BLK_OBJ3', 'BLK_OBJ4'],
        msg=None)
    eq_(lconf_obj['RepeatedBlk1'].min_required_blocks, 2, msg=None)
    eq_(lconf_obj['RepeatedBlk1'].max_allowed_blocks, 5, msg=None)
    eq_(lconf_obj['RepeatedBlk1'].has_comments, False, msg=None)

    eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ4'].key_order, [
        'MyKey1_mapping', 'MyKey2', 'MyKey3', 'MyKey4', 'MyKey5list',
        'MyKey6list', 'MyKey7list', 'MyKey8'
    ],
        msg=None)
    # `Empty-KeyValuePair-ReplacementValue` "-9999999999.055"
    eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ1']['MyKey1_mapping']
        ['blk_mapping_key2'],
        12345.99,
        msg=None)
    eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ2']['MyKey1_mapping']
        ['blk_mapping_key2'],
        -9999999999.055,
        msg=None)
    eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ3']['MyKey1_mapping']
        ['blk_mapping_key2'],
        9999.999,
        msg=None)
    eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ4']['MyKey1_mapping']
        ['blk_mapping_key2'],
        9999.999,
        msg=None)

    eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ4']['MyKey5list'], ['one item'],
        msg=None)
    eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ4']['MyKey4'],
        'GREAT LIFE',
        msg=None)

    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)
    lconf_obj = lconf_prepare_and_parse_section_lines(
        section_lines,
        section_name,
        lconf_section__template_obj,
        with_comments=True)
    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)
    lconf_obj = lconf_prepare_and_parse_section_lines(
        section_lines,
        section_name,
        lconf_section__template_obj,
        with_comments=False)
    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=True)
    lconf_obj = lconf_prepare_and_parse_section_lines(
        section_lines,
        section_name,
        lconf_section__template_obj,
        with_comments=True)
Exemplo n.º 33
0
def test_lconf_prepare_default_obj__parse_section_lines__base_example_ok():
   """ Tests: test_lconf_prepare_default_obj__parse_section_lines__base_example_ok
   """
   print('::: TEST: test_lconf_prepare_default_obj__parse_section_lines__base_example_ok()')

   lconf_section__template_obj = get_lconf_section__base_example_template_obj()

   lconf_section_raw_str = get_lconf_section__base_example_lconf_section_raw_str()

   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['key10value_mapping'], LconfKVMap), msg=None)
   ok_(isinstance(lconf_obj['key1value_pair'], str), msg=None)
   ok_(isinstance(lconf_obj['key10value_mapping']['mapping10_key4_list'], LconfKVList), msg=None)
   ok_(isinstance(lconf_obj['key10value_mapping']['mapping10_key5_list'], LconfKVList), msg=None)
   ok_(isinstance(lconf_obj['key10value_mapping']['mapping10_key6_list'], LconfListOT), msg=None)
   ok_(isinstance(lconf_obj['key10value_mapping']['mapping10_key7_list'], LconfListOT), msg=None)
   ok_(isinstance(lconf_obj['key11value_mapping'], LconfKVMap), msg=None)
   ok_(isinstance(lconf_obj['key11value_mapping']['mapping11_key2_mapping'], LconfKVMap), msg=None)
   ok_(isinstance(
      lconf_obj['key11value_mapping']['mapping11_key2_mapping']['mapping11_key2_nested_mapping_key2_block_identifier'],
      LconfBlkI),
      msg=None)
   ok_(isinstance(
      lconf_obj['key11value_mapping']['mapping11_key2_mapping']['mapping11_key2_nested_mapping_key2_block_identifier'][
         'sky_blue_blk_name1'],
      LconfBlk),
      msg=None)
   eq_(lconf_obj['key11value_mapping']['mapping11_key2_mapping']['mapping11_key2_nested_mapping_key2_block_identifier'][
      'sky_blue_blk_name1']['blk_item_green'],
      206,
      msg=None)

   eq_(lconf_obj.key_order,
      ['key1value_pair', 'key2value_pair', 'key3value_pair', 'key4value_pair', 'key5value_pair', 'key6value_pair',
         'key7value_pair', 'key8value_pair', 'key9value_pair', 'key10value_mapping', 'key11value_mapping', 'key12list',
         'key13value_pairlist', 'key14list_of_color_tuples', 'key15value_pairlist', 'key16value_pairlist',
         'key17list_of_tuples', 'RepeatedBlk1'],
      msg=None)

   eq_(lconf_obj['key11value_mapping']['mapping11_key2_mapping']['mapping11_key2_nested_mapping_key2_block_identifier'][
      'sky_blue_blk_name1'].key_order,
      ['blk_item_red', 'blk_item_green', 'blk_item_blue'],
      msg=None)

   eq_(lconf_obj['key11value_mapping']['mapping11_key1'], '/home/examples', msg=None)
   ok_(isinstance(
      lconf_obj['key11value_mapping']['mapping11_key2_mapping']['mapping11_key2_nested_mapping_key1'],
      datetime),
      msg=None
   )
   eq_(lconf_obj['key1value_pair'], 'NOT-DEFINED', msg=None)  # `Empty-KeyValuePair-ReplacementValue` "NOT-DEFINED"
   eq_(lconf_obj['key7value_pair'], -94599.5, msg=None)  # `Empty-KeyValuePair-ReplacementValue` "-94599.5"

   eq_(lconf_obj['key11value_mapping']['mapping11_key2_mapping']['mapping11_key2_nested_mapping_key3'], 'car', msg=None)
   # `Empty-KeyValuePair-ReplacementValue` "0"
   eq_(lconf_obj['key11value_mapping']['mapping11_key2_mapping']['mapping11_key2_nested_mapping_key2_block_identifier'][
      'sky_blue_blk_name1']['blk_item_red'], 0, msg=None)

   ok_(isinstance(lconf_obj['key14list_of_color_tuples'], LconfListOT), msg=None)

   eq_(lconf_obj['key14list_of_color_tuples'].column_names, ('Color Name', 'Red', 'Green', 'Blue'), msg=None)
   eq_(
      lconf_obj['key14list_of_color_tuples'].column_names_idx_lookup,
      {'Color Name': 0, 'Red': 1, 'Green': 2, 'Blue': 3},
      msg=None
   )

   eq_(lconf_obj['key14list_of_color_tuples'][0], ('forestgreen', 34, 139, 34), msg=None)

   ok_(isinstance(lconf_obj['RepeatedBlk1'], LconfBlkI), msg=None)

   eq_(lconf_obj['RepeatedBlk1'].key_order, ['BLK_OBJ1', 'BLK_OBJ2', 'BLK_OBJ3', 'BLK_OBJ4'], msg=None)
   eq_(lconf_obj['RepeatedBlk1'].min_required_blocks, 2, msg=None)
   eq_(lconf_obj['RepeatedBlk1'].max_allowed_blocks, 5, msg=None)
   eq_(lconf_obj['RepeatedBlk1'].has_comments, False, msg=None)

   eq_(
      lconf_obj['RepeatedBlk1']['BLK_OBJ4'].key_order,
      ['MyKey1_mapping', 'MyKey2', 'MyKey3', 'MyKey4', 'MyKey5list', 'MyKey6list', 'MyKey7list', 'MyKey8'],
      msg=None
   )
   # `Empty-KeyValuePair-ReplacementValue` "-9999999999.055"
   eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ1']['MyKey1_mapping']['blk_mapping_key2'], 12345.99, msg=None)
   eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ2']['MyKey1_mapping']['blk_mapping_key2'], -9999999999.055, msg=None)
   eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ3']['MyKey1_mapping']['blk_mapping_key2'], 9999.999, msg=None)
   eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ4']['MyKey1_mapping']['blk_mapping_key2'], 9999.999, msg=None)

   eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ4']['MyKey5list'], ['one item'], msg=None)
   eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ4']['MyKey4'], 'GREAT LIFE', 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)
Exemplo n.º 34
0
def test_extract_lconf_section_splitlines1():
   """ Tests: extract LCONF Section and lconf_section_splitlines validate_first_line
   """
   print('::: TEST: test_extract_lconf_section_splitlines1()')
   example_lconf_section_str = r'''

any info text outside of a section



___SECTION :: Example

# Comment-Line: below: Main `Key :: Value Pair`
key1value_pair :: value1
# Comment-Line: below is a `Key :: Value Pair` with an empty value string: which is skipped
key2value_pair ::
key3value_pair ::
key4value_pair :: True
key5value_pair :: False
key6value_pair :: None
key7value_pair :: 1456.984
key8value_pair :: true
key9value_pair :: different characters # \n * | , & @  https://translate.google.com/ translate ਅਨੁਵਾਦ  翻訳する μεταφράζω


# Comment-Line: below is a Main `Key-Value-Mapping`
. key10value_mapping
   # Comment-Line:  Key-Value-Mapping items: are `Key :: Value Pairs`
   mapping10_key1 :: False
   mapping10_key2 :: true
   mapping10_key3 :: 123456

   # Comment-Line:  Key-Value-Mapping item: `Key :: Value-List`
   - mapping10_key4_list :: 1,2

   # Comment-Line:  Key-Value-Mapping item: `Key-Value-List`
   - mapping10_key5_list
      1
      2

   # Comment-Line:  Key-Value-Mapping item: `List-Of-Tuples`
   - mapping10_key6_list |x|y|
      1,3
      2,6

   # Comment-Line:  Key-Value-Mapping item: `List-Of-Tuples`
   - mapping10_key7_list |a|b|c|
      1,2.0,3
      2,4.0,6
___END

empty section next
___SECTION :: EMPTY_EXAMPLE
___END


empty section next
___SECTION :: MYSection
test :: No
___END
'''
   all_sections = lconf_extract_all_sections(example_lconf_section_str)
   for section in all_sections:
      lconf_section_splitlines(section, validate_first_line=True)
Exemplo n.º 35
0
def test__parse_a_regular_nested_dict_to_lconf12():
   """ Tests: test__parse_a_regular_nested_dict_to_lconf12
   """
   print('::: TEST: test__parse_a_regular_nested_dict_to_lconf12()')

   nested_dict = {
      "accounting": {
         "nested": {
            "Inner": 'something'
         },
         "nested key": None,
         "nested key2": False,
         "nested key3": '',
         "nested list1": [],
         "nested list2": [1, 2, 3],
      },
      "key": None,
      "key2": False,
      "key3": '',
      "list1": [],
      "list2": [1, 2, 3]
   }
   result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict, 'example', onelinelists=True, skip_none_value=True)
   lconf_validate_source(result_dict_to_lconf_str)
   section_lines, section_name = lconf_section_splitlines(result_dict_to_lconf_str, validate_first_line=False)
   ok_('   nested key ::' in section_lines, msg=None)
   ok_('   nested key2 :: False' in section_lines, msg=None)
   ok_('   nested key3 ::' in section_lines, msg=None)
   ok_('key ::' in section_lines, msg=None)
   ok_('key2 :: False' in section_lines, msg=None)
   ok_('key3 ::' in section_lines, msg=None)

   result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict, 'example', onelinelists=True, skip_none_value=False)
   lconf_validate_source(result_dict_to_lconf_str)
   section_lines, section_name = lconf_section_splitlines(result_dict_to_lconf_str, validate_first_line=False)
   ok_('   nested key :: None' in section_lines, msg=None)
   ok_('   nested key2 :: False' in section_lines, msg=None)
   ok_('   nested key3 ::' in section_lines, msg=None)
   ok_('key :: None' in section_lines, msg=None)
   ok_('key2 :: False' in section_lines, msg=None)
   ok_('key3 ::' in section_lines, msg=None)

   result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict, 'example', onelinelists=False, skip_none_value=True)
   lconf_validate_source(result_dict_to_lconf_str)
   section_lines, section_name = lconf_section_splitlines(result_dict_to_lconf_str, validate_first_line=False)
   ok_('   nested key ::' in section_lines, msg=None)
   ok_('   nested key2 :: False' in section_lines, msg=None)
   ok_('   nested key3 ::' in section_lines, msg=None)
   ok_('key ::' in section_lines, msg=None)
   ok_('key2 :: False' in section_lines, msg=None)
   ok_('key3 ::' in section_lines, msg=None)

   result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict, 'example', onelinelists=False, skip_none_value=False)
   lconf_validate_source(result_dict_to_lconf_str)
   section_lines, section_name = lconf_section_splitlines(result_dict_to_lconf_str, validate_first_line=False)
   ok_('   nested key :: None' in section_lines, msg=None)
   ok_('   nested key2 :: False' in section_lines, msg=None)
   ok_('   nested key3 ::' in section_lines, msg=None)
   ok_('key :: None' in section_lines, msg=None)
   ok_('key2 :: False' in section_lines, msg=None)
   ok_('key3 ::' in section_lines, msg=None)
Exemplo n.º 36
0
def test_lconf_emit_default_obj__ok0():
   """ Tests: test_lconf_emit_default_obj__ok0
   """
   print('::: TEST: test_lconf_emit_default_obj__ok0()')

   lconf_section__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-List`'),
      ('interests', KVList(True, [])),
      ('#4', '# Comment-Line: `Key :: Value Pair`'),
      ('registered', ''),
   ])
   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_DEFAULT,
      with_comments=True
   )

   section_lines, section_name = lconf_section_splitlines(lconf_section_raw_str, validate_first_line=False)
   eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
   # empty comment line
   eq_(section_lines[1], '', msg=None)
   eq_(section_lines[8], '# Comment-Line: `Key-Value-List`', msg=None)
   eq_(section_lines[9], '- interests ::', msg=None)
   eq_(section_lines[12], '___END', msg=None)

   lconf_validate_one_section_str(lconf_section_raw_str)

   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_NO,
      with_comments=True
   )
   lconf_validate_one_section_str(lconf_section_raw_str)

   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_YES,
      with_comments=True
   )
   lconf_validate_one_section_str(lconf_section_raw_str)

   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_DEFAULT,
      with_comments=False
   )
   section_lines, section_name = lconf_section_splitlines(lconf_section_raw_str, validate_first_line=False)
   eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
   # empty multi line list: `Key :: Value-List`
   eq_(section_lines[6], '- interests ::', msg=None)
   eq_(section_lines[8], '___END', msg=None)

   lconf_validate_one_section_str(lconf_section_raw_str)
   #
   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_NO,
      with_comments=False
   )

   section_lines, section_name = lconf_section_splitlines(lconf_section_raw_str, validate_first_line=False)
   eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
   # empty multi line list: `Key :: Value-List`
   eq_(section_lines[6], '- interests', msg=None)
   eq_(section_lines[8], '___END', msg=None)

   lconf_validate_one_section_str(lconf_section_raw_str)

   lconf_section_raw_str = lconf_emit_default_obj(
      lconf_section__template_obj,
      'Test Example1',
      onelinelists=LCONF_YES,
      with_comments=False
   )
   section_lines, section_name = lconf_section_splitlines(lconf_section_raw_str, validate_first_line=False)
   eq_(section_lines[0], '___SECTION :: Test Example1', msg=None)
   # empty multi line list: `Key :: Value-List`
   eq_(section_lines[6], '- interests ::', msg=None)
   eq_(section_lines[8], '___END', msg=None)

   lconf_validate_one_section_str(lconf_section_raw_str)
Exemplo n.º 37
0
def test_lconf_prepare_and_parse_section_lines_ok0():
    """ Tests: test_lconf_prepare_and_parse_section_lines_ok0
   """
    print('::: TEST: test_lconf_prepare_and_parse_section_lines_ok0()')

    lconf_section__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-List`'),
        ('interests', KVList(True, [])),
        ('#4', '# Comment-Line: `Key :: Value Pair`'),
        ('registered', ''),
    ])

    lconf_section_raw_str = r'''___SECTION :: Test Example1

# Comment-Line: `Key :: Value Pair`
first :: Joe
last :: Smith
sex :: m
age :: 18
salary :: 12500
# Comment-Line
- interests
   soccer
   tennis

# Comment-Line: `Key :: Value Pair`
registered :: False
___END
'''
    lconf_validate_one_section_str(lconf_section_raw_str)
    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=False)
    lconf_obj = lconf_prepare_and_parse_section_lines(
        section_lines,
        section_name,
        lconf_section__template_obj,
        with_comments=True)

    ok_(isinstance(lconf_obj, LconfRoot), msg=None)
    eq_(lconf_obj['first'], 'Joe', msg=None)
    eq_(lconf_obj['last'], 'Smith', msg=None)
    eq_(lconf_obj['sex'], 'm', msg=None)
    eq_(lconf_obj['age'], '18', msg=None)
    eq_(lconf_obj['salary'], '12500', msg=None)

    ok_(isinstance(lconf_obj['interests'], LconfKVList), msg=None)
    eq_(lconf_obj['interests'], ['soccer', 'tennis'], msg=None)
    eq_(lconf_obj['interests'].use_oneline, True, msg=None)

    section_lines, section_name = lconf_section_splitlines(
        lconf_section_raw_str, validate_first_line=True)
    lconf_obj = lconf_prepare_and_parse_section_lines(
        section_lines,
        section_name,
        lconf_section__template_obj,
        with_comments=False)
Exemplo n.º 38
0
def test__parse_a_regular_nested_dict_to_lconf1():
    """ Tests: test__parse_a_regular_nested_dict_to_lconf1
   """
    print('::: TEST: test__parse_a_regular_nested_dict_to_lconf1()')

    nested_dict = {
        "description": "A trivial terminal emulator",
        "maintainer": "Timothy Hobbs < (at)  dot cz>",
        "last_update_time": "2014_02_12_12:59",
        "dependency": "libx11",
        "executable": "/usr/bin/xterm",
        "x11": [123.0, 123],
        "sound_card": False,
        "inherit_working_directory": None,
        "allow_network_access": True,
        "test_mapping": {
            "test_mapping_key1": None,
            "test_mapping_key2": 234,
            "test_mapping_key3": 'value',
            "test_mapping_key4": '',
        }
    }

    result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict,
                                                   'PERMISSIONS',
                                                   onelinelists=True,
                                                   skip_none_value=True)
    lconf_validate_source(result_dict_to_lconf_str)
    section_lines, section_name = lconf_section_splitlines(
        result_dict_to_lconf_str, validate_first_line=False)
    ok_('- x11 :: 123.0,123' in section_lines, msg=None)
    ok_('   test_mapping_key1 ::' in section_lines, msg=None)
    ok_('   test_mapping_key3 ::' in section_lines, msg=None)
    ok_('   test_mapping_key4 ::' in section_lines, msg=None)

    result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict,
                                                   'PERMISSIONS',
                                                   onelinelists=False,
                                                   skip_none_value=True)
    lconf_validate_source(result_dict_to_lconf_str)
    section_lines, section_name = lconf_section_splitlines(
        result_dict_to_lconf_str, validate_first_line=False)
    ok_('- x11' in section_lines, msg=None)
    ok_('   test_mapping_key1 ::' in section_lines, msg=None)
    ok_('   test_mapping_key3 ::' in section_lines, msg=None)
    ok_('   test_mapping_key4 ::' in section_lines, msg=None)

    result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict,
                                                   'PERMISSIONS',
                                                   onelinelists=True,
                                                   skip_none_value=False)
    lconf_validate_source(result_dict_to_lconf_str)
    section_lines, section_name = lconf_section_splitlines(
        result_dict_to_lconf_str, validate_first_line=False)
    ok_('- x11 :: 123.0,123' in section_lines, msg=None)
    ok_('   test_mapping_key1 :: None' in section_lines, msg=None)
    ok_('   test_mapping_key3 ::' in section_lines, msg=None)
    ok_('   test_mapping_key4 ::' in section_lines, msg=None)

    result_dict_to_lconf_str = lconf_dict_to_lconf(nested_dict,
                                                   'PERMISSIONS',
                                                   onelinelists=False,
                                                   skip_none_value=False)
    lconf_validate_source(result_dict_to_lconf_str)
    section_lines, section_name = lconf_section_splitlines(
        result_dict_to_lconf_str, validate_first_line=False)
    ok_('- x11' in section_lines, msg=None)
    ok_('   test_mapping_key1 :: None' in section_lines, msg=None)
    ok_('   test_mapping_key3 ::' in section_lines, msg=None)
    ok_('   test_mapping_key4 ::' in section_lines, msg=None)