Access Programming Interface

0.7 (Current)

csv2oerp Access Programming Interface v0.7.

class csv2oerp.api07.Openerp(host='localhost', port=8069, user='admin', pwd='admin', db='', mode='netrpc', lang=u'en_EN')[source]

An OpenERP connection interface.

create(model, vals=None, context=None)[source]

Create new record with specified value.

Parameters:
  • model (str) – The OpenERP model (res.partner)
  • vals (dict) – field values for new record, e.g {‘field_name’: field_value, ...}
  • context (dict) – context arguments, like lang, time zone
execute(model, method, *args, **kwargs)[source]

Execute a query concordely to the Openerp method signature.

Parameters:
  • model – The OpenERP model (‘res.partner’)
  • method – The model’s method (‘read’)
fields_get(model, fields=None, context=None)[source]

Get the description of list of fields.

Parameters:
  • model (str) – The OpenERP model (res.partner)
  • fields (list) – Fields to return
  • context (dict) – context arguments, like lang, time zone
read(model, ids, context=None)[source]

Read OpenERP record.

Parameters:
  • model (str) – The OpenERP model (res.partner)
  • ids (list) – object id or list of object ids
  • context (dict) – context arguments, like lang, time zone
search(model, args=None, offset=0, limit=None, order=None, context=None, count=False)[source]

Search for records based on a search domain.

Parameters:
  • model (str) – The OpenERP model (res.partner)
  • args (list) – list of tuples specifying the search domain [(‘field_name’, ‘operator’, value), ...]. Pass an empty list to match all records.
  • offset (int) – optional number of results to skip in the returned values (default: 0)
  • limit (int) – optional max number of records to return (default: None)
  • order (str) – optional columns to sort by (default: self._order=id )
  • context (dict) – context arguments, like lang, time zone
write(model, ids, vals=None, context=None)[source]

Update records with given ids with the given field values.

Parameters:
  • model (str) – The OpenERP model (res.partner)
  • ids (list) – object id or list of object ids to update according to vals
  • vals (dict) – field values for new record, e.g {‘field_name’: field_value, ...}
  • context (dict) – context arguments, like lang, time zone
class csv2oerp.api07.Field(name, columns=None, default=None, callbacks=None, relation=None, required=False, readonly=False, skip=False, ignore=False, replace=False, update=False, unique=False, overwrite=True, context=None)[source]

Specify the column number (or custom value) and some special treatments from which the current model’s field will be allocated to the object’s creation.

Parameters:
  • column (int) – The column number to map to
  • field (str) – The actual field name to map to
  • callback (function) – The callback function to apply to returned value
  • search (bool) – Search for same value before object creation
  • default (object) – Default value if no value found or if custom value (column=None)
  • required (bool) – Is this field is required
  • skip (bool) – Is this field is skippable
  • ignore (bool) – Is this field is ignorable (So object creation is skipped)
  • replace (bool) – Is this field is replaceable (So it can be redifined)
  • update (bool) – Is this field have not to be updated if existing in database
  • unique (bool) – Is this field value must be unique inside current model
  • relation (Model) – Relation to link to (Model object or list of Model Object)
  • overwrite (bool) – Overwrite the field value in OpenERP (relation)
check(session, model, context=None)[source]

Check field access fr openerp server

Parameters:
  • session (Session) – The import session used for the test
  • model (Model) – The model used for the test
get_data(session, model, line)[source]

Return the value relative to the line in argument.

Parameters:
  • server (Oerp_instance) – The server to interact with
  • line (list) – The line to search for
Raises :

SkipLineException, SkipObjectException, RequiredFieldError

Returns:

object

get_metadata(session, model, context=None)[source]

Get metadatas from fields_get methods

Parameters:
  • session (Session) – The import session used for the test
  • model (Model) – The model used for the test
Returns:

dict

class csv2oerp.api07.Router(column, callback)[source]

Specify the column number and special treatments from which the current model’s field will be allocated to the object’s creation.

Parameters:
  • column (int) – The actual column number (mandatory)
  • callback (function) – The actual callback function

0.6 (Obsolete)

csv2oerp Access Programming Interface v0.6.

csv2oerp.api06.connect(*args, **kwargs)[source]

Set globals constants needed to initialize a connection to openerp

New in version 0.5.3.

Parameters:
  • host (str) – IP or DNS name of the OERP server
  • port (int) – Port number to reach
  • user (str) – Username in the OERP server
  • pwd (str) – Password of the username
  • dbname (str) – Name of the database to reach
Raises :

nothing

class csv2oerp.api06.Import_session(**kw)[source]

Main class which provides the functionnal part of the importation process.

Note

sys.argv integrated provides a command line parser.

Here are the available command line arguments:

-h, --help                  Show this kind of help message and exit
-o OFFSET, --offset=OFFSET  Offset (Usually for header omission)
-l LIMIT, --limit=LIMIT     Limit
-c, --check-mapping         Check mapping template
-v, --verbose               Verbose mode
-d, --debug
   debug mode
-q, --quiet                 Doesn't print anything to stdout
check_mapping()[source]

Check mapping for semantics errors. http://www.openerp.com/forum/topic31343.html Also check for required and readonly fields.

connect(*args, **kwargs)[source]

Set constants needed to initialize a connection to OpenERP.

Parameters:
  • host (str) – IP or DNS name of the OERP server
  • port (int) – Port number to reach
  • user (str) – Username in the OERP server
  • pwd (str) – Password of the username
  • dbname (str) – Name of the database to reach
create(model, data, search=None)[source]

Object’s automatic and abstracted creation from model

Logged public method

Parameters:
  • model (str) – Name of the OERP class model
  • data (dict) – Data dictionnary to transmit to OERP create/write method
  • search (list) – List of fields to be used for search
Returns:

int

db[source]

Return the current database name for this session.

get_index_from_value(column, value, withcast=True)[source]

Retrieve lines which have value at column

By default value will be casted by column value type overwrite it by withcast=False

Parameters:
  • column (int) – The column index
  • value (value) – The value to compare from
Returns:

int

get_line_from_index(line_num)[source]

Retrieve lines which have value at column

Parameters:line_num (value) – The index of line
Returns:tuple
get_lines_from_value(column, value)[source]

Retrieve lines which have value at column

Parameters:
  • column (int) – The column index
  • value (value) – The value to compare from
Returns:

tuple

host[source]

Return the current connection host for this session.

lang[source]

Getting current language.

lines[source]

Getting all lines from CSV parser.

Returns:list
mapping[source]

Getting columns mapping configuration.

port[source]

Return the current connection port for this session.

pwd[source]

Return the current password for this session.

server[source]

Return the current socket connection to the OpenERP server (xmlrpc).

set_input_file(filename, delimiter=', ', quotechar='"', encoding='utf8')[source]

Set the CSV file to use.

set_mapping(mapping)[source]

Columns mapping configuration.

See Creation of your Columns mapping for further details.

set_preconfigure_script(filename)[source]

Declare a parrallel script which will be called before importation.

It must implement a main method which take in params the instance of Import class, it will be able to use all facilities from Import class.

It must return a list of lines (lines already list too)

set_syslog(arg=True)[source]

Set if syslog must be used instead of a log file.

uid[source]

Return the current UID for this session.

user[source]

Return the current username for this session.

class csv2oerp.api06.Custom(value, search=False)[source]

Specify a custom value for current field.

Mapping example:

>>> mapping = {
...     'model': {
...         'field': Custom('custom', search=True),
...         }
...     }
Parameters:
  • value (type) – The value to apply.
  • search (bool) – Search for same value before create object
class csv2oerp.api06.Relation(relation, search=False, required=False, skip=False, noupdate=False, unique=False)[source]

Specify a relation field. Also declares metadatas like required, readonly attributes.

Mapping example:

>>> {
...     'model': [
...         {
...             'field': Relation('REL_custom::model', search=True),
...             },
...         ],
...     'REL_custom::model': {
...         'field': Column(1),
...         }
...     }
Parameters:
  • relation (str) – The full name of the model which has to be related to field
  • search (bool) – Search for same value before create object
  • required (bool) – Is this field is required
  • skip (bool) – Is this field is skippable
  • ignore (bool) – Is this field is ignorable (So object creation is skipped)
  • replace (bool) – Is this field is replaceable (So it can be redifined)
  • noupdate (bool) – Is this field have not to be updated if existing in database
  • unique (bool) – Is this model’s instance must be unique inside current model
class csv2oerp.api06.Custom(value, search=False)[source]

Specify a custom value for current field.

Mapping example:

>>> mapping = {
...     'model': {
...         'field': Custom('custom', search=True),
...         }
...     }
Parameters:
  • value (type) – The value to apply.
  • search (bool) – Search for same value before create object

Exceptions

exception csv2oerp.exceptions.BadFieldType(model, field, type_)[source]

Exception used when a field is a relation and not relationnal in Oerp

exception csv2oerp.exceptions.RequiredFieldError(model, field)[source]

Exception used when a column index has not be found

exception csv2oerp.exceptions.FieldDefinitionError(msg=u'')[source]

Exception used when a model is not conform

exception csv2oerp.exceptions.ModelDefinitionError(msg=u'')[source]

Exception used when a model is not conform

exception csv2oerp.exceptions.ColumnIndexError(msg=u'')[source]

Exception used when a column index has not be found

exception csv2oerp.exceptions.SkipLineException(msg=u'')[source]

Exception used when a subfunction need to skip a entire particular line

Usefull into lambda statement with field definition

exception csv2oerp.exceptions.SkipObjectException(msg=u'')[source]

Exception used when a subfunction need to skip a particular object

Usefull into lambda statement with field definition

exception csv2oerp.exceptions.FatalException(error, line, model, state, search, data, content)[source]

Exception used when a fatal error occurs

Some tools

csv2oerp.tools.clean(string, encode_from=u'utf-8')[source]

Clean a string by removing returns and double quotes.

Strip accents, and replace carriages return by space, replace double quotes by simple ones

csv2oerp.tools.clean_line(line, encode_from=u'utf-8')[source]

Encode a list in encode_from encoding

Strip accents, and replace carriages return by space, replace double quotes by simple ones

Parameters:line – A list
csv2oerp.tools.clean_lines(lines, encode_from=u'utf-8')[source]

Encode a list of list in encode_from encoding

Strip accents, and replace carriages return by space, replace double quotes by simple ones

Parameters:lines – A list of list
csv2oerp.tools.generate_code(filenames=(), header=True, mode=u'stdout', delimiter=None, quotechar=None, encoding=u'utf-8')[source]

Return a template’s skeleton code

prerequisites:

Having a columns header to the CSV file
Parameters:
  • filenames – Name tuple of filenames to generate the importation code from
  • header (bool) – First line column’s title or not
  • stdout (str) – Printing mode (stdout, file)
csv2oerp.tools.get_line_from_table(csv_file, value, column, separator=u', ', quote=u'"', header=True)[source]

Search for an equivalence between value from column col_referer and value value, and return value from column col_return index at the same line as matching pair col_referer values.

Parameters:
  • csv_file (str) – The name of the CSV file to search into
  • value (type) – The value to test
  • col_referer (int) – The number of the column to compare to value
  • col_return (int) – he number of the column to return if matches
  • separator (str) – The CSV separator
  • quote (str) – The CSV quote
  • header (bool) – CSV file has header
Returns:

list

csv2oerp.tools.purge_stats()[source]

Re-initialize objects activities resume

csv2oerp.tools.search_csv(csv_file, value, col_referer=0, col_return=1, separator=', ', quote='"', header=True)[source]

Search for an equivalence between value from column col_referer and value value, and return value from column col_return index at the same line as matching pair col_referer values.

Parameters:
  • csv_file (str) – The name of the CSV file to search into
  • value (type) – The value to test
  • col_referer (int) – The number of the column to compare to value
  • col_return (int) – he number of the column to return if matches
  • separator (str) – The CSV separator
  • quote (str) – The CSV quote
  • header (bool) – CSV file has header
Returns:

list

csv2oerp.tools.show_stats()[source]

Show model’s activities resume. At the end of an import for example.

csv2oerp.tools.strip_accents(string, normalize=u'NFD', category=u'Mn')[source]

Replace accents from string with non accented char

Table Of Contents

Previous topic

How to

Next topic

Command line Interface

This Page