Python Code Generator

System documentation for ResInsight


ResInsight is able to produce a Python library that is capable of interacting with ResInsight. This generator produce Python code based on tagging of classes and members in C++, making it easy to maintain the Python code from one code base.

The Python code is partly hand-coded classes and autogenerated classes from ResInsight. The Python code is located at https://github.com/OPM/ResInsight/tree/dev/GrpcInterface/Python

Description of the rips library

Manually maintained classes are located at the root of the rips library. Some manually maintained framework classes and methods are located in pdmobject.py This includes helper functions to convert data between Python and GRPC data structures used to communicate with ResInsight. https://api.resinsight.org/en/main/api/rips.PdmObjectBase.html#rips.PdmObjectBase

When a class is tagged to be scriptable using CAF_PDM_InitScriptableObject, Python code for this class can be generated. This code is generated by ResInsight using a command line parameter. The fields to be exposed in Python is tagged with CAF_PDM_InitScriptableField...

Python code generation

Python executable must be made available to the ResInsight build process. Then, GRPC with dependencies must be installed. If these requirements are met, the Python code will automatically be produced during a normal ResInsight build.

Manual generation of Python code

  1. Build ResInsight
  2. Start ResInsight with command line parameter --generate myPythonCode.py
  3. ResInsight will start, objects will be inspected, and Python code will be exported to the file myPythonCode.py
  4. The utility functions in ResInsight is located at https://github.com/OPM/ResInsight/blob/3a9407886749ca7163197136ab80c884b131a06c/Fwk/AppFwk/cafPdmScripting/cafPdmPythonGenerator.h

Concepts used to produce Python code

Command file functions ResInsight has a basic script interpreter based on sequential text commands read from a file. This was implemented before the Python support. Command file object derive from RicfCommandObject and can be executed using the class RicfCommandFileExecutor

To be able to reuse these functions, a wrapper for these functions is available in Python.

Class and methods Use the macro CAF_PDM_InitScriptableObject to mark a class as scriptable. Use the macro CAF_PDM_InitScriptableField... to mark a field as scriptable.

Add Python method to an existing class Sometimes it is required to add some more code related to a function using the Python API. This can be adding new objects to a parent object, and more management code related to this operation is required. To use this concept, add a new class deriving from PdmObjectMethod, and connect to the class you want to extend using the class keyword.

More details on the GRPC interface https://ceetronsolutions.github.io/resinsight-system-doc/editor/grpc-interface