sqla_inspect package

Submodules

sqla_inspect.ascii module

sqla_inspect.base module

utilities to inspect Sqlalchemy models

class sqla_inspect.base.BaseSqlaInspector(model)[source]

Bases: object

Base class for exporters

get_columns_only()[source]

Return only the columns

static get_info_field(prop)[source]

Return the info attribute of the given property

get_relationships_only()[source]

Return only the relationships

get_sorted_columns()[source]

Return columns regarding their relevance in the model’s declaration

class sqla_inspect.base.FormatterRegistry[source]

Bases: dict

A registry used to store sqla columns <-> formatters association

add_formatter(sqla_col_type, formatter, key_specific=None)[source]

Add a formatter to the registry if key_specific is provided, this formatter will only be used for some specific exports

get_formatter(sqla_col, key_specific=None)[source]

sqla_inspect.csv module

sqla_inspect.excel module

Excel exportation module

class sqla_inspect.excel.SqlaXlsExporter(model)[source]

Bases: sqla_inspect.excel.XlsWriter, sqla_inspect.export.SqlaExporter

Main class used for exporting datas to the xls format

Models attributes output can be customized through the info param :

Column(Integer, infos={‘export’:
{‘excel’:<excel_specific_options>,
<main_export_options>

}

}

main_export_options and excel_specific_options can be :

label

label of the column header

format

a function that will be fired on each row to format the output

related_key

If the attribute is a relationship, the value of the given attribute of the related object will be used to fill the cells

exclude

This data will not be inserted in the export if True

Usage:

a = SqlaXlsExporter(MyModel) for i in MyModel.query().filter(<myfilter>):

a.add_row(i)

a.render()

config_key = 'excel'
class sqla_inspect.excel.XlsExporter[source]

Bases: sqla_inspect.excel.XlsWriter, sqla_inspect.export.BaseExporter

A main xls exportation tool (without sqlalchemy support)

writer = MyXlsExporter() writer.add_row({‘key’: u’La valeur de la cellule de la colonne 1’}) writer.render()

headers = ()
class sqla_inspect.excel.XlsWriter[source]

Bases: object

Class providing common tools to write excel files from tabular datas

Has to be subclassed, the subclass should provide a _datas and a headers attribute that contains the datas to render and the headers

_datas

list of tuples (each tuple is a row)

headers

list of dict containing the label of each column:
{‘label’: <a label>}
format_row(row)[source]

The render method expects rows as lists, here we switch our row format from dict to list respecting the order of the headers

render(f_buf=None)[source]

Definitely render the workbook

Parameters:f_buf (obj) – A file buffer supporting the write and seek

methods

save_book(f_buf=None)[source]

Return a file buffer containing the resulting xls

Parameters:f_buf (obj) – A file buffer supporting the write and seek

methods

set_color(cell, color)[source]

Set the given color to the provided cell

cell

A xls cell object

color

A openpyxl color var
title = u'Export'

sqla_inspect.export module

Base export class

class sqla_inspect.export.BaseExporter(**kw)[source]

A base exportation object, used to export tabular datas (csv or xls format) Should be used in conjunction with a writer

add_row(row)[source]

Add a row to our buffer

static format_row(row)[source]

Row formatter, should be implemented in subclasses

headers = []
render()[source]

Render the datas as a file buffer

set_datas(datas)[source]

bulk add rows

class sqla_inspect.export.SqlaExporter(model)[source]

Bases: sqla_inspect.export.BaseExporter, sqla_inspect.base.BaseSqlaInspector

Sqla exportation class Allow to stream datas to be exported

config_key

The key in the export subdict in the sqlalchemy Column’s info dict that is used to configure this export e.g : set config_key = “csv” if your column configuration looks like the following

Column(Integer, info={'export' :
    {'csv': {<config>}},
        <main_export_config>
    }

Note

By default, we look for the title in the colanderalchemy’s title key info={‘colanderalchemy’: {‘title’: u’Column title’}}

add_row(obj)[source]

fill a new row with the given obj

obj

instance of the exporter’s model
config_key = ''
sqla_inspect.export.format_value(column_dict, value, key=None)[source]

Format a value coming from the database (for example converts datetimes to strings)

Parameters:
  • column_dict – The column datas collected during inspection
  • value – A value coming from the database
  • key – The exportation key

sqla_inspect.py3o module

Module contents