| Home | Trees | Indices | Help |
|
|---|
|
|
This module provides tools to import tabular data.
Example of use (run this with `cubicweb-ctl shell instance import-script.py`):
.. sourcecode:: python
from cubicweb.devtools.dataimport import *
# define data generators
GENERATORS = []
USERS = [('Prenom', 'firstname', ()),
('Nom', 'surname', ()),
('Identifiant', 'login', ()),
]
def gen_users(ctl):
for row in ctl.iter_and_commit('utilisateurs'):
entity = mk_entity(row, USERS)
entity['upassword'] = u'motdepasse'
ctl.check('login', entity['login'], None)
ctl.store.add('CWUser', entity)
email = {'address': row['email']}
ctl.store.add('EmailAddress', email)
ctl.store.relate(entity['eid'], 'use_email', email['eid'])
ctl.store.rql('SET U in_group G WHERE G name "users", U eid %(x)s', {'x':entity['eid']})
CHK = [('login', check_doubles, 'Utilisateurs Login',
'Deux utilisateurs ne devraient pas avoir le même login.'),
]
GENERATORS.append( (gen_users, CHK) )
# create controller
if 'cnx' in globals():
ctl = CWImportController(RQLObjectStore(cnx))
else:
print 'debug mode (not connected)'
print 'run through cubicweb-ctl shell to access an instance'
ctl = CWImportController(ObjectStore())
ctl.askerror = 1
ctl.generators = GENERATORS
ctl.data['utilisateurs'] = lazytable(ucsvreader(open('users.csv')))
# run
ctl.run()
.. BUG file with one column are not parsable
.. TODO rollback() invocation is not possible yet
|
|||
|
catch_error Helper for @contextmanager decorator. |
|||
|
ObjectStore Store objects in memory for faster validation (development mode) |
|||
|
RQLObjectStore ObjectStore that works with an actual RQL repository (production mode) |
|||
|
CWImportController Controller of the data import process. |
|||
|
NoHookRQLObjectStore ObjectStore that works with an actual RQL repository (production mode) |
|||
| MetaGenerator | |||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
Return a dict made from sanitized mapped values. ValueError can be raised on unexpected values found in checkers >>> row = {'myname': u'dupont'} >>> map = [('myname', u'name', (call_transform_method('title'),))] >>> mk_entity(row, map) {'name': u'Dupont'} >>> row = {'myname': u'dupont', 'optname': u''} >>> map = [('myname', u'name', (call_transform_method('title'),)), ... ('optname', u'MARKER', (optional,))] >>> mk_entity(row, map) {'name': u'Dupont', 'optname': None} |
checker to filter optional field If value is undefined (ex: empty string), return None that will break the checkers validation chain General use is to add 'optional' check in first condition to avoid ValueError by further checkers >>> MAPPER = [(u'value', 'value', (optional, int))] >>> row = {'value': u'XXX'} >>> mk_entity(row, MAPPER) {'value': None} >>> row = {'value': u'100'} >>> mk_entity(row, MAPPER) {'value': 100} |
raise ValueError if value is empty This check should be often found in last position in the chain. |
return a transformation function to turn string input value into a
Follow it by |
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Wed Oct 13 19:49:23 2010 | http://epydoc.sourceforge.net |