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 whateverFromImporterBaseandToImporterBasereference.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
ToCsvwhich 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 callsmake_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.
Widgetmodel name becomesWidgetImporterclass 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_classrepresents the source side instead of the target. It usessource_model_classinstead, 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
FromSqlalchemyToCsvHandlerMixinfor most of the heavy lifting.
- 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 setsoutput_fileand alsooutput_writer. And it callswrite_output_header()to write the field header row.
- output_writer = None¶
While the output file is open, this will reference a
csv.DictWriterinstance.
- 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_runis true, this callswriterow()on theoutput_writerinstance.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 theoutput_writerinstance.