crudini icon indicating copy to clipboard operation
crudini copied to clipboard

Cannot create 'default' section

Open kevingunn-wk opened this issue 3 years ago • 1 comments

When running crudini with the --set flag, I'm unable to create a default section.

Command: crudini --set file.conf default name value

Result:

Traceback (most recent call last):
  File "/usr/local/bin/crudini", line 979, in <module>
    sys.exit(main())
  File "/usr/local/bin/crudini", line 975, in main
    return crudini.run()
  File "/usr/local/bin/crudini", line 881, in run
    self.command_set()
  File "/usr/local/bin/crudini", line 731, in command_set
    self.set_name_value(self.section, self.param, self.value)
  File "/usr/local/bin/crudini", line 708, in set_name_value
    self.conf.add_section(section)
  File "/usr/local/lib/python3.9/site-packages/iniparse/compat.py", line 62, in add_section
    raise ValueError('Invalid section name: %s' % section)
ValueError: Invalid section name: default

However, if I modify the command, it succeeeds: crudini --set file.conf defaults name value

File contents:

[defaults]
name = value

If a default section exists, then the command seems to work just fine, but fails when creating a new file when a default section does not yet exist.

kevingunn-wk avatar Aug 17 '21 20:08 kevingunn-wk

Indeed :(

This looks to be an issue in iniparse. It doesn't treat "default" specially when reading, so I don't know why it's restricting creation of such a section. I guess it's for compat with other parsers. I see ConfigParser has similar restrictions, so I guess it's trying to avoid compat issues with it: https://stackoverflow.com/questions/25628651/write-to-lowercase-default-section-configparser/52217359 I also see iniparse hardcodes "default" rather than using its DEFAULTSECT definition.

As a workaround we could check case variations of "default" and change to "default_crudini_remove_when_writing". We already do similar string replacements on write.

I'll need to look into compat issues though a bit, before doing this

pixelb avatar Aug 17 '21 21:08 pixelb