Builtin Bloks

Blok anyblok-io

class anyblok_io.bloks.io.AnyBlokIO(registry)[source]

Bases: anyblok.blok.Blok

In / Out tool’s:

  • Formater: convert value 2 str or str 2 value in function of the field,
  • Importer: main model to define an import,
  • Exporter: main model to define an export,
name = 'anyblok-io'
version = '1.3.0'
author = 'Suzanne Jean-Sébastien'
autoinstall = False
priority = 100

Note

Require the anyblok-io blok

Mapping

Model.IO.Mapping allows to link a Model instance by a Model namesapce and str key. this key is an external ID

Save an instance with a key:

Blok = self.registry.System.Blok
blok = Blok.query().filter(Blok.name == 'anyblok-core').first()
self.registry.IO.Mapping.set('External ID', blok)

Warning

By default if you save another instance with the same key and the same model, an IOMapingSetException will be raised. Il really you want this mapping you must call the set method with the named argument raiseifexist=False:

self.registry.IO.Mapping.set('External ID', blok, raiseifexist=False)

Get an entry in the mapping:

blok2 = self.registry.IO.Mapping.get('Model.System.Blok', 'External ID')
assert blok2 is blok

Formater

The goal of the formater is to get:

  • value from string
  • value from mapping key
  • string from value
  • mapping key from value

The value is the value of the field.

Warning

The relation ships are particulare cases. The value is the json of the primary keys. The Many2Many and the One2Many are the json of the list of the primary keys

Exporter

The Model.IO.Exporter export some entries in fonction of configuration. anyblok-io blok doesn’t give complete exporter, just the base Model to standardize all the possible export:

exporter = registry.IO.Exporter.insert(...)  # create a exporter
entries = ...  # entries are instance of model
fp = exporter.run(entries)
# fp is un handler on the opened file (StringIO)

Importer

The Model.IO.Importer import some entries in fonction of configuration. anyblok-io blok doesn’t give complete importer, just the base Model to standardize all the possible import:

importer = registry.IO.Importer.insert(...)  # create an importer
# the file to import are filled in the parameter
entries = importer.run()

API doc

exceptions

exception anyblok_io.bloks.io.exceptions.IOException[source]

Bases: Exception

IO exception

exception anyblok_io.bloks.io.exceptions.IOMappingCheckException[source]

Bases: anyblok_io.bloks.io.exceptions.IOException

IO Exception for setter

exception anyblok_io.bloks.io.exceptions.IOMappingSetException[source]

Bases: anyblok_io.bloks.io.exceptions.IOException

IO Exception for setter

exception anyblok_io.bloks.io.exceptions.ImporterException[source]

Bases: Exception

Simple Exception for importer

exception anyblok_io.bloks.io.exceptions.ExporterException[source]

Bases: Exception

Simple Exception for exporter

exception anyblok_io.bloks.io.exceptions.FormaterException[source]

Bases: Exception

Simple Exception for importer

core

class anyblok_io.bloks.io.core.Query[source]

Bases: object

AnyBlok registration:

  • Type: Core
  • Registry name: Core.Query
delete(*args, **kwargs)[source]

Inherit the Query.delete methods.:: Model.query().delete(remove_mapping=True)

Parameters:remove_mapping – boolean, if check (default) the mapping is

removed

class anyblok_io.bloks.io.core.SqlBase[source]

Bases: object

AnyBlok registration:

  • Type: Core
  • Registry name: Core.SqlBase
delete(*args, **kwargs)[source]

Inherit the Model.delete methods.:

instance.delete(remove_mapping=True)
Parameters:remove_mapping – boolean, if check (default) the mapping is removed

mapping

class anyblok_io.bloks.io.mapping.Mapping[source]

Bases: object

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Mapping
  • Tablename: io_mapping
Fields  
key
  • Type - anyblok.column.String
  • primary_key - True
  • default - anyblok.column.NoDefaultValue
  • size - 64
model
  • Type - anyblok.column.String
  • primary_key - True
  • foreign_key - Model.System.Model => name
  • default - anyblok.column.NoDefaultValue
  • size - 256
primary_key
  • Type - anyblok.column.Json
  • nullable - False
  • default - anyblok.column.NoDefaultValue
blokname
  • Type - anyblok.column.String
  • Label - 'Blok name'
  • foreign_key - Model.System.Blok => name
  • default - anyblok.column.NoDefaultValue
  • size - 64
classmethod check_primary_keys(model, *pks)[source]

check if the all the primary keys match with primary keys of the model

Parameters:
  • model – model to check
  • pks – list of the primary keys to check
Exception:

IOMappingCheckException

classmethod clean(bloknames=None, models=None)[source]

Clean all mapping with removed object linked:

Mapping.clean(bloknames=['My blok'])

Warning

For filter only the no blokname:

Mapping.clean(bloknames=[None])
Params bloknames:
 filter by blok, keep the order to remove the mapping
Params models:filter by model, keep the order to remove the mapping
classmethod delete(model, key, mapping_only=True, byquery=False)[source]

Delete the key for this model

Parameters:
  • model – model of the mapping
  • key – string of the key
Return type:

Boolean True if the mapping is removed

classmethod delete_for_blokname(blokname, models=None, byquery=False)[source]

Clean all mapping with removed object linked:

Mapping.clean('My blok')

Warning

For filter only the no blokname:

Mapping.clean(None)
Params blokname:
 filter by blok
Params models:filter by model, keep the order to remove the mapping
filter_by_model_and_key(model, key)[source]

SQLAlechemy hybrid method to filter by model and key

Parameters:
  • model – model of the mapping
  • key – external key of the mapping

Hybrid method

filter_by_model_and_keys(model, *keys)[source]

SQLAlechemy hybrid method to filter by model and key

Parameters:
  • model – model of the mapping
  • key – external key of the mapping

Hybrid method

classmethod get(model, key)[source]

return instance of the model with this external key

Parameters:
  • model – model of the mapping
  • key – string of the key
Return type:

instance of the model

classmethod get_mapping_primary_keys(model, key)[source]

return primary key for a model and an external key

Parameters:
  • model – model of the mapping
  • key – string of the key
Return type:

dict primary key: value or None

classmethod multi_delete(model, *keys, **kwargs)[source]

Delete all the keys for this model

Parameters:
  • model – model of the mapping
  • keys – list of the key
Return type:

Boolean True if the mappings are removed

classmethod set(key, instance, raiseifexist=True, blokname=None)[source]

Add or update a mmping with a model and a external key

Parameters:
  • model – model to check
  • key – string of the key
  • instance – instance of the model to save
  • raiseifexist – boolean (True by default), if True and the entry exist then an exception is raised
  • blokname – name of the blok where come from the mapping
Exception:

IOMappingSetException

classmethod set_primary_keys(model, key, pks, raiseifexist=True, blokname=None)[source]

Add or update a mmping with a model and a external key

Parameters:
  • model – model to check
  • key – string of the key
  • pks – dict of the primary key to save
  • raiseifexist – boolean (True by default), if True and the entry exist then an exception is raised
  • blokname – name of the blok where come from the mapping
Exception:

IOMappingSetException

mixin

class anyblok_io.bloks.io.mixin.IOMixin[source]

Bases: object

AnyBlok registration:

  • Type: Mixin
  • Registry name: Mixin.IOMixin
Fields  
id
  • Type - anyblok.column.Integer
  • primary_key - True
  • autoincrement - True
  • default - anyblok.column.NoDefaultValue
mode
  • Type - anyblok.column.Selection
  • nullable - False
  • default - anyblok.column.NoDefaultValue
  • selections - 'get_mode_choices'
  • size - 64
model
  • Type - anyblok.column.String
  • nullable - False
  • foreign_key - Model.System.Model => name
  • default - anyblok.column.NoDefaultValue
  • size - 256

importer

class anyblok_io.bloks.io.importer.Importer[source]

Bases: anyblok.mixin.IOMixin

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Importer
  • Tablename: io_importer
  • Inherited Models or Mixins:
    • anyblok.mixin.IOMixin
Fields  
file_to_import
  • Type - anyblok.column.LargeBinary
  • nullable - False
  • default - anyblok.column.NoDefaultValue
offset
  • Type - anyblok.column.Integer
  • default - 0
nb_grouped_lines
  • Type - anyblok.column.Integer
  • nullable - False
  • default - 50
commit_at_each_grouped
  • Type - anyblok.column.Boolean
  • default - True
check_import
  • Type - anyblok.column.Boolean
  • default - False

exporter

class anyblok_io.bloks.io.exporter.Exporter[source]

Bases: anyblok.mixin.IOMixin

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Exporter
  • Tablename: io_exporter
  • Inherited Models or Mixins:
    • anyblok.mixin.IOMixin

formater

class anyblok_io.bloks.io.formater.Formater[source]

Bases: object

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater
  • Tablename: io_formater
class anyblok_io.bloks.io.formater.Float[source]

Bases: anyblok.model.Formater

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.Float
  • Tablename: io_formater_float
  • Inherited Models or Mixins:
    • anyblok.model.Formater
class anyblok_io.bloks.io.formater.Decimal[source]

Bases: anyblok.model.Formater

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.Decimal
  • Tablename: io_formater_decimal
  • Inherited Models or Mixins:
    • anyblok.model.Formater
class anyblok_io.bloks.io.formater.Json[source]

Bases: anyblok.model.Formater

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.Json
  • Tablename: io_formater_json
  • Inherited Models or Mixins:
    • anyblok.model.Formater
class anyblok_io.bloks.io.formater.Interval[source]

Bases: anyblok.model.Formater

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.Interval
  • Tablename: io_formater_interval
  • Inherited Models or Mixins:
    • anyblok.model.Formater
class anyblok_io.bloks.io.formater.Integer[source]

Bases: anyblok.model.Formater

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.Integer
  • Tablename: io_formater_integer
  • Inherited Models or Mixins:
    • anyblok.model.Formater
class anyblok_io.bloks.io.formater.BigInteger[source]

Bases: anyblok.model.Integer

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.BigInteger
  • Tablename: io_formater_biginteger
  • Inherited Models or Mixins:
    • anyblok.model.Integer
class anyblok_io.bloks.io.formater.Boolean[source]

Bases: anyblok.model.Formater

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.Boolean
  • Tablename: io_formater_boolean
  • Inherited Models or Mixins:
    • anyblok.model.Formater
class anyblok_io.bloks.io.formater.Time[source]

Bases: anyblok.model.Formater

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.Time
  • Tablename: io_formater_time
  • Inherited Models or Mixins:
    • anyblok.model.Formater
class anyblok_io.bloks.io.formater.Date[source]

Bases: anyblok.model.Formater

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.Date
  • Tablename: io_formater_date
  • Inherited Models or Mixins:
    • anyblok.model.Formater
class anyblok_io.bloks.io.formater.DateTime[source]

Bases: anyblok.model.Formater

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.DateTime
  • Tablename: io_formater_datetime
  • Inherited Models or Mixins:
    • anyblok.model.Formater
class anyblok_io.bloks.io.formater.Many2One[source]

Bases: anyblok.model.Formater

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.Many2One
  • Tablename: io_formater_many2one
  • Inherited Models or Mixins:
    • anyblok.model.Formater
class anyblok_io.bloks.io.formater.One2One[source]

Bases: anyblok.model.Many2One

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.One2One
  • Tablename: io_formater_one2one
  • Inherited Models or Mixins:
    • anyblok.model.Many2One
class anyblok_io.bloks.io.formater.Many2Many[source]

Bases: anyblok.model.Formater

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.Many2Many
  • Tablename: io_formater_many2many
  • Inherited Models or Mixins:
    • anyblok.model.Formater
class anyblok_io.bloks.io.formater.One2Many[source]

Bases: anyblok.model.Many2Many

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Formater.One2Many
  • Tablename: io_formater_one2many
  • Inherited Models or Mixins:
    • anyblok.model.Many2Many

Blok anyblok-io-csv

class anyblok_io.bloks.io_csv.AnyBlokIOCSV(registry)[source]

Bases: anyblok.blok.Blok

CSV Importer / Exporter behaviour

name = 'anyblok-io-csv'
version = '1.3.0'
author = 'Suzanne Jean-Sébastien'
autoinstall = False
priority = 100

Note

Require the anyblok-io-csv blok

Exporter

Add an exporter mode (CSV) in AnyBlok:

Exporter = registry.IO.Exporter.CSV

Define what export:

csv_delimiter = ','
csv_quotechar = '"'
model = ``Existing model name``
fields = [
    {'name': 'field name'},
    {'name': 'fieldname1.fieldname2. ... .fieldnamen}  #  fieldname1, fieldname 2 must be Many2One or have foreign keys
    {'name': 'field', model="external_id"}  # field must be Many2One or foreign_keys or primary keys
    ...
]

Create the Exporter:

exporter = Exporter.insert(csv_delimiter=csv_delimiter,
                           csv_quotechar=csv_quotechar,
                           model=model,
                           fields=fields)

Warning

You can also make insert with registry.IO.Exporter directly

Run the export:

fp = exporter.run(entries)  # entries are instance of the ``model``

Importer

Add an importer mode (CSV) in AnyBlok:

Importer = registry.IO.Importer.CSV

Define what import:

csv_delimiter = ','
csv_quotechar = '"'
model = ``Existing model name``
with open(..., 'rb') as fp:
    file_to_import = fp.read()

Create the Importer:

importer = Importer.insert(csv_delimiter=csv_delimiter,
                           csv_quotechar=csv_quotechar,
                           model=model,
                           file_to_import=file_to_import)

Warning

You can also make insert with registry.IO.Importer directly

Run the import:

res = importer.run()

The result is a dict with:

  • error_found: List the error, durring the import
  • created_entries: Entries created by the import
  • updated_entries: Entries updated by the import

List of the options for the import:

  • csv_on_error:
    • raise_now: Raise now
    • raise_at_the_end (default): Raise at the end
    • ignore: Ignore and continue
  • csv_if_exist:
    • pass: Pass to the next record
    • overwrite (default): Update the record
    • create: Create another record
    • raise: Raise an exception
  • csv_if_does_not_exist:
    • pass: Pass to the next record
    • create (default): Create another record
    • raise: Raise an exception

API doc

exceptions

exception anyblok_io.bloks.io_csv.exceptions.CSVImporterException[source]

Bases: anyblok_io.bloks.io.exceptions.ImporterException

Simple exception for CSV importer

exception anyblok_io.bloks.io_csv.exceptions.CSVExporterException[source]

Bases: anyblok_io.bloks.io.exceptions.ExporterException

Simple exception for CSV exporter

mixin

class anyblok_io.bloks.io_csv.mixin.IOCSVFieldMixin[source]

Bases: object

AnyBlok registration:

  • Type: Mixin
  • Registry name: Mixin.IOCSVFieldMixin
Fields  
id
  • Type - anyblok.column.Integer
  • primary_key - True
  • autoincrement - True
  • default - anyblok.column.NoDefaultValue
name
  • Type - anyblok.column.String
  • nullable - False
  • default - anyblok.column.NoDefaultValue
  • size - 64
class anyblok_io.bloks.io_csv.mixin.IOCSVMixin[source]

Bases: object

AnyBlok registration:

  • Type: Mixin
  • Registry name: Mixin.IOCSVMixin
Fields  
csv_delimiter
  • Type - anyblok.column.String
  • nullable - False
  • default - ','
  • size - 64
csv_quotechar
  • Type - anyblok.column.String
  • nullable - False
  • default - '"'
  • size - 64

importer

class anyblok_io.bloks.io_csv.importer.Importer[source]

Bases: anyblok.mixin.IOCSVMixin

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Importer
  • Tablename: io_importer
  • Inherited Models or Mixins:
    • anyblok.mixin.IOCSVMixin
Fields  
csv_on_error
  • Type - anyblok.column.Selection
  • default - 'raise_at_the_end'
  • size - 64
csv_if_exist
  • Type - anyblok.column.Selection
  • default - 'overwrite'
  • size - 64
csv_if_does_not_exist
  • Type - anyblok.column.Selection
  • default - 'create'
  • size - 64
class anyblok_io.bloks.io_csv.importer.CSV(importer, blokname=None)[source]

Bases: object

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Importer.CSV
  • Tablename: io_importer_csv

exporter

class anyblok_io.bloks.io_csv.exporter.Exporter[source]

Bases: anyblok.mixin.IOCSVMixin

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Exporter
  • Tablename: io_exporter
  • Inherited Models or Mixins:
    • anyblok.mixin.IOCSVMixin
class anyblok_io.bloks.io_csv.exporter.Field[source]

Bases: anyblok.mixin.IOCSVFieldMixin

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Exporter.Field
  • Tablename: io_exporter_field
  • Inherited Models or Mixins:
    • anyblok.mixin.IOCSVFieldMixin
Fields  
exporter
  • Type - anyblok.relationship.Many2One
  • backref - 'fields_to_export'
  • model - Model.IO.Exporter
  • index - False
mode
  • Type - anyblok.column.Selection
  • nullable - False
  • default - 'any'
  • selections - 'get_selection'
  • size - 64
mapping
  • Type - anyblok.column.String
  • default - anyblok.column.NoDefaultValue
  • size - 64
class anyblok_io.bloks.io_csv.exporter.CSV(exporter)[source]

Bases: object

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Exporter.CSV
  • Tablename: io_exporter_csv

Blok anyblok-io-xml

class anyblok_io.bloks.io_xml.AnyBlokIOXML(registry)[source]

Bases: anyblok.blok.Blok

XML Importer / Exporter behaviour

Warning

Importer and Exporter are not implemented yet

name = 'anyblok-io-xml'
version = '1.3.0'
author = 'Suzanne Jean-Sébastien'
autoinstall = False
priority = 100

Note

Require the anyblok-io-xml blok

Exporter

TODO

Importer

Add an importer mode (XML) in AnyBlok:

Importer = registry.IO.Importer.XML

Define what import:

model = ``Existing model name``
with open(..., 'rb') as fp:
    file_to_import = fp.read()

Create the Importer:

importer = Importer.insert(model=model,
                           file_to_import=file_to_import)

Warning

You can also make insert with registry.IO.Importer directly

Run the import:

res = importer.run()

The result is a dict with:

  • error_found: List the error, durring the import
  • created_entries: Entries created by the import
  • updated_entries: Entries updated by the import

Root structure of the XML file:

<records on_error="...">
    ...
</records>

raise can have the value:

  • raise (dafault)
  • ignore

records node can have:

  • commit: commit the import, only if no error found
  • record: import one record

Add a record:

<records>
    <record>
        ...
        <field name="..." />
        ...
    </record>
</records>

Record attribute:

  • model: if not filled, then the importer will indicate the model
  • external_id: Mapping key
  • param: Mapping key only for the import (not save)
  • on_error:
    • raise
    • ignore (default)
  • if_exist:
    • overwrite (default)
    • create
    • pass: continue to the next record
    • continue: continue on the sub record without take this record
    • raise
  • id_does_not_exist:
    • create (default)
    • pass
    • raise

The field node represente a Field, a Column or RelationShip, the attribute are:

  • name (required): name of the field, column or relation ship

Case of the relation ship, they have some more attribute:

  • external_id:
  • param:
  • on_error:
    • raise
    • ignore (default)
  • if_exist:
    • overwrite (default)
    • create
    • pass: continue to the next record
    • continue: continue on the sub record without take this record
    • raise
  • id_does_not_exist:
    • create (default)
    • pass
    • raise

Many2One and One2One declaration is directly in the field node:

<records
    <record
        ...
        <field name="Many2One or One2One">
            ...
            <field name="..." />
            ...
        </field>
        ...
    </record>
</records>

One2Many and Many2Many declarations is also in the field but with a record node:

<records
    <record
        ...
        <field name="Many2Many or One2Many">
            ...
            <record>
                ...
                <field name="..." />
                ...
            </record>
            ...
        </field>
        ...
    </record>
</records>

In the case of polymorphisme you may use the attribute model on record:

<records
    <record
        ...
        <field name="Many2Many or One2Many">
            ...
            <record model="``polymophic model``">
                ...
                <field name="..." />
                ...
            </record>
            <record model="``another polymophic model``">
                ...
                <field name="..." />
                ...
            </record>
            ...
        </field>
        ...
    </record>
</records>

API doc

exceptions

exception anyblok_io.bloks.io_xml.exceptions.XMLImporterException[source]

Bases: anyblok_io.bloks.io.exceptions.ImporterException

Simple exception for XML importer

exception anyblok_io.bloks.io_xml.exceptions.XMLExporterException[source]

Bases: anyblok_io.bloks.io.exceptions.ExporterException

Simple exception for XML exporter

importer

class anyblok_io.bloks.io_xml.importer.Importer[source]

Bases: object

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Importer
  • Tablename: io_importer
class anyblok_io.bloks.io_xml.importer.XML(importer, blokname=None)[source]

Bases: object

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Importer.XML
  • Tablename: io_importer_xml

exporter

class anyblok_io.bloks.io_xml.exporter.Exporter[source]

Bases: object

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Exporter
  • Tablename: io_exporter
class anyblok_io.bloks.io_xml.exporter.XML(exporter)[source]

Bases: object

AnyBlok registration:

  • Type: Model
  • Registry name: Model.IO.Exporter.XML
  • Tablename: io_exporter_xml