Skip to content

Runtime Classes

Reading an XTF does not need a generated library. ili2py builds the very same classes at runtime, straight from the IMD, and reads and writes the XTF with them. Nothing is written to disk, nothing has to be installed.

When to use which

The generated library is code. It can be read, imported, checked by a type checker and shipped without ili2py. That makes it the way to go for software built around a known model.

The runtime classes are for the cases where the model is only known when the program runs: a tool which reads whatever XTF it gets, a quick look into some data, a test. They need ili2py at runtime and the IMD of the models.

Both read and write the same XTF. The generated library is rendered from the very same description the runtime classes are built of, so they have the same fields, the same XML names, docstrings, properties (metadata, constraints, geom_*), enumerations, domain types and geometry classes. A tool working on the read data does not see a difference, besides the way to get a class.

Generated library Runtime classes
Create ili2py python-classes -i Model.imd -o out -l model RuntimeLibrary.from_imd("Model.imd", "model")
Read XsdataBackend().read("data.xtf", Transfer) library.read("data.xtf")
Get a class from model.Model.Topic import Class library.classes["model.Model.Topic.Class"]
Static typing full none, the classes exist only when the program runs
Depends on ili2py no yes
Lives as long as the package is installed as long as the with block runs

Reading and writing

from ili2py.readers.xtf.runtime import RuntimeLibrary

with RuntimeLibrary.from_imd("SZ_Waldreservate_V3.imd", "wald") as library:
    transfer = library.read("waldreservate.xtf")

    with open("written.xtf", "w", encoding="UTF-8") as out:
        library.write(transfer, out)

The second argument of from_imd is the name of the library, as -l of ili2py python-classes. If it is left out, the name of the root model is taken.

The classes are built when the with block starts and live in a module registered in sys.modules while the block runs. Python needs it to resolve the annotations of the classes. At the end of the block the module is removed again, the objects read stay usable. Without with, read, write, transfer and class_of_oid build the classes on first use, the module stays registered until unload() is called.

read and write use XsdataBackend with its defaults. Another backend can be handed over, e.g. to read the geometry compact (see Compact geometry):

from ili2py.readers.xtf.xsdata_backend import XsdataBackend

with RuntimeLibrary.from_imd("SZ_Waldreservate_V3.imd", "wald") as library:
    transfer = library.read("waldreservate.xtf", XsdataBackend(compact_geometry=True))

The geometry access works the same as with the generated library, read compact or not.

Finding a class

The classes have no place in a package tree, they are all kept in one registry. The key of a class is the python path it has in the generated library of the same name. What the generated library offers as

from wald.SZ_Waldreservate_V3.Stammdaten import Prioritaet

is found at runtime as

prioritaet = library.classes["wald.SZ_Waldreservate_V3.Stammdaten.Prioritaet"]

A class nested in another one (e.g. the Segment of a line type) has the outer classes in its key, as wald.SZ_Waldreservate_V3.Polygon.PolygonBoundary.PolygonPolyline.Segment. The python name of a class is derived from its key (wald_SZ_Waldreservate_V3_Stammdaten_Prioritaet), it is what the debugger shows.

The class of an INTERLIS element is also found by its identifier in the IMD:

prioritaet = library.class_of_oid("SZ_Waldreservate_V3.Stammdaten.Prioritaet")

Usually there is no need to know the classes at all: the elements of the read transfer can be found by their INTERLIS names, see Access by INTERLIS name.

The source of a class

A runtime class has no source file, but it knows the description it is built of. render gives its source as the generated library has it, the names of other classes as its module would import them:

with RuntimeLibrary.from_imd("SZ_Waldreservate_V3.imd", "wald") as library:
    prioritaet = library.classes["wald.SZ_Waldreservate_V3.Stammdaten.Prioritaet"]
    print(library.render(prioritaet))
@dataclass
class Prioritaet(Katalogeintrag):
    class Meta:
        namespace = "http://www.interlis.ch/xtf/2.4/SZ_Waldreservate_V3"

    tid: "str | None" = field(
        default=None,
        metadata={
            "type": "Attribute",
            "name": "tid",
            ...
        },
    )

    @property
    def metadata(self) -> dict:
        return {
            "interlis": {
                "oid": "SZ_Waldreservate_V3.Stammdaten.Prioritaet",
                ...
            },
        }
    ...

It is the source before autoflake, isort and black, as ili2py python-classes writes it into the module first.

Limitations

  • The INTERLIS versions 2.3 and 2.4 are supported, as for the generated library.
  • The runtime classes take the behaviour of the generated library as it is, gaps included, a fix applies to both.
  • A library can only be built once at a time under the same name. A second one with the same name raises an ImportError until the first one is unloaded (end of its with block or unload()).