lipkau / PsIni

Work with INI files in PowerShell using hashtables
http://lipkau.github.io/PsIni
MIT License
151 stars 50 forks source link

Aligning behaviour of PSIni and Python's ConfigParser #49

Open wbrandenburger opened 4 years ago

wbrandenburger commented 4 years ago

Servus,

I'm working a lot with python and its parser for config files. There are several characteristics of an ini file structure, which are not considered in PSIni:

Multiline Values

Indented lines enable multiline values,

[Multiline Values]
chorus: I'm a lumberjack, and I'm okay
    I sleep all night and I work all day

which can be easily considered when insert the evaluation of a Regex-string ,e.g., ^( {4,}.*)\s*$, after inserting a key in Get-IniContent.

Interpolation of values

It enables values to contain format strings which refer to other values in the same section, or values in the special default section. Additionally extended interpolation enables to denote a value from a foreign section.

[BasicInterpolation]
home_dir = /Users
my_dir = %(home_dir)s/lumberjack

[ExtendedInterpolation]
my_pictures = ${BasicInterpolation:my_dir}/Pictures

Moreover, there is the oppurtunity to use environment variables as values.

I have implemented already these functionality but for the last published version in PSGallery 3.1.2. Is there the possibility to open a pull request for version 4.0?

Besides these extensions, there seems to be a misbehaviour when using redundant keys in one section. In Get-IniContent, the generation of [ArrayList] overwrites the current key and logically add to this key a empty object of [ArrayList]. I think, that behaviour is not intended and can be easily fixed.

I'm looking forward hearing from you!

lipkau commented 4 years ago

Hello.

I like your ideas. Would gladly incorporate that into the module and release it as a new major version

Unfortunately, I do not have enough time for now. If you are willing to help, just send me a PR.

majkinetor commented 4 years ago

If this gets incorporated it should be optional, as it will break stuff.

Multiline Values

The INI file format does not support multiline values. This behavior if implemented should be non default as it breaks exiting scripts, for example:

[Multiline Values]
chorus=I'm a lumberjack, and I'm okay
    idented_key=I sleep all night and I work all day

Here, idented key will be taken as a part of above key.

Interpolation of values Moreover, there is the oppurtunity to use environment variables as values.

Env vars are typically used for this. It makes sence to expand env vars, possibly as an option when getting values. The problem here is writting values back - if I read and imediatelly write values all vars would get expanded and removed. WIth behavior like that its almost useless feature. Module will have to write back those env vars if key values containing them were unchanged.

wbrandenburger commented 4 years ago

I totally agree, it has to be optional. The described behavior will be a problem, if set as default. I will consider that.

majkinetor commented 4 years ago

In default behavior, I think that highest possible compatibility with Windows API should be the goal as that behavior is what will be expected by most. Anything else can be added as configurable feature or fun parameter. This could also be tested easily.

I personally started using this one but because it wasn't compatible with WIndows API it changed ini files in ways that are not semantically equal to starting ini file. I finially used custom Ini script that calls Windows API Profile functions.

lipkau commented 4 years ago

about the multiline:

chorus=I'm a lumberjack, and I'm okay
    idented_key=I sleep all night and I work all day

I found this note

Following the syntax of the language it is designed to work with (Python), to span a node over multiple lines ConfigParser requires a deeper indentation in the lines that follow, instead of the more common backslash + line break (see: configparser — Configuration file parser)

on https://en.wikipedia.org/wiki/INI_file#cite_note-13

lipkau commented 4 years ago

@majkinetor

because it wasn't compatible with WIndows API

What part of this module is currently not compatible? :-o

majkinetor commented 4 years ago

Part #50 :) Also trimming of values.

lipkau commented 4 years ago

@majkinetor: How would the Windows API handle the examples I created here?

key=value|
key = value|
 key = value|
key = "value"|
key = value |
key = " value "|
key = "  value  "|
key =   "    value    "    |
key = ""value""|

added | to each line to indicate EOL