17 # You should have received a copy of the GNU Lesser General Public License along |
17 # You should have received a copy of the GNU Lesser General Public License along |
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
19 """This module provides tools to import tabular data. |
19 """This module provides tools to import tabular data. |
20 |
20 |
21 |
21 |
22 |
|
23 Example of use (run this with `cubicweb-ctl shell instance import-script.py`): |
22 Example of use (run this with `cubicweb-ctl shell instance import-script.py`): |
24 |
23 |
25 .. sourcecode:: python |
24 .. sourcecode:: python |
26 |
25 |
27 from cubicweb.devtools.dataimport import * |
26 from cubicweb.dataimport import * |
28 # define data generators |
27 # define data generators |
29 GENERATORS = [] |
28 GENERATORS = [] |
30 |
29 |
31 USERS = [('Prenom', 'firstname', ()), |
30 USERS = [('Prenom', 'firstname', ()), |
32 ('Nom', 'surname', ()), |
31 ('Nom', 'surname', ()), |
34 ] |
33 ] |
35 |
34 |
36 def gen_users(ctl): |
35 def gen_users(ctl): |
37 for row in ctl.iter_and_commit('utilisateurs'): |
36 for row in ctl.iter_and_commit('utilisateurs'): |
38 entity = mk_entity(row, USERS) |
37 entity = mk_entity(row, USERS) |
39 entity['upassword'] = u'motdepasse' |
38 entity['upassword'] = 'motdepasse' |
40 ctl.check('login', entity['login'], None) |
39 ctl.check('login', entity['login'], None) |
41 ctl.store.add('CWUser', entity) |
40 entity = ctl.store.create_entity('CWUser', **entity) |
42 email = {'address': row['email']} |
41 email = ctl.store.create_entity('EmailAddress', address=row['email']) |
43 ctl.store.add('EmailAddress', email) |
42 ctl.store.relate(entity.eid, 'use_email', email.eid) |
44 ctl.store.relate(entity['eid'], 'use_email', email['eid']) |
|
45 ctl.store.rql('SET U in_group G WHERE G name "users", U eid %(x)s', {'x':entity['eid']}) |
43 ctl.store.rql('SET U in_group G WHERE G name "users", U eid %(x)s', {'x':entity['eid']}) |
46 |
44 |
47 CHK = [('login', check_doubles, 'Utilisateurs Login', |
45 CHK = [('login', check_doubles, 'Utilisateurs Login', |
48 'Deux utilisateurs ne devraient pas avoir le même login.'), |
46 'Deux utilisateurs ne devraient pas avoir le même login.'), |
49 ] |
47 ] |
72 import csv |
70 import csv |
73 import traceback |
71 import traceback |
74 import os.path as osp |
72 import os.path as osp |
75 from StringIO import StringIO |
73 from StringIO import StringIO |
76 from copy import copy |
74 from copy import copy |
|
75 from datetime import datetime |
77 |
76 |
78 from logilab.common import shellutils |
77 from logilab.common import shellutils |
79 from logilab.common.date import strptime |
78 from logilab.common.date import strptime |
80 from logilab.common.decorators import cached |
79 from logilab.common.decorators import cached |
81 from logilab.common.deprecation import deprecated |
80 from logilab.common.deprecation import deprecated |
82 |
81 |
|
82 from cubicweb.schema import META_RTYPES, VIRTUAL_RTYPES |
83 from cubicweb.server.utils import eschema_eid |
83 from cubicweb.server.utils import eschema_eid |
84 from cubicweb.server.edition import EditedEntity |
84 from cubicweb.server.edition import EditedEntity |
|
85 |
85 |
86 |
86 def count_lines(stream_or_filename): |
87 def count_lines(stream_or_filename): |
87 if isinstance(stream_or_filename, basestring): |
88 if isinstance(stream_or_filename, basestring): |
88 f = open(stream_or_filename) |
89 f = open(stream_or_filename) |
89 else: |
90 else: |
285 """Store objects in memory for *faster* validation (development mode) |
285 """Store objects in memory for *faster* validation (development mode) |
286 |
286 |
287 But it will not enforce the constraints of the schema and hence will miss some problems |
287 But it will not enforce the constraints of the schema and hence will miss some problems |
288 |
288 |
289 >>> store = ObjectStore() |
289 >>> store = ObjectStore() |
290 >>> user = {'login': 'johndoe'} |
290 >>> user = store.create_entity('CWUser', login=u'johndoe') |
291 >>> store.add('CWUser', user) |
291 >>> group = store.create_entity('CWUser', name=u'unknown') |
292 >>> group = {'name': 'unknown'} |
292 >>> store.relate(user.eid, 'in_group', group.eid) |
293 >>> store.add('CWUser', group) |
|
294 >>> store.relate(user['eid'], 'in_group', group['eid']) |
|
295 """ |
293 """ |
296 def __init__(self): |
294 def __init__(self): |
297 self.items = [] |
295 self.items = [] |
298 self.eids = {} |
296 self.eids = {} |
299 self.types = {} |
297 self.types = {} |
592 return callfunc_every(self.store.commit, |
590 return callfunc_every(self.store.commit, |
593 self.commitevery, |
591 self.commitevery, |
594 self.get_data(datakey)) |
592 self.get_data(datakey)) |
595 |
593 |
596 |
594 |
597 |
|
598 from datetime import datetime |
|
599 from cubicweb.schema import META_RTYPES, VIRTUAL_RTYPES |
|
600 |
|
601 |
|
602 class NoHookRQLObjectStore(RQLObjectStore): |
595 class NoHookRQLObjectStore(RQLObjectStore): |
603 """ObjectStore that works with an actual RQL repository (production mode)""" |
596 """ObjectStore that works with an actual RQL repository (production mode)""" |
604 _rql = None # bw compat |
597 _rql = None # bw compat |
605 |
598 |
606 def __init__(self, session, metagen=None, baseurl=None): |
599 def __init__(self, session, metagen=None, baseurl=None): |