Welcome | Get started | Dive into Lino | Contribute | Topics | Reference | More

lino.utils.dpy

Defines Lino's Python serializer and deserializer. See Specification.

Functions

Deserializer(fp, **options)

The Deserializer used when manage.py loaddata encounters a .py fixture.

create_mti_child(parent_model, pk, ...)

Similar to lino.utils.mti.insert_child(), but for usage in Python dumps (generated by pm dump2py).

install_migrations(self, loader)

Install "migrators" into the given global namespace.

override(globals_dict)

A decorator to be applied when redefining, in a migrate_from_VERSION() method, one of the create_APP_MODEL() functions defined in the restore.py file of a dump.

unused_load_fixture_from_module(m, **options)

No longer used in unit tests to manually load a given fixture module.

Classes

DpyDeserializer()

The Django deserializer for The Python serializer.

DpyLoader(globals_dict[, quick])

Instantiated by restore.py.

FakeDeserializedObject(deserializer, object, ...)

Imitates DeserializedObject required by loaddata.

FlushDeferredObjects()

Indicator class object.

LoaderBase()

Migrator(site, loader)

The SITE's Migrator class is instantiated by install_migrations.

Serializer()

Serializes a QuerySet to a py stream.

lino.utils.dpy.create_mti_child(parent_model, pk, child_model, **kw)

Similar to lino.utils.mti.insert_child(), but for usage in Python dumps (generated by pm dump2py).

The difference is very tricky. The return value here is an "almost normal" model instance, whose save and full_clean methods have been hacked. These are the only methods that will be called by Deserializer. You should not use this instance for anything else and throw it away when the save() has been called.

class lino.utils.dpy.FakeDeserializedObject(deserializer, object, **kw)

Bases: DeserializedObject

Imitates DeserializedObject required by loaddata.

Unlike normal DeserializedObject, we don't want to bypass pre_save and validation methods on the individual objects.

try_save(*args, **kw)

Try to save the specified Model instance obj. Return True on success, False if this instance wasn't saved and should be deferred.

class lino.utils.dpy.Serializer

Bases: Serializer

Serializes a QuerySet to a py stream.

Usage: manage.py dumpdata --format py

DEPRECATED. The problem with this approach is that a serializer creates -by definition- one single file. And Python needs -understandably- to load a module completely into memory before it can be executed. Use pm dump2py instead.

class lino.utils.dpy.FlushDeferredObjects

Bases: object

Indicator class object. Fixture may yield a FlushDeferredObjects to indicate that all deferred objects should get saved before going on.

class lino.utils.dpy.DpyLoader(globals_dict, quick=None)

Bases: LoaderBase

Instantiated by restore.py.

class lino.utils.dpy.DpyDeserializer

Bases: LoaderBase

The Django deserializer for The Python serializer.

Note that this deserializer explicitly ignores fixtures whose source file is located in the current directory because i the case of .py files this can lead to side effects when importing them. See e.g. #1029. We consider it an odd behaviour of Django to search for fixtures also in the current directory (and not, as documented, in the fixtures subdirs of plugins and the optional FIXTURE_DIRS).

lino.utils.dpy.Deserializer(fp, **options)

The Deserializer used when manage.py loaddata encounters a .py fixture.

This is done by setting SERIALIZATION_MODULES={"py": "lino.utils.dpy"}.

See SERIALIZATION_MODULES.

class lino.utils.dpy.Migrator(site, loader)

Bases: object

The SITE's Migrator class is instantiated by install_migrations.

If migration_class is None (the default), then this class will be instantiated. Applications may define their own Migrator class which should be a subclasss of this.

after_load(todo)

Declare a function to be called after all data has been loaded.

before_load(todo)

Declare a function to be called before loading dumped data.

lino.utils.dpy.install_migrations(self, loader)

Install "migrators" into the given global namespace.

Python dumps are generated with one line near the end of their restore.py file which calls this method, passing it their global namespace:

settings.SITE.install_migrations(globals())

A dumped fixture should always call this, even if there is no version change and no data migration, because this also does certain other things:

  • set loading_from_dump to True

  • remove any Permission and Site objects that might have been generated by post_syncdb signal if these apps are installed.

lino.utils.dpy.unused_load_fixture_from_module(m, **options)

No longer used in unit tests to manually load a given fixture module.

lino.utils.dpy.override(globals_dict)

A decorator to be applied when redefining, in a migrate_from_VERSION() method, one of the create_APP_MODEL() functions defined in the restore.py file of a dump.