Confusion about configparser Python module

Hello,

I need to both read and write settings to an INI file. This is trivial using the standard configparser module in Python. I see that there is a rs.GetSettings function but no corresponding rs.WriteSettings so that direction won’t work.

I tried import configparser as I would in Python but that is not available.

After some searching there is a ConfigParser module available (with different capitalization) but this does not behave the way the standard module does.

Sample file:

[Project]
Project Name = not defined.

Simplest script:

import ConfigParser
import sys

Filename = ‘C:/Temp/RhinoParameterFile.ini’
Parser = ConfigParser.ConfigParser()
Parser.read(Filename)
ProjectName = Parser[‘Project’][‘Project Name’]

Error message:

Message: ‘instance’ object has no attribute ‘getitem

Traceback:
line 7, in , “C:\Users\henry\Documents\Shape Fidelity\Rhino Not Project Specific\ConfigFileTest.py”

It is very frustrating when the most basic Python things don’t work. What is the intended solution for reading and writing INI files?

Thanks for your help/

Moved to Scripting category

Hi @Henry_Wede,

How about something like this?

import Rhino
import System

__RHINOSCRIPT__ = '1c7a3523-9a8f-4cec-a8e0-310f580536a7'

def GetSettings(fileName, appName, keyName):
    rhinoScript = Rhino.RhinoApp.GetPlugInObject(__RHINOSCRIPT__)
    if rhinoScript:
        return rhinoScript.GetSettings(fileName, appName, keyName)
    return None
    
def SaveSettings(fileName, appName, keyName, value):
    rhinoScript = Rhino.RhinoApp.GetPlugInObject(__RHINOSCRIPT__)
    if rhinoScript:
        return rhinoScript.SaveSettings(fileName, appName, keyName, value)

if __name__ == "__main__":
    path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)
    fileName = System.IO.Path.Combine(path, "test.ini")
    appName = "MyApp"
    keyName = "Message"
    
    SaveSettings(fileName, appName, keyName, "Hello Rhino!")
    
    value = GetSettings(fileName, appName, keyName)
    if value:
        print(value)

– Dale

1 Like

What is the intended solution for reading and writing INI files?

Try using the Python 2 version of ConfigParser with its correct syntax and API

ProjectName = Parser.get('Project', 'Project Name')

https://docs.python.org/2.7/library/configparser.html

or implementing __getitem__ yourself on a subclass:

class DictParser(ConfigParser.ConfigParser):
    def __getitem__(self, x):
        return dict(self.items(x))

My experience with ConfigParser was nonetheless, erm, trying. I switched to .toml files.

toml from PyPi worked great for a while, but unicode didn’t work in GhPython with it. It had so many bugs, was so poorly maintained and poorly tested in general compared to tomli and tomli-w, that I forked the latter two especially for GhPython:

After a couple false starts, I was able to get this to work. My simplified version was this:

import Rhino
__RHINOSCRIPT__ = '1c7a3523-9a8f-4cec-a8e0-310f580536a7'
rhinoScript = Rhino.RhinoApp.GetPlugInObject(__RHINOSCRIPT__)

Filename = 'C:/Temp/RhinoParameterFile.ini'

Result = rhinoScript.SaveSettings(Filename, 'MyApp', 'Key name', 'Key value')
print(Result)

TestKey = rhinoScript.GetSettings(Filename, 'MyApp', 'Key name')
print(TestKey)

Can you please explain lines 2 and 3? I have never seen this and I have no idea why it is necessary.

Henry

1 Like

Hi @Henry_Wede,

Here is the help for RhinoApp.GetPlugInObject.

And here is the help for legacy RhinoScript.

– Dale