wuttasync.exporting.csv

Exporting to CSV

class wuttasync.exporting.csv.FromSqlalchemyToCsvHandlerMixin[source]

Mixin class for SQLAlchemy ORM → CSV export handlers.

This knows how to dynamically generate exporter classes to represent the models in the source ORM. Such classes will inherit from FromSqlalchemyToCsvMixin, in addition to whatever FromImporterBase and ToImporterBase reference.

That all happens within define_importers().

FromImporterBase = None

For a handler to use this mixin, it must set this to a valid base class for the ORM source side. The define_importers() logic will use this when dynamically generating new exporter classes.

ToImporterBase

This must be set to a valid base class for the CSV target side. Default is ToCsv which should typically be fine; you can change if needed.

alias of ToCsv

define_importers()[source]

This mixin overrides typical (manual) importer definition, and instead dynamically generates a set of exporters, e.g. one per table in the source DB.

It does this based on the source model, as returned by get_source_model(). It calls make_importer_factory() for each model class found.

get_source_model()[source]

This should return the app model or a similar module containing data model classes for the source side.

The source model is used to dynamically generate a set of exporters (e.g. one per table in the source DB) which can use CSV file as data target. See also define_importers().

Subclass must override this if needed; default behavior is not implemented.

make_importer_factory(model_class, name)[source]

Generate a new exporter class, targeting the given data model class.

The newly-created class will inherit from:

Parameters:
  • model_class – A data model class.

  • name – The “model name” for the importer/exporter. New class name will be based on this, so e.g. Widget model name becomes WidgetImporter class name.

Returns:

The new class, meant to process import/export targeting the given data model.

class wuttasync.exporting.csv.FromSqlalchemyToCsvMixin[source]

Mixin class for SQLAlchemy ORM → CSV exporters.

Such exporters are generated automatically by FromSqlalchemyToCsvHandlerMixin, so you won’t typically reference this mixin class directly.

This mixin effectively behaves like the model_class represents the source side instead of the target. It uses source_model_class instead, for automatic things like inspecting the fields list.

class wuttasync.exporting.csv.FromWuttaToCsv(config, **kwargs)[source]

Handler for Wutta (app database) → CSV export.

This uses FromSqlalchemyToCsvHandlerMixin for most of the heavy lifting.

FromImporterBase

alias of FromWutta

class wuttasync.exporting.csv.ToCsv(config, **kwargs)[source]

Base class for exporter using CSV file as data target.

This inherits from ToFile.

csv_encoding = 'utf_8'

Encoding used for the CSV output file.

You can specify an override if needed when calling process_data().

open_output_file()[source]

Opens the output CSV file for writing.

This calls get_output_file_path() and opens that file. It sets output_file and also output_writer. And it calls write_output_header() to write the field header row.

output_writer = None

While the output file is open, this will reference a csv.DictWriter instance.

update_target_object(obj, source_data, target_data=None)[source]

In a CSV export the assumption is we always start with an empty file, so “create” is the only logical action for each record - there are no updates or deletes per se.

But under the hood, this method is used for create as well, so we override it and actually write the record to CSV file. Unless dry_run is true, this calls writerow() on the output_writer instance.

See also parent method docs, update_target_object()

write_output_header()[source]

Write the field header row to the CSV file.

Default logic calls writeheader() on the output_writer instance.

class wuttasync.exporting.csv.ToCsvHandler(config, **kwargs)[source]

Base class for export handlers using CSV file(s) as data target.