sqla_inspect package

Submodules

sqla_inspect.ascii module

Provide common tools for string handling

sqla_inspect.ascii.camel_case_to_name(name)[source]

Used to convert a classname to a lowercase name

sqla_inspect.ascii.force_ascii(datas)[source]

Return enforced ascii string éko=>ko

sqla_inspect.ascii.force_unicode(value)[source]

return an utf-8 unicode entry

sqla_inspect.ascii.force_utf8(value)[source]

return a utf-8 string

sqla_inspect.ascii.gen_random_string(size=15)[source]

Generate random string

size

size of the resulting string
sqla_inspect.ascii.random_tag_id(size=15)[source]

Return a random string supposed to be used as tag id

sqla_inspect.ascii.to_utf8(datas)[source]

Force utf8 string entries in the given datas

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

Csv exporter for sqlalchemy datas

uses the sqlalchemy info attr to retrieve meta datas about the columns

class sqla_inspect.csv.CsvExporter[source]

Bases: sqla_inspect.csv.CsvWriter, sqla_inspect.export.BaseExporter

A common csv writer to be subclassed Set a header attribute and use it

class MyCsvExporter(CsvExporter):
headers = ({‘name’: ‘key’, ‘label’: u’Ma colonne 1’}, ...)

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

headers = ()
class sqla_inspect.csv.CsvWriter[source]

Bases: object

A base csv writer

delimiter = ';'
format_row(row)[source]

Format the row to fit our export switch the key used to store it in the dict

since csv writer is expecting dict with keys matching the headers’ names we switch the name the attributes fo the row are stored using labels instead of names

quotechar = '"'
render(f_buf=None)[source]

Write to the dest buffer

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

methods

class sqla_inspect.csv.SqlaCsvExporter(model)[source]

Bases: sqla_inspect.csv.CsvWriter, sqla_inspect.export.SqlaExporter

Main class used for exporting a SQLAlchemy model to a csv format

Models attributes output can be customized through the info param :

Column(Integer, infos={‘export’:
{ ‘csv’: <csv specific options>, <main_export_options> }

})

main_export_options and csv_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 = SqlaCsvWriter(MyModel) for i in MyModel.query().filter(<myfilter>):

a.add_row(i)

a.render()

You get a file buffer with the csv formatted datas

config_key = 'csv'

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[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

Py3o exporters

>>> model = Company.query().first()
>>> template = Template.query().first()
>>> odt_file_datas = compile_template(model, template.data_obj)
class sqla_inspect.py3o.SqlaContext(model, rels=None)[source]

Bases: sqla_inspect.base.BaseSqlaInspector

Provide a tool to build a context dict based on a given model. The datas are built following the informations retrieved from the model’s declaration.

Custom configuration can be achieved by customizing the info dict attribute from each column.

config_key

The key in the info dict we will look for.

Actually handles the following informations :
exclude : should the column be excluded from the output name : the key in the resulting dict
>>> serializer = SqlaContext(Company)
>>> company = Company.get(263)
>>> res = s.compile_obj(company)
Parameters:model – a SQLA model
collect_columns()[source]

Collect columns information from a given model.

a column info contains

the py3 informations

exclude

Should the column be excluded from the current context ?

name

the name of the key in the resulting py3o context of the column

__col__

The original column object

__prop__

In case of a relationship, the SqlaContext wrapping the given object
compile_obj(obj)[source]

generate a context based on the given obj

Parameters:obj – an instance of the model
config_key = 'py3o'
sqla_inspect.py3o.compile_template(instance, template, additionnal_context=None)[source]

Fill the given template with the instance’s datas and return the odt file

For every instance class, common values are also inserted in the context dict (and so can be used) :

  • config values
Parameters:
  • instance (obj) – the instance of a model (like Userdatas, Company)
  • template – the template object to use
  • additionnal_context (dict) – A dict containing datas we’d like to ass to

the py3o compilation template :return: a stringIO object filled with the resulting odt’s informations

sqla_inspect.py3o.format_py3o_val(value)[source]

format a value to fit py3o’s context

  • Handle linebreaks
sqla_inspect.py3o.get_compilation_context(instance)[source]

Return the compilation context for py3o templating

Build a deep dict representation of the given instance and add config values

Parameters:instance (obj) – a SQLAlchemy model instance
Returns:a multi level dict with context datas
Return type:dict

Module contents