[LIB] ConfOverride - Override script configuration object with ini pairs

Post your working scripts, libraries and tools.
User avatar
cyruz
Posts: 346
Joined: 30 Sep 2013, 13:31

[LIB] ConfOverride - Override script configuration object with ini pairs

Post by cyruz » 04 Oct 2022, 17:58

Hi guys,

just a quick function I wrote to "normalize" the way I initialize all my scripts.

It overrides the properties of a configuration object with the key:value pairs in each section of an ini file.

Function:

Code: Select all

; ----------------------------------------------------------------------------------------------------------------------
; Function .....: ConfOverride
; Description ..: Override a configuration object with data read from an ini configuration file.
; ..............: The configuration object needs to contain separate section objects with key:value pairs. Eg:
; ..............: CONF := {}, CONF.SCRIPT := { a:"value", b:"value" }, CONF.SETTINGS := { c:"value" } and so on...
; ..............: Use the pair __LOCKED:1 to avoid a section being overridden.
; ..............: Use the pair __OBJECT:1 to get an object with the Value property set on return (eg. { Value: x }).
; Parameters ...: oConf - Configuration object.
; ..............: sFile - Path to the ini file.
; Return .......: 1 - Object updated.
; ..............: 0 - Object not updated.
; AHK Version ..: AHK v2 x32/64 Unicode
; Author .......: cyruz - http://ciroprincipe.info
; License ......: WTFPL - http://www.wtfpl.net/txt/copying/
; Changelog ....: Oct. 04, 2022 - v0.1.0 - First version.
; ..............: Jan. 01, 2023 - v0.1.1 - Added the possibiliy to get an object with the Value property on return.
; ..............: Mar. 10, 2024 - v0.1.2 - Redirect __Enum to OwnProps() for each section object.
; ----------------------------------------------------------------------------------------------------------------------

ConfOverride(oConf, sFile)
{
    If !FileExist(sFile)
        Throw Error("Configuration file not existing.", sFile)

    sIniFile := IniRead(sFile)
    Loop Parse, sIniFile, "`n"
    {
        If oConf.HasOwnProp(A_LoopField)
        {
            If oConf.%A_LoopField%.HasOwnProp("__LOCKED")
            && oConf.%A_LoopField%.__LOCKED
                Continue
            sSectionName    := A_LoopField
            sSectionContent := IniRead(sFile, sSectionName)
            bObject         := oConf.%A_LoopField%.HasOwnProp("__OBJECT")
                            && oConf.%A_LoopField%.__OBJECT ? 1 : 0
            oConf.%sSectionName%.DefineProp("__Enum", {Call: (*) => oConf.%sSectionName%.OwnProps()})
            Loop Parse, sSectionContent, "`n"
                If RegExMatch(A_LoopField, "S)^\s*(\w+)\s*\=\s*(.*)\s*$", &oMatch)
                    oConf.%sSectionName%.%oMatch.1% := bObject ? { Value: oMatch.2 } : oMatch.2
                  , bUpdated := 1
       }
    }
    Return IsSet(bUpdated) ? 1 : 0
}
Example:

Code: Select all

#Include <ConfOverride>

; This is an example configuration object.
CONF := {}
       CONF.SCRIPT :=
       {
           NAME                : "Script Name"
         , DESCRIPTION         : "Script Descriptiom"
	     , ICON                : A_ScriptDir "\ScriptIcon.ico"
	     , INI                 : A_ScriptDir "\ScriptIni.ini"
         , VERSION             : 0.1
         , __LOCKED            : 1
       }
       CONF.SETTINGS :=
       {
           A : "Value"
         , B : "Value"
       }
       CONF.PROFILES :=
       {
           A : "Value"
       }

MsgBox(CONF.SETTINGS.A)
ConfOverride(CONF, CONF.SCRIPT.INI)
MsgBox(CONF.SETTINGS.A)
Ini File:

Code: Select all

[SETTINGS]
A=Cucu!

Changelog:
  • Jan. 01, 2023 - v0.1.1 - Added the possibiliy to get an object with the Value property on return.
  • Mar. 10, 2024 - v0.1.2 - Redirect __Enum to OwnProps() for each section object.
Cheers :beer:
ABCza on the old forum.
My GitHub.

Return to “Scripts and Functions (v2)”