Architecture
This page describes the layers of ili2py from the IMD to the read XTF as they are now: how the python classes come to be, rendered as a library or built at runtime, and how the reader works with either of them, with the geometry read compact or not.
The layers
flowchart TD
imd[/"IMD (IlisMeta16)"/] --> meta["metamodel dataclasses<br/>readers.interlis_24.ilismeta16.xsdata.Imd16Reader<br/>interfaces.interlis.interlis_24.ilismeta16"]
meta --> index["Index<br/>mappers.helpers"]
index --> library["Library<br/>writers.py.python_structure"]
library --> prepare["prepare_library<br/>writers.py.render"]
prepare --> builder["Ili23SpecBuilder / Ili24SpecBuilder<br/>readers.xtf.spec_builder"]
builder --> specs["ClassSpec, ModuleSpec<br/>readers.xtf.specs"]
specs --> renderer["SpecRenderer, TreeLayout<br/>writers.py.spec_renderer, tree_layout"]
renderer --> templates["jinja2 templates per spec kind<br/>writers.py.templates"]
templates --> rendered[/"rendered library<br/>package on disk"/]
specs --> factory["RuntimeClassFactory<br/>readers.xtf.runtime_classes"]
factory --> runtime[/"runtime classes<br/>RuntimeLibrary, one flat module"/]
rendered --> backend["XtfBackend: XsdataBackend<br/>readers.xtf.xsdata_backend"]
runtime --> backend
xtf[/"XTF"/] --> backend
backend --> tree["transfer object tree"]
tree --> access["TransferAccess, geometry access protocols<br/>readers.xtf.access"]
| Layer | Where | What it does |
|---|---|---|
| Metamodel | readers.interlis_24.ilismeta16, interfaces.interlis |
Reads the IMD with xsdata into the dataclasses of IlisMeta16, one class per metamodel class. |
| Index | mappers.helpers.Index |
Resolves the associations of the metamodel once (attributes of a class, imports, line forms, values of extended enumerations, inherited OIDs, transfer order, ...), so nobody has to walk the metamodel again. |
| Library structure | writers.py.python_structure.Library |
The python view of the models: packages, modules, classes, attributes, enumerations, domain types. It is the input of the spec builder. |
| Version handling | writers.py.render.prepare_library |
Chooses the INTERLIS version (2.3 or 2.4). For 2.3 it puts the translated XML names into a copy of the library, as 2.3 writes them translated. |
| Specs | readers.xtf.spec_builder, specs |
Describes every class once (ClassSpec): fields, XML names, docstrings, properties, members, bases. The differences of 2.3 and 2.4 live here. |
| Rendered classes | writers.py.render, spec_renderer, tree_layout, templates |
Writes the specs as python package, one module per topic. The package does not depend on ili2py. |
| Runtime classes | readers.xtf.runtime_classes, runtime |
Builds the classes of the very same specs in memory, without writing code. |
| Backend | readers.xtf.backend, xsdata_backend, geometry_parser |
Binds the classes to an XTF, reads and writes it. It does not care where the classes come from. |
| Access | readers.xtf.access, geometry, dataclass_access |
Finds elements by their INTERLIS names and reads the geometry the same way, whatever class flavour and reading mode. |
The metamodel, the index and the library structure are shared by everything built on top, the diagrams included.
One description, two layouts
Both flavours are made of the same specs. The spec builder of the INTERLIS version describes every class once, the rendered library and the runtime only differ in where they put the classes and how they name them.
flowchart LR
library["prepared Library"] -->|"spec builder"| spec["ClassSpec per class<br/>references by key"]
spec -->|"TreeLayout, templates"| source["modules of a package<br/>nested helper classes"]
spec -->|"RuntimeClassFactory"| flat["one flat module<br/>registry by key"]
source -. "same classes, same XTF" .- flat
Specs. RuntimeLibrary.from_imd and ili2py python-classes run the same chain up to the prepared library, then
the spec builder of the version (Ili23SpecBuilder, Ili24SpecBuilder) describes each class as a ClassSpec: fields
(FieldSpec), choices, properties as data, enumeration members, bases, docstring and the kind of class (dataclass,
enum or class for the domain and BLACKBOX types). The packages and modules of the rendered library are described
as ModuleSpec (docstring, VERSION, ILI_VERSION, metadata). A spec names other classes by key only, the python
path the class has in the rendered library, e.g. {oereb.GeometryCHLV95_V1.Line.Segment}. What is written instead
of the key is up to the layout.
Rendered. SpecRenderer writes the specs as source. TreeLayout knows the module of each key and the name a
module sees a class by, and collects the imports this needs:
| The class is | Named as | Imported by |
|---|---|---|
| nested in the class body the name is evaluated in | Segment |
- |
| in the same module | Line.Segment |
- |
| in a model (package) | INTERLIS.XmlDateTime |
from <library> import INTERLIS |
| in a topic (module) | Model_Topic_Class |
from <library>.Model.Topic import Class as Model_Topic_Class |
in a module of the library itself (references) |
BinBlBoxType |
from <library>.references import BinBlBoxType |
Each spec kind has its template (dataclass.jinja2, enum.jinja2, class.jinja2), the parts of a class body they
share are in macros/class_body.jinja2. The geometry access methods are written into the source as well
(macros/access.jinja2), the package must stand on its own. The templates know nothing about INTERLIS versions,
autoflake, isort and black tidy up each file afterwards.
Runtime. RuntimeClassFactory builds a class of each spec, all of them flat in one module, the class name is the
slug of the key. The module has to be in sys.modules while the classes are used by xsdata, the annotations are
resolved there. A class whose spec is about geometry (ClassSpec.access) gets the access methods as base class from
readers.xtf.dataclass_access instead of having them written into its body.
A runtime class keeps the spec it is built of (__ili2py_spec__), so RuntimeLibrary.render(cls) gives its source as
the rendered library has it. Since there is one description, the two flavours can not drift apart. The end-to-end tests
still read and write the XTF of the same models with both and compare the result.
Reading and writing
The backend gets the class of the root element of the XTF (Transfer) and works with whatever it is handed:
XsdataBackend().read("data.xtf", Transfer) # rendered
XsdataBackend().read("data.xtf", library.transfer) # runtime, what library.read does
flowchart TD
read["XsdataBackend.read(source, Transfer)"] --> converters["registered_converters(library name)"]
converters -->|"module offers registered_converters"| own["runtime: the special types and enumerations of the specs"]
converters -->|"it does not offer "| modules["rendered: convertable_types and references modules"]
own --> mode{"compact_geometry"}
modules --> mode
mode -->|"No"| plain["XtfXmlParser + XtfLxmlEventHandler<br/>xsdata binds every element,<br/>geometry as dataclasses"]
mode -->|"Yes"| pass1["GeometryXmlParser, pass 1<br/>xsdata binds the objects,<br/>subtrees of geometric fields are skipped and stored"]
pass1 --> pass2["bind_geometries(CompactGeometryBuilder), pass 2<br/>Coord, Polyline, Ring, Surface, Multi* from the stored subtrees"]
plain --> tree["transfer object tree"]
pass2 --> tree
What the backend needs to know it reads from the classes, never from the IMD:
- The special types (domains of str, int, float and BLACKBOX BINARY) and the enumerations have to be registered as
xsdata converters for the time of the call. xsdata would take an enumeration (
str, Enum) for text, registered a value of the XTF becomes the member and an unknown one is reported. A rendered library has them in its modulesconvertable_typesandreferences, the module of a runtime library offers its ownregistered_converters. - The root element of the XTF has to be the one of the transfer class,
XtfLxmlEventHandlerchecks it before anything is bound, an XTF of the other INTERLIS version fails with a clear error. - An element no class knows fails with the default configuration of xsdata. Configured to go on
(
fail_on_unknown_properties=False), xsdata skips it without a word, soXtfXmlParser(andGeometryXmlParserextending it) logs a warning for each skipped object. - The XML names of the geometry (2.3 or 2.4) are chosen by the namespace in the
Metaof the transfer class (GeometryTags.reads). - Which fields hold geometry is told by
metadata["geometric"]["is_geometric"]of the field. The precision and the python types of the axes come from the coord dataclass below the field and its type restrictions.
Writing goes the other way round: GeometrySerializer hands a compact geometry as a whole to GeometryEventWriter,
which writes its elements as text the same way xsdata would. Geometry held as dataclasses is written by xsdata itself.
What a geometric field holds
| Classes | Read normal | Read compact |
|---|---|---|
| Rendered | geometry dataclasses, access methods written into the source by the templates | geometry.Coord, Polyline, Surface, MultiPolyline, MultiSurface |
| Runtime | geometry dataclasses, access methods by base classes of dataclass_access |
the same compact geometries |
All four fulfil the protocols of readers.xtf.access (CoordAccess, PolylineAccess, SurfaceAccess,
MultiPolylineAccess, MultiSurfaceAccess). A tool reading a surface does not need to know which of the four it got.
In the same way TransferAccess finds baskets and elements by their INTERLIS names: both flavours carry the name in
their metadata and mirror EXTENDS in their python bases. See Geometry access
and Access by INTERLIS name.