157 def synchronize(self, mtime=None): |
157 def synchronize(self, mtime=None): |
158 """synchronize content known by this repository with content in the |
158 """synchronize content known by this repository with content in the |
159 external repository |
159 external repository |
160 """ |
160 """ |
161 self.info('synchronizing pyro source %s', self.uri) |
161 self.info('synchronizing pyro source %s', self.uri) |
162 extrepo = self.get_connection()._repo |
162 cnx = self.get_connection() |
|
163 extrepo = cnx._repo |
163 etypes = self.support_entities.keys() |
164 etypes = self.support_entities.keys() |
164 if mtime is None: |
165 if mtime is None: |
165 mtime = self.last_update_time() |
166 mtime = self.last_update_time() |
166 updatetime, modified, deleted = extrepo.entities_modified_since(etypes, |
167 updatetime, modified, deleted = extrepo.entities_modified_since(etypes, |
167 mtime) |
168 mtime) |
168 repo = self.repo |
169 repo = self.repo |
169 session = repo.internal_session() |
170 session = repo.internal_session() |
170 try: |
171 try: |
171 for etype, extid in modified: |
172 for etype, extid in modified: |
172 try: |
173 try: |
173 eid = self.extid2eid(extid, etype, session) |
174 exturi = cnx.describe(extid)[1] |
174 rset = session.eid_rset(eid, etype) |
175 if exturi == 'system' or not exturi in repo.sources_by_uri: |
175 entity = rset.get_entity(0, 0) |
176 eid = self.extid2eid(extid, etype, session) |
176 entity.complete(entity.e_schema.indexable_attributes()) |
177 rset = session.eid_rset(eid, etype) |
177 repo.index_entity(session, entity) |
178 entity = rset.get_entity(0, 0) |
|
179 entity.complete(entity.e_schema.indexable_attributes()) |
|
180 repo.index_entity(session, entity) |
178 except: |
181 except: |
179 self.exception('while updating %s with external id %s of source %s', |
182 self.exception('while updating %s with external id %s of source %s', |
180 etype, extid, self.uri) |
183 etype, extid, self.uri) |
181 continue |
184 continue |
182 for etype, extid in deleted: |
185 for etype, extid in deleted: |
275 session.set_shared_data('sources_error', msg % self.uri) |
278 session.set_shared_data('sources_error', msg % self.uri) |
276 return [] |
279 return [] |
277 descr = rset.description |
280 descr = rset.description |
278 if rset: |
281 if rset: |
279 needtranslation = [] |
282 needtranslation = [] |
|
283 rows = rset.rows |
280 for i, etype in enumerate(descr[0]): |
284 for i, etype in enumerate(descr[0]): |
281 if (etype is None or not self.schema.eschema(etype).is_final() or |
285 if (etype is None or not self.schema.eschema(etype).is_final() or |
282 getattr(union.locate_subquery(i, etype, args).selection[i], 'uidtype', None)): |
286 getattr(union.locate_subquery(i, etype, args).selection[i], 'uidtype', None)): |
283 needtranslation.append(i) |
287 needtranslation.append(i) |
284 if needtranslation: |
288 if needtranslation: |
285 for rowindex, row in enumerate(rset): |
289 cnx = session.pool.connection(self.uri) |
|
290 for rowindex in xrange(rset.rowcount - 1, -1, -1): |
|
291 row = rows[rowindex] |
286 for colindex in needtranslation: |
292 for colindex in needtranslation: |
287 if row[colindex] is not None: # optional variable |
293 if row[colindex] is not None: # optional variable |
288 etype = descr[rowindex][colindex] |
294 etype = descr[rowindex][colindex] |
289 eid = self.extid2eid(row[colindex], etype, session) |
295 exttype, exturi, extid = cnx.describe(row[colindex]) |
290 row[colindex] = eid |
296 if exturi == 'system' or not exturi in self.repo.sources_by_uri: |
291 results = rset.rows |
297 eid = self.extid2eid(row[colindex], etype, session) |
|
298 row[colindex] = eid |
|
299 else: |
|
300 # skip this row |
|
301 print 'skip external', etype, exturi, extid, self.uri |
|
302 del rows[rowindex] |
|
303 del descr[rowindex] |
|
304 break |
|
305 results = rows |
292 else: |
306 else: |
293 results = [] |
307 results = [] |
294 if server.DEBUG: |
308 if server.DEBUG: |
295 if len(results)>10: |
309 if len(results)>10: |
296 print '--------------->', results[:10], '...', len(results) |
310 print '--------------->', results[:10], '...', len(results) |