dm-script

Efficiently Reading JSON/INI Configuration Files in Gatan Script


I'm developing an application in Gatan Script and need to flexibly configure the UI by setting text values and checkbox statuses using a configuration file. Currently, I'm using an INI file formatted like this:

[basic]
display_mag = False
display_scalebar = True

At the moment, I read the file content line by line using ReadFileLine, but this approach is somewhat cumbersome and not very maintainable. I have a couple of questions:

I've already attempted the line-by-line method, but the code becomes lengthy and difficult to manage. Any suggestions or examples of a more concise and robust solution would be greatly appreciated.

Thanks in advance for your help!


Solution

  • As long as your data is in the form of a text-based file, then ReadFileLine is really the only possible approach from within DM scripting. Text-based files can be in any number of formats, so one needs to parse each such line, which ultimately requires format-specific script code anyway.

    Another option is to save configuration data in TagGroup files, which can be loaded and saved via the TagGroupLoadFromFile and TagGroupSaveToFile functions. However, these are not text-based and so cannot be edited with text editor programs. On the other hand, one can edit them within DM using the TagGroupOpenBrowserWindow(1) function applied to any TagGroup object loaded from a file. Details are covered in the Scripting>Objects>TagGroup Object section of DM's on-line help.