def test_lconf_emit_ok8(): """ Tests: test_lconf_emit_ok8 """ print('::: TEST: test_lconf_emit_ok8()') 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_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=False, validate=True) emit_result = lconf_emit(lconf_obj, onelinelists=LCONF_YES, empty_key_value_pair=True) reparsed_lconf_obj = lconf_parse_section_extract_by_name( emit_result, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=False, validate=True) re_emit_result = lconf_emit(reparsed_lconf_obj, onelinelists=LCONF_YES, empty_key_value_pair=True) eq_(emit_result, re_emit_result, msg=None)
def test_lconf_emit_ok8(): """ Tests: test_lconf_emit_ok8 """ print('::: TEST: test_lconf_emit_ok8()') 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_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=False, validate=True ) emit_result = lconf_emit(lconf_obj, onelinelists=LCONF_YES, empty_key_value_pair=True) reparsed_lconf_obj = lconf_parse_section_extract_by_name( emit_result, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=False, validate=True ) re_emit_result = lconf_emit(reparsed_lconf_obj, onelinelists=LCONF_YES, empty_key_value_pair=True) eq_(emit_result, re_emit_result, msg=None)
def test_lconf_emit_ok0(): """ Tests: test_lconf_emit_ok0 """ print('::: TEST: test_lconf_emit_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: `Key-Value-List` - interests :: soccer,tennis # Comment-Line: `Key :: Value Pair` registered :: False ___END''' lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Test Example1', lconf_section__template_obj, with_comments=True, validate=True) emit_result = lconf_emit(lconf_obj, onelinelists=LCONF_DEFAULT, empty_key_value_pair=True) eq_(lconf_section_raw_str, emit_result, msg=None) reparsed_lconf_obj = lconf_parse_section_extract_by_name( emit_result, 'Test Example1', lconf_section__template_obj, with_comments=True, validate=True) eq_(lconf_obj, reparsed_lconf_obj, msg=None) re_emit_result = lconf_emit(reparsed_lconf_obj, onelinelists=LCONF_DEFAULT, empty_key_value_pair=True) eq_(lconf_section_raw_str, re_emit_result, msg=None)
def test_lconf_emit_ok0(): """ Tests: test_lconf_emit_ok0 """ print('::: TEST: test_lconf_emit_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: `Key-Value-List` - interests :: soccer,tennis # Comment-Line: `Key :: Value Pair` registered :: False ___END''' lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Test Example1', lconf_section__template_obj, with_comments=True, validate=True ) emit_result = lconf_emit(lconf_obj, onelinelists=LCONF_DEFAULT, empty_key_value_pair=True) eq_(lconf_section_raw_str, emit_result, msg=None) reparsed_lconf_obj = lconf_parse_section_extract_by_name( emit_result, 'Test Example1', lconf_section__template_obj, with_comments=True, validate=True ) eq_(lconf_obj, reparsed_lconf_obj, msg=None) re_emit_result = lconf_emit(reparsed_lconf_obj, onelinelists=LCONF_DEFAULT, empty_key_value_pair=True) eq_(lconf_section_raw_str, re_emit_result, msg=None)
def test_lconf_parse_section_extract_by_name_ok1(): """ Tests: test_lconf_parse_section_extract_by_name_ok1 """ print('::: TEST: test_lconf_parse_section_extract_by_name_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_parse_section_extract_by_name( lconf_section_raw_str, 'Test Example1', 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_parse_section_extract_by_name_expect_failure1(): """ Tests: test_lconf_parse_section_extract_by_name_expect_failure1 """ print('::: TEST: test_lconf_parse_section_extract_by_name_expect_failure1()') 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_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Wrong Section Name', lconf_section__template_obj, with_comments=True, validate=True )
def test_lconf_parse_section_extract_by_name_expect_failure1(): """ Tests: test_lconf_parse_section_extract_by_name_expect_failure1 """ print( '::: TEST: test_lconf_parse_section_extract_by_name_expect_failure1()') 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_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Wrong Section Name', lconf_section__template_obj, with_comments=True, validate=True)
def test_lconf_parse_section_extract_by_name_expect_failure2(): """ Tests: test_lconf_parse_section_extract_by_name_expect_failure2 """ print('::: TEST: test_lconf_parse_section_extract_by_name_expect_failure2()') lconf_section__template_obj = Root([ ('#1', ''), ('first', ''), ]) lconf_section_raw_str = r'''___SECTION :: Test Example1 first :: Joe ___END ''' lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Wrong Section Name', lconf_section__template_obj, with_comments=False, validate=True )
def test_lconf_parse_section_extract_by_name_expect_failure2(): """ Tests: test_lconf_parse_section_extract_by_name_expect_failure2 """ print( '::: TEST: test_lconf_parse_section_extract_by_name_expect_failure2()') lconf_section__template_obj = Root([ ('#1', ''), ('first', ''), ]) lconf_section_raw_str = r'''___SECTION :: Test Example1 first :: Joe ___END ''' lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Wrong Section Name', lconf_section__template_obj, with_comments=False, validate=True)
def test_lconf_parse_section_extract_by_name_ok1(): """ Tests: test_lconf_parse_section_extract_by_name_ok1 """ print('::: TEST: test_lconf_parse_section_extract_by_name_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_parse_section_extract_by_name( lconf_section_raw_str, 'Test Example1', 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_parse_section_extract_by_name_base_example_ok(): """ Tests: test_lconf_parse_section_extract_by_name_base_example_ok """ print('::: TEST: test_lconf_parse_section_extract_by_name_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_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=False, validate=True ) ok_(isinstance(lconf_obj, LconfRoot), msg=None) ok_(isinstance(lconf_obj['key10value_mapping'], LconfKVMap), 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['key11value_mapping']['mapping11_key2_mapping']['mapping11_key2_nested_mapping_key3'], 'car', 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 ) eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ4']['MyKey5list'], ['one item'], msg=None) eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ4']['MyKey4'], 'GREAT LIFE', msg=None) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=True, validate=False ) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=False, validate=False ) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=True, validate=True )
def test_lconf_emit_ok9(): """ Tests: test_lconf_emit_ok9 """ print('::: TEST: test_lconf_emit_ok9()') # 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'), []), (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') ]), (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') ]), lconf_to_float), ('#4', '# Comment-Line: using one transform function for all'), ('list_of_tuples4', ListOT(('a', 'b', 'c'), [ ('1.0', '2.0', '3.0'), ('44.0', '55.0', '66.0') ])), ('#5', '# Comment-Line: using one transform function for all'), ('list_of_tuples5', 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'))), ('#6', '# Comment-Line: using one transform function for all'), ('list_of_tuples6', 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_to_float, lconf_to_float)), ]) lconf_section_raw_str = r'''___SECTION :: TestExample # Comment-Line: below is a Main `List-Of-Tuples` with 4 columns: |Color Name|Red|Green|Blue| - list_of_color_tuples |Color Name|Red|Green|Blue| forestgreen,34,139,34 brick,156,102,31 # Comment-Line: below is a Main `List-Of-Tuples` with 3 columns: |a|b|c| - list_of_tuples2 |a|b|c| 100,200,300 400,500,600 # Comment-Line: using one transform function for all - list_of_tuples3 |a|b|c| 100.0,200.0,300.0 400.0,500.0,600.0 # Comment-Line: using one transform function for all - list_of_tuples4 |a|b|c| 100.0,200.0,300.0 400.0,500.0,600.0 # Comment-Line: using one transform function for all - list_of_tuples5 |a|b|c| 100.0,,300.0 400.0,500.0,600.0 # Comment-Line: using one transform function for all - list_of_tuples6 |a|b|c| 100.0,,300.0 400.0,500.0,600.0 ___END''' lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'TestExample', lconf_section__template_obj, with_comments=True, validate=True ) emit_result = lconf_emit(lconf_obj, onelinelists=LCONF_DEFAULT) reparsed_lconf_obj = lconf_parse_section_extract_by_name( emit_result, 'TestExample', lconf_section__template_obj, with_comments=True, validate=True ) eq_(lconf_obj, reparsed_lconf_obj, msg=None) re_emit_result = lconf_emit(reparsed_lconf_obj, onelinelists=LCONF_DEFAULT, empty_key_value_pair=True) eq_(emit_result, re_emit_result, msg=None) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'TestExample', lconf_section__template_obj, with_comments=True, validate=True ) emit_result = lconf_emit(lconf_obj, onelinelists=LCONF_NO, empty_key_value_pair=True) reparsed_lconf_obj = lconf_parse_section_extract_by_name( emit_result, 'TestExample', lconf_section__template_obj, with_comments=True, validate=True ) eq_(lconf_obj, reparsed_lconf_obj, msg=None) re_emit_result = lconf_emit(reparsed_lconf_obj, onelinelists=LCONF_NO, empty_key_value_pair=True) eq_(emit_result, re_emit_result, msg=None) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'TestExample', lconf_section__template_obj, with_comments=True, validate=True ) emit_result = lconf_emit(lconf_obj, onelinelists=LCONF_YES, empty_key_value_pair=True) reparsed_lconf_obj = lconf_parse_section_extract_by_name( emit_result, 'TestExample', lconf_section__template_obj, with_comments=True, validate=True ) eq_(lconf_obj, reparsed_lconf_obj, msg=None) re_emit_result = lconf_emit(reparsed_lconf_obj, onelinelists=LCONF_YES, empty_key_value_pair=True) eq_(emit_result, re_emit_result, msg=None)
def test_lconf_to_native_type_ok2(): """ Tests: test_lconf_to_native_type_ok2 """ print('::: TEST: test_lconf_to_native_type_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_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=False, validate=True) 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) 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) 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) 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) result_native_type = lconf_to_native_type(lconf_obj) ok_(isinstance(lconf_obj, LconfRoot), msg=None) ok_(isinstance(result_native_type, dict), msg=None) ok_(lconf_obj['key1value_pair'] == result_native_type['key1value_pair'] == 'NOT-DEFINED', msg=None) ok_(lconf_obj['key7value_pair'] == result_native_type['key7value_pair'] == -94599.5, msg=None) # RE DUMP AS JSON: NOTE SPECIAL characters might not be handled correctly # IMPORTANT: datetime.datetime(2014, 5, 8, 13, 39) is not JSON serializable result_native_type['key11value_mapping']['mapping11_key2_mapping'][ 'mapping11_key2_nested_mapping_key1'] = '2014-05-08 13:39:00' re_dump_json = json_dumps(result_native_type, indent=3) # RE CONVERT TO LCONF - NOTE: there will not be any Block-Identifier also the order might be messed up which does not work # with comment lines result_reconverted_dict_to_lconf2 = lconf_dict_to_lconf( json_loads(re_dump_json), 'BaseEXAMPLE', onelinelists=False, skip_none_value=False ) lconf_validate_one_section_str(result_reconverted_dict_to_lconf2) # do yaml of result_native_type if has_yaml: dump_yaml = yaml_dump(result_native_type, indent=3, allow_unicode=True) parsed_load_yaml = yaml_load(dump_yaml) ok_(isinstance(parsed_load_yaml, dict), msg=None) eq_( parsed_load_yaml['key11value_mapping']['mapping11_key2_mapping']['mapping11_key2_nested_mapping_key1'], '2014-05-08 13:39:00' ) eq_(parsed_load_yaml['key11value_mapping']['mapping11_key2_mapping'][ 'mapping11_key2_nested_mapping_key2_block_identifier']['sky_blue_blk_name1']['blk_item_green'], 206, msg=None) eq_(parsed_load_yaml['key11value_mapping']['mapping11_key1'], '/home/examples', msg=None) eq_(parsed_load_yaml['key1value_pair'], 'NOT-DEFINED', msg=None) # `Empty-KeyValuePair-ReplacementValue` "NOT-DEFINED" eq_(parsed_load_yaml['key7value_pair'], -94599.5, msg=None) # `Empty-KeyValuePair-ReplacementValue` "-94599.5" eq_(parsed_load_yaml['key11value_mapping']['mapping11_key2_mapping']['mapping11_key2_nested_mapping_key3'], 'car', msg=None) # `Empty-KeyValuePair-ReplacementValue` "0" eq_(parsed_load_yaml['key11value_mapping']['mapping11_key2_mapping'][ 'mapping11_key2_nested_mapping_key2_block_identifier']['sky_blue_blk_name1']['blk_item_red'], 0, msg=None) # Tuple are changed to lists eq_(parsed_load_yaml['key14list_of_color_tuples'][0], ['forestgreen', 34, 139, 34], msg=None) # `Empty-KeyValuePair-ReplacementValue` "-9999999999.055" eq_(parsed_load_yaml['RepeatedBlk1']['BLK_OBJ1']['MyKey1_mapping']['blk_mapping_key2'], 12345.99, msg=None) eq_(parsed_load_yaml['RepeatedBlk1']['BLK_OBJ2']['MyKey1_mapping']['blk_mapping_key2'], -9999999999.055, msg=None) eq_(parsed_load_yaml['RepeatedBlk1']['BLK_OBJ3']['MyKey1_mapping']['blk_mapping_key2'], 9999.999, msg=None) eq_(parsed_load_yaml['RepeatedBlk1']['BLK_OBJ4']['MyKey1_mapping']['blk_mapping_key2'], 9999.999, msg=None) eq_(parsed_load_yaml['RepeatedBlk1']['BLK_OBJ4']['MyKey5list'], ['one item'], msg=None) eq_(parsed_load_yaml['RepeatedBlk1']['BLK_OBJ4']['MyKey4'], 'GREAT LIFE', msg=None)
def test_lconf_emit_ok9(): """ Tests: test_lconf_emit_ok9 """ print('::: TEST: test_lconf_emit_ok9()') # 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'), []), (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')]), (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')]), lconf_to_float), ('#4', '# Comment-Line: using one transform function for all'), ('list_of_tuples4', ListOT(('a', 'b', 'c'), [('1.0', '2.0', '3.0'), ('44.0', '55.0', '66.0')])), ('#5', '# Comment-Line: using one transform function for all'), ('list_of_tuples5', 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'))), ('#6', '# Comment-Line: using one transform function for all'), ('list_of_tuples6', 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_to_float, lconf_to_float)), ]) lconf_section_raw_str = r'''___SECTION :: TestExample # Comment-Line: below is a Main `List-Of-Tuples` with 4 columns: |Color Name|Red|Green|Blue| - list_of_color_tuples |Color Name|Red|Green|Blue| forestgreen,34,139,34 brick,156,102,31 # Comment-Line: below is a Main `List-Of-Tuples` with 3 columns: |a|b|c| - list_of_tuples2 |a|b|c| 100,200,300 400,500,600 # Comment-Line: using one transform function for all - list_of_tuples3 |a|b|c| 100.0,200.0,300.0 400.0,500.0,600.0 # Comment-Line: using one transform function for all - list_of_tuples4 |a|b|c| 100.0,200.0,300.0 400.0,500.0,600.0 # Comment-Line: using one transform function for all - list_of_tuples5 |a|b|c| 100.0,,300.0 400.0,500.0,600.0 # Comment-Line: using one transform function for all - list_of_tuples6 |a|b|c| 100.0,,300.0 400.0,500.0,600.0 ___END''' lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'TestExample', lconf_section__template_obj, with_comments=True, validate=True) emit_result = lconf_emit(lconf_obj, onelinelists=LCONF_DEFAULT) reparsed_lconf_obj = lconf_parse_section_extract_by_name( emit_result, 'TestExample', lconf_section__template_obj, with_comments=True, validate=True) eq_(lconf_obj, reparsed_lconf_obj, msg=None) re_emit_result = lconf_emit(reparsed_lconf_obj, onelinelists=LCONF_DEFAULT, empty_key_value_pair=True) eq_(emit_result, re_emit_result, msg=None) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'TestExample', lconf_section__template_obj, with_comments=True, validate=True) emit_result = lconf_emit(lconf_obj, onelinelists=LCONF_NO, empty_key_value_pair=True) reparsed_lconf_obj = lconf_parse_section_extract_by_name( emit_result, 'TestExample', lconf_section__template_obj, with_comments=True, validate=True) eq_(lconf_obj, reparsed_lconf_obj, msg=None) re_emit_result = lconf_emit(reparsed_lconf_obj, onelinelists=LCONF_NO, empty_key_value_pair=True) eq_(emit_result, re_emit_result, msg=None) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'TestExample', lconf_section__template_obj, with_comments=True, validate=True) emit_result = lconf_emit(lconf_obj, onelinelists=LCONF_YES, empty_key_value_pair=True) reparsed_lconf_obj = lconf_parse_section_extract_by_name( emit_result, 'TestExample', lconf_section__template_obj, with_comments=True, validate=True) eq_(lconf_obj, reparsed_lconf_obj, msg=None) re_emit_result = lconf_emit(reparsed_lconf_obj, onelinelists=LCONF_YES, empty_key_value_pair=True) eq_(emit_result, re_emit_result, msg=None)
def test_lconf_parse_section_extract_by_name_ok0(): """ Tests: test_lconf_parse_section_extract_by_name_ok0 """ print('::: TEST: test_lconf_parse_section_extract_by_name_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''' Some text outside of any sections ___SECTION :: EmptySection ___END Some text outside of any sections ___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 more text ___SECTION :: EmptySection2 ___END ''' lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Test Example1', lconf_section__template_obj, with_comments=True, validate=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) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Test Example1', lconf_section__template_obj, with_comments=False, validate=False) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Test Example1', lconf_section__template_obj, with_comments=True, validate=False) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Test Example1', lconf_section__template_obj, with_comments=False, validate=True)
def test_lconf_parse_section_extract_by_name_base_example_ok(): """ Tests: test_lconf_parse_section_extract_by_name_base_example_ok """ print( '::: TEST: test_lconf_parse_section_extract_by_name_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_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=False, validate=True) ok_(isinstance(lconf_obj, LconfRoot), msg=None) ok_(isinstance(lconf_obj['key10value_mapping'], LconfKVMap), 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['key11value_mapping']['mapping11_key2_mapping'] ['mapping11_key2_nested_mapping_key3'], 'car', 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) eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ4']['MyKey5list'], ['one item'], msg=None) eq_(lconf_obj['RepeatedBlk1']['BLK_OBJ4']['MyKey4'], 'GREAT LIFE', msg=None) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=True, validate=False) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=False, validate=False) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=True, validate=True)
def test_lconf_parse_section_extract_by_name_ok0(): """ Tests: test_lconf_parse_section_extract_by_name_ok0 """ print('::: TEST: test_lconf_parse_section_extract_by_name_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''' Some text outside of any sections ___SECTION :: EmptySection ___END Some text outside of any sections ___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 more text ___SECTION :: EmptySection2 ___END ''' lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Test Example1', lconf_section__template_obj, with_comments=True, validate=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) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Test Example1', lconf_section__template_obj, with_comments=False, validate=False ) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Test Example1', lconf_section__template_obj, with_comments=True, validate=False ) lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'Test Example1', lconf_section__template_obj, with_comments=False, validate=True )
def main(): pass # EXAMPLE: ONLY VALIDATE lconf_validate_one_section_str( lconf_section__base_example_lconf_section_raw_str) # EXAMPLE: ONLY PREPARE DEFAULT OBJ lconf_default_obj = lconf_prepare_default_obj( lconf_section__base_example_template_obj, with_comments=False) print( '\n\n============== EXAMPLE: ONLY PREPARE DEFAULT OBJ ==============\n' ) print(lconf_default_obj) # EXAMPLE: VALIDATE, PREPARE, PARSE: # validate a `LCONF-Section string` and prepare a default lconf obj from the template obj and parse the LCONF-Section print( '\n\n============== EXAMPLE: VALIDATE, PREPARE, PARSE ==============\n' ) lconf_parse_obj = lconf_prepare_and_parse_section( lconf_section__base_example_lconf_section_raw_str, lconf_section__base_example_template_obj, with_comments=True, validate=True) print(lconf_parse_obj) # EXAMPLE: EXTRACT KNOWN SECTION, VALIDATE, PREPARE, PARSE: this is also useful to extract from files lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__base_example_lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__base_example_template_obj, with_comments=True, validate=True) print( '\n\n============== EXAMPLE: EXTRACT KNOWN SECTION, VALIDATE, PREPARE, PARSE: also for files ==============\n' ) print(lconf_parse_obj) # EXAMPLE: ACCESS The Section-INFO print( '\n\n============== EXAMPLE: ACCESS The Section-INFO ==============\n') print((' lconf_parse_obj.key_order: ', lconf_parse_obj.key_order)) print((' lconf_parse_obj.key_empty_replacementvalue: ', lconf_parse_obj.key_empty_replacementvalue)) print((' lconf_parse_obj.section_name: ', lconf_parse_obj.section_name)) print((' lconf_parse_obj.is_parsed: ', lconf_parse_obj.is_parsed)) print((' lconf_parse_obj.has_comments: ', lconf_parse_obj.has_comments)) # EXAMPLE: EMIT DEFAULT OBJ lconf_section_emitted_default_obj_str = lconf_emit_default_obj( lconf_section__base_example_template_obj, 'EMITTED BaseEXAMPLE', onelinelists=LCONF_DEFAULT, with_comments=True) print('\n\n============== EXAMPLE: EMIT DEFAULT OBJ (==============\n') print(lconf_section_emitted_default_obj_str) # EXAMPLE: EMIT PARSED LCONF OBJ lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__base_example_lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__base_example_template_obj, with_comments=True, validate=True) lconf_section_emitted_parsed_obj_str = lconf_emit( lconf_parse_obj, onelinelists=LCONF_DEFAULT, empty_key_value_pair=True) print( '\n\n============== EXAMPLE: EMIT PARSED LCONF OBJ (empty_key_value_pair=True) ==============\n' ) print(lconf_section_emitted_parsed_obj_str) lconf_section_emitted_parsed_obj_str = lconf_emit( lconf_parse_obj, onelinelists=LCONF_DEFAULT, empty_key_value_pair=False) print( '\n\n============== EXAMPLE: EMIT PARSED LCONF OBJ (empty_key_value_pair=False) ==============\n' ) print(lconf_section_emitted_parsed_obj_str) # EXAMPLE: EMIT TO JSON lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__base_example_lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__base_example_template_obj, with_comments=False, validate=True) result_ordered_native_type = lconf_to_ordered_native_type(lconf_parse_obj) # IMPORTANT: datetime.datetime(2014, 5, 8, 13, 39) is not JSON serializable result_ordered_native_type['key11value_mapping']['mapping11_key2_mapping'][ 'mapping11_key2_nested_mapping_key1'] = '2014-05-08 13:39:00' dump_json = json_dumps(result_ordered_native_type, indent=3) print('\n\n============== EXAMPLE: EMIT TO ORDERED JSON ==============\n') print(dump_json) # EXAMPLE: EMIT TO YAML if has_yaml: lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__base_example_lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__base_example_template_obj, with_comments=False, validate=True) result_native_type = lconf_to_native_type(lconf_parse_obj) # IMPORTANT: datetime.datetime(2014, 5, 8, 13, 39) is not JSON serializable result_native_type['key11value_mapping']['mapping11_key2_mapping'][ 'mapping11_key2_nested_mapping_key1'] = '2014-05-08 13:39:00' dump_yaml = yaml_dump(result_native_type, indent=3, allow_unicode=True) print('\n\n============== EXAMPLE: EMIT TO YAML ==============\n') print(dump_yaml)
def main(): # ---------------------------------- EXAMPLE 3 ---------------------------------- # # EXAMPLE 3: ONLY VALIDATE lconf_validate_one_section_str( lconf_section__example_3_lconf_section_raw_str) # EXAMPLE 3: ONLY PREPARE DEFAULT OBJ lconf_default_obj = lconf_prepare_default_obj( lconf_section__example_3__template_obj, with_comments=False) print( '\n\n============== EXAMPLE 3: ONLY PREPARE DEFAULT OBJ ==============\n' ) print(lconf_default_obj) # EXAMPLE 3: VALIDATE, PREPARE, PARSE: # validate a `LCONF-Section string` and prepare a default lconf obj from the template obj and parse the LCONF-Section print( '\n\n============== EXAMPLE 3: VALIDATE, PREPARE, PARSE ==============\n' ) lconf_parse_obj = lconf_prepare_and_parse_section( lconf_section__example_3_lconf_section_raw_str, lconf_section__example_3__template_obj, with_comments=True, validate=True) print(lconf_parse_obj) # EXAMPLE 3: EXTRACT KNOWN SECTION, VALIDATE, PREPARE, PARSE: this is also useful to extract from files lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__example_3_lconf_section_raw_str, 'EXAMPLE 3', lconf_section__example_3__template_obj, with_comments=True, validate=True) print( '\n\n============== EXAMPLE 3: EXTRACT KNOWN SECTION, VALIDATE, PREPARE, PARSE: this is also useful to extract from files ==============\n' ) print(lconf_parse_obj) # EXAMPLE 3: ACCESS The Section-INFO print( '\n\n============== EXAMPLE 3: ACCESS The Section-INFO ==============\n' ) print((' lconf_parse_obj.key_order: ', lconf_parse_obj.key_order)) print((' lconf_parse_obj.section_name: ', lconf_parse_obj.section_name)) print((' lconf_parse_obj.is_parsed: ', lconf_parse_obj.is_parsed)) print((' lconf_parse_obj.has_comments: ', lconf_parse_obj.has_comments)) # EXAMPLE 3: EMIT DEFAULT OBJ lconf_section_emitted_default_obj_str = lconf_emit_default_obj( lconf_section__example_3__template_obj, 'EMITTED EXAMPLE 3', onelinelists=LCONF_DEFAULT, with_comments=True) print('\n\n============== EXAMPLE 3: EMIT DEFAULT OBJ ==============\n') print(lconf_section_emitted_default_obj_str) # EXAMPLE: EMIT PARSED LCONF OBJ lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__example_3_lconf_section_raw_str, 'EXAMPLE 3', lconf_section__example_3__template_obj, with_comments=True, validate=True) lconf_section_emitted_parsed_obj_str = lconf_emit( lconf_parse_obj, onelinelists=LCONF_DEFAULT, empty_key_value_pair=True) print( '\n\n============== EXAMPLE 3: EMIT PARSED LCONF OBJ ==============\n') print(lconf_section_emitted_parsed_obj_str) # EXAMPLE 3: EMIT TO JSON lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__example_3_lconf_section_raw_str, 'EXAMPLE 3', lconf_section__example_3__template_obj, with_comments=False, validate=True) result_ordered_native_type = lconf_to_ordered_native_type(lconf_parse_obj) dump_json = json_dumps(result_ordered_native_type, indent=3) print( '\n\n============== EXAMPLE 3: EMIT TO ORDERED JSON ==============\n') print(dump_json) # EXAMPLE: EMIT TO YAML if has_yaml: lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__example_3_lconf_section_raw_str, 'EXAMPLE 3', lconf_section__example_3__template_obj, with_comments=False, validate=True) result_native_type = lconf_to_native_type(lconf_parse_obj) dump_yaml = yaml_dump(result_native_type, indent=3, allow_unicode=True) print('\n\n============== EXAMPLE 3: EMIT TO YAML ==============\n') print(dump_yaml)
def test_lconf_to_ordered_native_type_ok0(): """ Tests: test_lconf_to_ordered_native_type_ok0 """ print('::: TEST: test_lconf_to_ordered_native_type_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 :: EXAMPLE 1 #1 :: #2 :: # Comment-Line: `Key :: Value Pair` first :: Joe last :: Smith sex :: m age :: 18 salary :: 12500 #3 :: # Comment-Line: `Key-Value-List` - interests soccer tennis #4 :: # Comment-Line: `Key :: Value Pair` registered :: False ___END''' lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'EXAMPLE 1', lconf_section__template_obj, with_comments=True, validate=True ) result_ordered_native_type = lconf_to_ordered_native_type(lconf_obj) ok_(isinstance(lconf_obj, LconfRoot), msg=None) ok_(isinstance(result_ordered_native_type, OrderedDict), msg=None) ok_(isinstance(lconf_obj['interests'], LconfKVList), msg=None) ok_(isinstance(result_ordered_native_type['interests'], list), msg=None) # RE DUMP AS JSON re_dump_json = json_dumps(result_ordered_native_type, indent=3) # RE CONVERT TO LCONF result_reconverted_dict_to_lconf2 = lconf_dict_to_lconf( json_loads(re_dump_json, object_pairs_hook=OrderedDict), 'EXAMPLE 1', onelinelists=False, skip_none_value=False ) eq_(lconf_section_raw_str, result_reconverted_dict_to_lconf2, msg=None)
def test_lconf_to_native_type_ok0(): """ Tests: test_lconf_to_native_type_ok0 """ print('::: TEST: test_lconf_to_native_type_ok0()') lconf_section__template_obj = Root([ # Default Empty Line ('#1', ''), # Default Comment Line ('#2', '# Comment-Line: `Key :: Value Pair`'), ('first', ''), ('last', '', None, 'NOT-DEFINED'), ('sex', '', None, 'NOT-DEFINED'), ('age', '', lconf_to_int, -1), ('salary', ''), ('#3', '# Comment-Line: `Key-Value-List`'), ('interests', KVList(True, [])), ('#4', '# Comment-Line: `Key :: Value Pair`'), ('registered', ''), ]) lconf_section_raw_str = r'''___SECTION :: EXAMPLE 1 #1 :: #2 :: # Comment-Line: `Key :: Value Pair` first :: Joe last :: Smith sex :: m age :: 18 salary :: 12500 #3 :: # Comment-Line: `Key-Value-List` - interests soccer tennis #4 :: # Comment-Line: `Key :: Value Pair` registered :: False ___END''' lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'EXAMPLE 1', lconf_section__template_obj, with_comments=False, validate=True ) result_native_type = lconf_to_native_type(lconf_obj) ok_(isinstance(lconf_obj, LconfRoot), msg=None) ok_(isinstance(result_native_type, dict), msg=None) ok_(isinstance(lconf_obj['interests'], LconfKVList), msg=None) ok_(isinstance(result_native_type['interests'], list), msg=None) ok_(lconf_obj['last'] == result_native_type['last'] == 'Smith', msg=None) ok_(lconf_obj['age'] == result_native_type['age'] == 18, msg=None) # RE DUMP AS JSON re_dump_json = json_dumps(result_native_type, indent=3) # RE CONVERT TO LCONF result_reconverted_dict_to_lconf2 = lconf_dict_to_lconf( json_loads(re_dump_json), 'EXAMPLE 1', onelinelists=False, skip_none_value=False ) lconf_validate_one_section_str(result_reconverted_dict_to_lconf2) if has_yaml: dump_yaml = yaml_dump(result_native_type, indent=3, allow_unicode=True) parsed_load_yaml = yaml_load(dump_yaml) ok_(isinstance(parsed_load_yaml, dict), msg=None) ok_(isinstance(parsed_load_yaml['interests'], list), msg=None) ok_(parsed_load_yaml['last'] == lconf_obj['last'] == result_native_type['last'] == 'Smith', msg=None) ok_(parsed_load_yaml['age'] == lconf_obj['age'] == result_native_type['age'] == 18, msg=None)
def main(): pass # EXAMPLE: ONLY VALIDATE lconf_validate_one_section_str(lconf_section__base_example_lconf_section_raw_str) # EXAMPLE: ONLY PREPARE DEFAULT OBJ lconf_default_obj = lconf_prepare_default_obj(lconf_section__base_example_template_obj, with_comments=False) print('\n\n============== EXAMPLE: ONLY PREPARE DEFAULT OBJ ==============\n') print(lconf_default_obj) # EXAMPLE: VALIDATE, PREPARE, PARSE: # validate a `LCONF-Section string` and prepare a default lconf obj from the template obj and parse the LCONF-Section print('\n\n============== EXAMPLE: VALIDATE, PREPARE, PARSE ==============\n') lconf_parse_obj = lconf_prepare_and_parse_section( lconf_section__base_example_lconf_section_raw_str, lconf_section__base_example_template_obj, with_comments=True, validate=True ) print(lconf_parse_obj) # EXAMPLE: EXTRACT KNOWN SECTION, VALIDATE, PREPARE, PARSE: this is also useful to extract from files lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__base_example_lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__base_example_template_obj, with_comments=True, validate=True ) print( '\n\n============== EXAMPLE: EXTRACT KNOWN SECTION, VALIDATE, PREPARE, PARSE: also for files ==============\n') print(lconf_parse_obj) # EXAMPLE: ACCESS The Section-INFO print('\n\n============== EXAMPLE: ACCESS The Section-INFO ==============\n') print(' lconf_parse_obj.key_order: ', lconf_parse_obj.key_order) print(' lconf_parse_obj.key_empty_replacementvalue: ', lconf_parse_obj.key_empty_replacementvalue) print(' lconf_parse_obj.section_name: ', lconf_parse_obj.section_name) print(' lconf_parse_obj.is_parsed: ', lconf_parse_obj.is_parsed) print(' lconf_parse_obj.has_comments: ', lconf_parse_obj.has_comments) # EXAMPLE: EMIT DEFAULT OBJ lconf_section_emitted_default_obj_str = lconf_emit_default_obj( lconf_section__base_example_template_obj, 'EMITTED BaseEXAMPLE', onelinelists=LCONF_DEFAULT, with_comments=True ) print('\n\n============== EXAMPLE: EMIT DEFAULT OBJ (==============\n') print(lconf_section_emitted_default_obj_str) # EXAMPLE: EMIT PARSED LCONF OBJ lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__base_example_lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__base_example_template_obj, with_comments=True, validate=True ) lconf_section_emitted_parsed_obj_str = lconf_emit(lconf_parse_obj, onelinelists=LCONF_DEFAULT, empty_key_value_pair=True) print('\n\n============== EXAMPLE: EMIT PARSED LCONF OBJ (empty_key_value_pair=True) ==============\n') print(lconf_section_emitted_parsed_obj_str) lconf_section_emitted_parsed_obj_str = lconf_emit(lconf_parse_obj, onelinelists=LCONF_DEFAULT, empty_key_value_pair=False) print('\n\n============== EXAMPLE: EMIT PARSED LCONF OBJ (empty_key_value_pair=False) ==============\n') print(lconf_section_emitted_parsed_obj_str) # EXAMPLE: EMIT TO JSON lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__base_example_lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__base_example_template_obj, with_comments=False, validate=True ) result_ordered_native_type = lconf_to_ordered_native_type(lconf_parse_obj) # IMPORTANT: datetime.datetime(2014, 5, 8, 13, 39) is not JSON serializable result_ordered_native_type['key11value_mapping']['mapping11_key2_mapping'][ 'mapping11_key2_nested_mapping_key1'] = '2014-05-08 13:39:00' dump_json = json_dumps(result_ordered_native_type, indent=3) print('\n\n============== EXAMPLE: EMIT TO ORDERED JSON ==============\n') print(dump_json) # EXAMPLE: EMIT TO YAML if has_yaml: lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__base_example_lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__base_example_template_obj, with_comments=False, validate=True ) result_native_type = lconf_to_native_type(lconf_parse_obj) # IMPORTANT: datetime.datetime(2014, 5, 8, 13, 39) is not JSON serializable result_native_type['key11value_mapping']['mapping11_key2_mapping'][ 'mapping11_key2_nested_mapping_key1'] = '2014-05-08 13:39:00' dump_yaml = yaml_dump(result_native_type, indent=3, allow_unicode=True) print('\n\n============== EXAMPLE: EMIT TO YAML ==============\n') print(dump_yaml)
def test_lconf_to_ordered_native_type_ok1(): """ Tests: test_lconf_to_ordered_native_type_ok1 """ print('::: TEST: test_lconf_to_ordered_native_type_ok1()') 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_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=False, validate=True) result_ordered_native_type = lconf_to_ordered_native_type(lconf_obj) ok_(isinstance(lconf_obj, LconfRoot), msg=None) ok_(isinstance(result_ordered_native_type, OrderedDict), msg=None) # RE DUMP AS JSON: NOTE SPECIAL characters might not be handled correctly # IMPORTANT: datetime.datetime(2014, 5, 8, 13, 39) is not JSON serializable result_ordered_native_type['key11value_mapping']['mapping11_key2_mapping'][ 'mapping11_key2_nested_mapping_key1'] = '2014-05-08 13:39:00' re_dump_json = json_dumps(result_ordered_native_type, indent=3) re_dump_json_lines = re_dump_json.splitlines() eq_(re_dump_json_lines[1], ' "key1value_pair": "NOT-DEFINED",', msg=None) eq_(re_dump_json_lines[3], ' "key3value_pair": "",', msg=None) eq_(re_dump_json_lines[10], ' "key10value_mapping": {', msg=None) eq_(re_dump_json_lines[32], ' "mapping10_key7_list": [', msg=None) eq_(re_dump_json_lines[35], ' 2.0,', msg=None) eq_(re_dump_json_lines[72], ' "key14list_of_color_tuples": [', msg=None) eq_(re_dump_json_lines[80], ' "brick",', msg=None) eq_(re_dump_json_lines[96], ' "nested_mapping_key1": "franz",', msg=None) eq_(re_dump_json_lines[103], ' "block-item_key1": 12345.99,', msg=None) eq_(re_dump_json_lines[144], ' "blk_mapping_key2": -9999999999.055,', msg=None) eq_(re_dump_json_lines[224], ' "Nested Repeated Block Identifier": {}', msg=None) # RE CONVERT TO LCONF - NOTE: there will not be any Block-Identifier result_reconverted_dict_to_lconf2 = lconf_dict_to_lconf( json_loads(re_dump_json, object_pairs_hook=OrderedDict), 'EXAMPLE 1', onelinelists=False, skip_none_value=False ) # NOTE: there will not be any Block-Identifier result_reconverted_dict_to_lconf2_lines = result_reconverted_dict_to_lconf2.splitlines() eq_(result_reconverted_dict_to_lconf2_lines[4], 'key4value_pair :: True', msg=None) eq_(result_reconverted_dict_to_lconf2_lines[9], r'key9value_pair :: different characters # \n * | , & @ https://translate.google.com/ translate ਅਨੁਵਾਦ 翻訳する μεταφράζω', msg=None) eq_(result_reconverted_dict_to_lconf2_lines[23], ' - mapping10_key7_list |item1|item2|item3|', msg=None) eq_(result_reconverted_dict_to_lconf2_lines[31], ' . sky_blue_blk_name1', msg=None) eq_(result_reconverted_dict_to_lconf2_lines[70], ' - block-item_key3_list |item1|item2|item3|', msg=None) eq_(result_reconverted_dict_to_lconf2_lines[104], ' - block-item_key3_list', msg=None) # REDO AGAIN redo__lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=False, validate=True) redo__result_ordered_native_type = lconf_to_ordered_native_type(redo__lconf_obj) redo__dump_json = json_dumps(result_ordered_native_type, indent=3) eq_(redo__dump_json, re_dump_json, msg=None) redo__result_reconverted_dict_to_lconf2 = lconf_dict_to_lconf( json_loads(re_dump_json, object_pairs_hook=OrderedDict), 'EXAMPLE 1', onelinelists=False, skip_none_value=False ) eq_(redo__result_reconverted_dict_to_lconf2, result_reconverted_dict_to_lconf2, msg=None)
def test_lconf_to_native_type_ok2(): """ Tests: test_lconf_to_native_type_ok2 """ print('::: TEST: test_lconf_to_native_type_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_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'BaseEXAMPLE', lconf_section__template_obj, with_comments=False, validate=True) 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) 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) 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) 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) result_native_type = lconf_to_native_type(lconf_obj) ok_(isinstance(lconf_obj, LconfRoot), msg=None) ok_(isinstance(result_native_type, dict), msg=None) ok_(lconf_obj['key1value_pair'] == result_native_type['key1value_pair'] == 'NOT-DEFINED', msg=None) ok_(lconf_obj['key7value_pair'] == result_native_type['key7value_pair'] == -94599.5, msg=None) # RE DUMP AS JSON: NOTE SPECIAL characters might not be handled correctly # IMPORTANT: datetime.datetime(2014, 5, 8, 13, 39) is not JSON serializable result_native_type['key11value_mapping']['mapping11_key2_mapping'][ 'mapping11_key2_nested_mapping_key1'] = '2014-05-08 13:39:00' re_dump_json = json_dumps(result_native_type, indent=3) # RE CONVERT TO LCONF - NOTE: there will not be any Block-Identifier also the order might be messed up which does not work # with comment lines result_reconverted_dict_to_lconf2 = lconf_dict_to_lconf( json_loads(re_dump_json), 'BaseEXAMPLE', onelinelists=False, skip_none_value=False) lconf_validate_one_section_str(result_reconverted_dict_to_lconf2) # do yaml of result_native_type if has_yaml: dump_yaml = yaml_dump(result_native_type, indent=3, allow_unicode=True) parsed_load_yaml = yaml_load(dump_yaml) ok_(isinstance(parsed_load_yaml, dict), msg=None) eq_( parsed_load_yaml['key11value_mapping']['mapping11_key2_mapping'] ['mapping11_key2_nested_mapping_key1'], '2014-05-08 13:39:00') eq_(parsed_load_yaml['key11value_mapping']['mapping11_key2_mapping'] ['mapping11_key2_nested_mapping_key2_block_identifier'] ['sky_blue_blk_name1']['blk_item_green'], 206, msg=None) eq_(parsed_load_yaml['key11value_mapping']['mapping11_key1'], '/home/examples', msg=None) eq_(parsed_load_yaml['key1value_pair'], 'NOT-DEFINED', msg=None) # `Empty-KeyValuePair-ReplacementValue` "NOT-DEFINED" eq_(parsed_load_yaml['key7value_pair'], -94599.5, msg=None) # `Empty-KeyValuePair-ReplacementValue` "-94599.5" eq_(parsed_load_yaml['key11value_mapping']['mapping11_key2_mapping'] ['mapping11_key2_nested_mapping_key3'], 'car', msg=None) # `Empty-KeyValuePair-ReplacementValue` "0" eq_(parsed_load_yaml['key11value_mapping']['mapping11_key2_mapping'] ['mapping11_key2_nested_mapping_key2_block_identifier'] ['sky_blue_blk_name1']['blk_item_red'], 0, msg=None) # Tuple are changed to lists eq_(parsed_load_yaml['key14list_of_color_tuples'][0], ['forestgreen', 34, 139, 34], msg=None) # `Empty-KeyValuePair-ReplacementValue` "-9999999999.055" eq_(parsed_load_yaml['RepeatedBlk1']['BLK_OBJ1']['MyKey1_mapping'] ['blk_mapping_key2'], 12345.99, msg=None) eq_(parsed_load_yaml['RepeatedBlk1']['BLK_OBJ2']['MyKey1_mapping'] ['blk_mapping_key2'], -9999999999.055, msg=None) eq_(parsed_load_yaml['RepeatedBlk1']['BLK_OBJ3']['MyKey1_mapping'] ['blk_mapping_key2'], 9999.999, msg=None) eq_(parsed_load_yaml['RepeatedBlk1']['BLK_OBJ4']['MyKey1_mapping'] ['blk_mapping_key2'], 9999.999, msg=None) eq_(parsed_load_yaml['RepeatedBlk1']['BLK_OBJ4']['MyKey5list'], ['one item'], msg=None) eq_(parsed_load_yaml['RepeatedBlk1']['BLK_OBJ4']['MyKey4'], 'GREAT LIFE', msg=None)
def main(): # ---------------------------------- EXAMPLE 4 a ---------------------------------- # # EXAMPLE 4 a: ONLY VALIDATE lconf_validate_one_section_str(lconf_section__example_4a_lconf_section_raw_str) # EXAMPLE 4 a: ONLY PREPARE DEFAULT OBJ lconf_default_obj = lconf_prepare_default_obj(lconf_section__example_4a__template_obj, with_comments=False) print('\n\n============== EXAMPLE 4 a: ONLY PREPARE DEFAULT OBJ ==============\n') print(lconf_default_obj) # EXAMPLE 4 a: VALIDATE, PREPARE, PARSE: # validate a `LCONF-Section string` and prepare a default lconf obj from the template obj and parse the LCONF-Section print('\n\n============== EXAMPLE 4 a: VALIDATE, PREPARE, PARSE ==============\n') lconf_parse_obj = lconf_prepare_and_parse_section( lconf_section__example_4a_lconf_section_raw_str, lconf_section__example_4a__template_obj, with_comments=True, validate=True ) print(lconf_parse_obj) # EXAMPLE 4 a: EXTRACT KNOWN SECTION, VALIDATE, PREPARE, PARSE: this is also useful to extract from files lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__example_4a_lconf_section_raw_str, 'EXAMPLE 4 a', lconf_section__example_4a__template_obj, with_comments=True, validate=True ) print( '\n\n============== EXAMPLE 4 a: EXTRACT KNOWN SECTION, VALIDATE, PREPARE, PARSE: also for files ==============\n') print(lconf_parse_obj) # EXAMPLE 4 a: ACCESS The Section-INFO print('\n\n============== EXAMPLE 4 a: ACCESS The Section-INFO ==============\n') print(' lconf_parse_obj.key_order: ', lconf_parse_obj.key_order) print(' lconf_parse_obj.section_name: ', lconf_parse_obj.section_name) print(' lconf_parse_obj.is_parsed: ', lconf_parse_obj.is_parsed) print(' lconf_parse_obj.has_comments: ', lconf_parse_obj.has_comments) # EXAMPLE 4 a: EMIT DEFAULT OBJ lconf_section_emitted_default_obj_str = lconf_emit_default_obj( lconf_section__example_4a__template_obj, 'EMITTED EXAMPLE 4 a', onelinelists=LCONF_DEFAULT, with_comments=True ) print('\n\n============== EXAMPLE 4 a: EMIT DEFAULT OBJ ==============\n') print(lconf_section_emitted_default_obj_str) # EXAMPLE: EMIT PARSED LCONF OBJ lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__example_4a_lconf_section_raw_str, 'EXAMPLE 4 a', lconf_section__example_4a__template_obj, with_comments=True, validate=True ) lconf_section_emitted_parsed_obj_str = lconf_emit(lconf_parse_obj, onelinelists=LCONF_DEFAULT) print('\n\n============== EXAMPLE 4 a: EMIT PARSED LCONF OBJ ==============\n') print(lconf_section_emitted_parsed_obj_str) # EXAMPLE 4 a: EMIT TO JSON lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__example_4a_lconf_section_raw_str, 'EXAMPLE 4 a', lconf_section__example_4a__template_obj, with_comments=False, validate=True ) result_ordered_native_type = lconf_to_ordered_native_type(lconf_parse_obj) dump_json = json_dumps(result_ordered_native_type, indent=3) print('\n\n============== EXAMPLE 4 a: EMIT TO ORDERED JSON ==============\n') print(dump_json) # EXAMPLE: EMIT TO YAML if has_yaml: lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__example_4a_lconf_section_raw_str, 'EXAMPLE 4 a', lconf_section__example_4a__template_obj, with_comments=False, validate=True ) result_native_type = lconf_to_native_type(lconf_parse_obj) dump_yaml = yaml_dump(result_native_type, indent=3, allow_unicode=True) print('\n\n============== EXAMPLE 4 a: EMIT TO YAML ==============\n') print(dump_yaml) # ---------------------------------- EXAMPLE 4 b ---------------------------------- # # EXAMPLE 4 b: ONLY VALIDATE lconf_validate_one_section_str(lconf_section__example_4b_lconf_section_raw_str) # EXAMPLE 4 b: ONLY PREPARE DEFAULT OBJ lconf_default_obj = lconf_prepare_default_obj(lconf_section__example_4b__template_obj, with_comments=False) print('\n\n============== EXAMPLE 4 b: ONLY PREPARE DEFAULT OBJ ==============\n') print(lconf_default_obj) # EXAMPLE 4 b: VALIDATE, PREPARE, PARSE: # validate a `LCONF-Section string` and prepare a default lconf obj from the template obj and parse the LCONF-Section print('\n\n============== EXAMPLE 4 b: VALIDATE, PREPARE, PARSE ==============\n') lconf_parse_obj = lconf_prepare_and_parse_section( lconf_section__example_4b_lconf_section_raw_str, lconf_section__example_4b__template_obj, with_comments=True, validate=True ) print(lconf_parse_obj) # EXAMPLE 4 b: EXTRACT KNOWN SECTION, VALIDATE, PREPARE, PARSE: this is also useful to extract from files lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__example_4b_lconf_section_raw_str, 'EXAMPLE 4 b', lconf_section__example_4b__template_obj, with_comments=True, validate=True ) print( '\n\n============== EXAMPLE 4 b: EXTRACT KNOWN SECTION, VALIDATE, PREPARE, PARSE: also for files ==============\n') print(lconf_parse_obj) # EXAMPLE 4 b: ACCESS The Section-INFO print('\n\n============== EXAMPLE 4 b: ACCESS The Section-INFO ==============\n') print(' lconf_parse_obj.key_order: ', lconf_parse_obj.key_order) print(' lconf_parse_obj.section_name: ', lconf_parse_obj.section_name) print(' lconf_parse_obj.is_parsed: ', lconf_parse_obj.is_parsed) print(' lconf_parse_obj.has_comments: ', lconf_parse_obj.has_comments) # EXAMPLE 4 b: EMIT DEFAULT OBJ lconf_section_emitted_default_obj_str = lconf_emit_default_obj( lconf_section__example_4b__template_obj, 'EMITTED EXAMPLE 4 b', onelinelists=LCONF_DEFAULT, with_comments=True ) print('\n\n============== EXAMPLE 4 b: EMIT DEFAULT OBJ ==============\n') print(lconf_section_emitted_default_obj_str) # EXAMPLE: EMIT PARSED LCONF OBJ lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__example_4b_lconf_section_raw_str, 'EXAMPLE 4 b', lconf_section__example_4b__template_obj, with_comments=True, validate=True ) lconf_section_emitted_parsed_obj_str = lconf_emit(lconf_parse_obj, onelinelists=LCONF_DEFAULT) print('\n\n============== EXAMPLE 4 b: EMIT PARSED LCONF OBJ ==============\n') print(lconf_section_emitted_parsed_obj_str) # EXAMPLE 4 b: EMIT TO JSON lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__example_4b_lconf_section_raw_str, 'EXAMPLE 4 b', lconf_section__example_4b__template_obj, with_comments=False, validate=True ) result_ordered_native_type = lconf_to_ordered_native_type(lconf_parse_obj) dump_json = json_dumps(result_ordered_native_type, indent=3) print('\n\n============== EXAMPLE 4 b: EMIT TO ORDERED JSON ==============\n') print(dump_json) # EXAMPLE: EMIT TO YAML if has_yaml: lconf_parse_obj = lconf_parse_section_extract_by_name( lconf_section__example_4b_lconf_section_raw_str, 'EXAMPLE 4 b', lconf_section__example_4b__template_obj, with_comments=False, validate=True ) result_native_type = lconf_to_native_type(lconf_parse_obj) dump_yaml = yaml_dump(result_native_type, indent=3, allow_unicode=True) print('\n\n============== EXAMPLE 4 b: EMIT TO YAML ==============\n') print(dump_yaml)
def test_lconf_to_native_type_ok0(): """ Tests: test_lconf_to_native_type_ok0 """ print('::: TEST: test_lconf_to_native_type_ok0()') lconf_section__template_obj = Root([ # Default Empty Line ('#1', ''), # Default Comment Line ('#2', '# Comment-Line: `Key :: Value Pair`'), ('first', ''), ('last', '', None, 'NOT-DEFINED'), ('sex', '', None, 'NOT-DEFINED'), ('age', '', lconf_to_int, -1), ('salary', ''), ('#3', '# Comment-Line: `Key-Value-List`'), ('interests', KVList(True, [])), ('#4', '# Comment-Line: `Key :: Value Pair`'), ('registered', ''), ]) lconf_section_raw_str = r'''___SECTION :: EXAMPLE 1 #1 :: #2 :: # Comment-Line: `Key :: Value Pair` first :: Joe last :: Smith sex :: m age :: 18 salary :: 12500 #3 :: # Comment-Line: `Key-Value-List` - interests soccer tennis #4 :: # Comment-Line: `Key :: Value Pair` registered :: False ___END''' lconf_obj = lconf_parse_section_extract_by_name( lconf_section_raw_str, 'EXAMPLE 1', lconf_section__template_obj, with_comments=False, validate=True) result_native_type = lconf_to_native_type(lconf_obj) ok_(isinstance(lconf_obj, LconfRoot), msg=None) ok_(isinstance(result_native_type, dict), msg=None) ok_(isinstance(lconf_obj['interests'], LconfKVList), msg=None) ok_(isinstance(result_native_type['interests'], list), msg=None) ok_(lconf_obj['last'] == result_native_type['last'] == 'Smith', msg=None) ok_(lconf_obj['age'] == result_native_type['age'] == 18, msg=None) # RE DUMP AS JSON re_dump_json = json_dumps(result_native_type, indent=3) # RE CONVERT TO LCONF result_reconverted_dict_to_lconf2 = lconf_dict_to_lconf( json_loads(re_dump_json), 'EXAMPLE 1', onelinelists=False, skip_none_value=False) lconf_validate_one_section_str(result_reconverted_dict_to_lconf2) if has_yaml: dump_yaml = yaml_dump(result_native_type, indent=3, allow_unicode=True) parsed_load_yaml = yaml_load(dump_yaml) ok_(isinstance(parsed_load_yaml, dict), msg=None) ok_(isinstance(parsed_load_yaml['interests'], list), msg=None) ok_(parsed_load_yaml['last'] == lconf_obj['last'] == result_native_type['last'] == 'Smith', msg=None) ok_(parsed_load_yaml['age'] == lconf_obj['age'] == result_native_type['age'] == 18, msg=None)