Serialization (Export)¶
CIMPyORM can be used to serialize datasets into XML-files as defined by the CIM standard. This way, datasets can be exported from CIMPyORM to facilitate import into other tools.
Dataset preparation¶
Before serialization, a dataset needs to be created and populated with CIM objects. To do this, either parse a dataset (as described in Quickstart), or start with an empty dataset and fill it with your own objects:
dataset, model = cimpyorm.create_empty_dataset(version="16")
Either add single objects:
term1 = model.Terminal(id=42, phases=model.enum.PhaseCode.v.AB)
dataset.add(term1)
Or add multiple at once:
dataset.add_all((model.Terminal(id=id) for id in range(10))
And finally commit the changes to the database:
dataset.commit()
Export¶
Once your dataset is ready to be exported, you can use the export
function to generate the
XML representation of the dataset as an InMemory-BytesIO object that can be written to disk or
streamed.
- cimpyorm.export(dataset, mode='Single', profile_whitelist=None, header_data=None)[source]¶
Returns a XML-serialization of the dataset within a zip-Archive.
- Parameters
dataset – The dataset/SQLAlchemy-Session object to export.
mode (
str
) – {‘Single’, ‘Multi’} - The export Mode, e.g. Single-File or Split by profiles.profile_whitelist (
Union
[list
,tuple
,None
]) – The profiles to export. Mandatory if Export-mode is ‘Multi’. Profiles can be specificed by their full name (e.g. ‘EquipmentProfile’ or their short name ‘EQ’)header_data (
Optional
[dict
]) – Additional information for creating the file-headers (FullModel Objects). This is a dictionary of CIM-header supplements. Currently only the ‘profile_header’ field is supported, in which a list of profile identifiers (e.g. ‘http://entsoe.eu/CIM/Topology/4/1’) are defined.
- Returns
A BytesIO-filehandle containing a zip-archive of the export.
The result can be written to disk by:
with open("outfile.xml", "wb+") as f: # Open the file handle in binary mode with write permissions
f.write(result.getvalue())