Provide common tools for string handling
Used to convert a classname to a lowercase name
Generate random string
size
size of the resulting string
utilities to inspect Sqlalchemy models
Bases: dict
A registry used to store sqla columns <-> formatters association
Csv exporter for sqlalchemy datas
uses the sqlalchemy info attr to retrieve meta datas about the columns
Bases: sqla_inspect.csv.CsvWriter, sqla_inspect.export.BaseExporter
A common csv writer to be subclassed Set a header attribute and use it
writer = MyCsvExporter() writer.add_row({‘key’: u’La valeur de la cellule de la colonne 1’}) writer.render()
Bases: object
A base csv writer
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
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 headerformat
a function that will be fired on each row to format the outputrelated_key
If the attribute is a relationship, the value of the given attribute of the related object will be used to fill the cellsexclude
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
Excel exportation module
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 headerformat
a function that will be fired on each row to format the outputrelated_key
If the attribute is a relationship, the value of the given attribute of the related object will be used to fill the cellsexclude
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()
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()
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>}
The render method expects rows as lists, here we switch our row format from dict to list respecting the order of the headers
Definitely render the workbook
Parameters: | f_buf (obj) – A file buffer supporting the write and seek |
---|
methods
Return a file buffer containing the resulting xls
Parameters: | f_buf (obj) – A file buffer supporting the write and seek |
---|
methods
Set the given color to the provided cell
cell
A xls cell objectcolor
A openpyxl color var
Base export class
A base exportation object, used to export tabular datas (csv or xls format) Should be used in conjunction with a writer
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’}}
Py3o exporters
>>> model = Company.query().first()
>>> template = Template.query().first()
>>> odt_file_datas = compile_template(model, template.data_obj)
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 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
generate a context based on the given obj
Parameters: | obj – an instance of the model |
---|
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: |
|
---|
the py3o compilation template :return: a stringIO object filled with the resulting odt’s informations
format a value to fit py3o’s context
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 |