File.containsConfPair
property
A property to set key = value
pairs under particular [sections]
in config files. For example, in stock Debian Jessie /etc/lightdm/lightdm.conf
contains the lines
[SeatDefaults]
#autologin-user=
With the property
"/etc/lightdm/lightdm.conf" `File.containsConfPair` ("SeatDefaults", "autologin-user", "swhitton")
this will get set to
[SeatDefaults]
autologin-user=swhitton
LightDM.autoLogin
property
An application of File.containsConfPair
to edit /etc/lightdm/lightdm.conf
to enable autologin for a specified user: a property encapsulating the above example.
Patches
Please see the two commits in branch confpairs
in the repo at git@github.com:spwhitton/propellor.git
.
Thanks for submitting these patches!
Looking at
containsConfPair
, it assumes an ini-style file, so is a little misplaced in Property.File. which is otherwise about generic text files.So, it would probably make sense to move it to a new Property.IniFile module.
However, parsing a config file recently pointed out that the tor config file has a similar need. It's not ini format, but shares the same basic idea of a "section" line which is followed by lines setting things specific to that section.
So, it would be great if
containsConfPair
could be generalized to also cover that tor config file use case. I think this would be pretty easy; just make it take one string containing the whole section line (including square brackets for ini file, or whatever for tor config file), and a second string containing the whole setting line.Thanks for the input!
I agree that generalising to lines under sections is a good idea, but I don't think it can be as simple as a property taking the full section header and the full settings line. That's because there is a need to update the values of keys under sections: in the example LightDM case, the line
autologin-user=someone
must replace anyautologin-user=someone_else
. So the function needs to know the key, not just the whole line.So to generalise containsConfPair, it might take a section header, key, value and a specification of what kind of config file it is. That specification would be a type containing the comment character, the formatting of section headers and the use of spaces, colons or equals signs between keys and values. What do you think to this?
I'd suggest making it take some helper functions.
Something like these:
Which seems sufficiently generic; it can even be used to delete entire sections!
Let's see..
Thanks for the ideas. I've implemented them as a new commit to my confpairs branch. Please take a look.
Two points:
I dropped the SectionEnd helper function. My implementation of adjustSection didn't need it and I couldn't think up a case where it would be needed.
I'm using a tuple
(section, key, value)
as the second argument toConfFile.containsIniPair
, rather than just using four arguments as you suggested. IfConfFile.containsIniPair
takes four arguments, then it cannot be used infix when attached to other properties with the&
operator, without using extra brackets.And merged, thanks.
The SectionEnd would be useful for eg, bind-style or apache-style config files. However, those probably need a better parser than this one anyway.