python-libconf icon indicating copy to clipboard operation
python-libconf copied to clipboard

Include directive issue

Open pevsonic opened this issue 4 years ago • 2 comments

I've got some config files to define lots of different configurable items (its a big protocol message set) that work as follows :

item-defs.cfg :

item-defs: (
@include "items/item1.cfg",
@include "items/item2.cfg",
# ...
@include "items/item150.cfg"
);

items/item1.cfg :

{
    item-def = {
        # ...
    };
},

items/item150.cfg :

{
    item-def = {
        # ...
    };
},

etc...

These work fine via libconfig but using libconf I get an error such as :

    item_defs.config = libconf.load(f)
  File "/home/pev/.local/lib/python3.7/site-packages/libconf.py", line 497, in load
    includedir=includedir)
  File "/home/pev/.local/lib/python3.7/site-packages/libconf.py", line 251, in from_file
    tokens.extend(tokenizer.tokenize(''.join(lines)))
  File "/home/pev/.local/lib/python3.7/site-packages/libconf.py", line 206, in tokenize
    string[pos:pos+20]))
libconf.ConfigParseError: Couldn't load config in 'item-defs.cfg' row 10, column 1: '@include "item-defs/'

Looking in the source this appears to be a limitation with the Tokeniser but there should be a way around it somehow. Can you advise on any approach for a workaround?

pevsonic avatar May 26 '20 17:05 pevsonic

Libconf treats include directives as if the 'child file' contents had been inlined at that exact place in the 'parent file'. Maybe it is just a problem with the placement of the comma after the @include directive? That could be fairly easy to resolve.

If you can put together a small reproduction case (just 2 or 3 files to show the problem), I can have a look at it.

ChrisAichinger avatar May 13 '21 08:05 ChrisAichinger

I'm able to reproduce this issue with a similar config structure:

item_list = (
  @include "test_include1.cfg",
  @include "test_include2.cfg"
);

where test_include1.cfg is:

{
  name = "test1"
}

and test_include2.cfg is:

{
  name = "test2"
}

I've attached a patch that fixes the issue for me (but I can't say that I have extensively tested it).

libconf.py.patch

dpelton-ciena avatar Aug 01 '23 18:08 dpelton-ciena