[source/native] session -> cnx

Also swap process_results arguments order to make cnx come earlier.

authorAurelien Campeas <aurelien.campeas@logilab.fr>
changesetd528feff7540
branchdefault
phasedraft
hiddenyes
parent revision#4f7e4ad3bbdf [source/native] cPickle is available in all supported pythons
child revision#f42205d393b7 [shared data] remove get/set_shared_data api
files modified by this revision
server/sources/__init__.py
server/sources/native.py
server/sqlutils.py
# HG changeset patch
# User Aurelien Campeas <aurelien.campeas@logilab.fr>
# Date 1401811423 -7200
# Tue Jun 03 18:03:43 2014 +0200
# Node ID d528feff7540dd830117dbccb92895132b194287
# Parent 4f7e4ad3bbdf1eb20ad16455a133c0e38eebefed
[source/native] session -> cnx

Also swap process_results arguments order to make cnx come earlier.

diff --git a/server/sources/__init__.py b/server/sources/__init__.py
@@ -1,6 +1,6 @@
1 -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2 +# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
3  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
4  #
5  # This file is part of CubicWeb.
6  #
7  # CubicWeb is free software: you can redistribute it and/or modify it under the
@@ -17,24 +17,20 @@
8  # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
9  """cubicweb server sources support"""
10 
11  __docformat__ = "restructuredtext en"
12 
13 -import itertools
14 -from os.path import join, splitext
15  from time import time
16 -from datetime import datetime, timedelta
17  from logging import getLogger
18 
19  from logilab.common import configuration
20  from logilab.common.deprecation import deprecated
21 
22  from yams.schema import role_name
23 
24  from cubicweb import ValidationError, set_log_methods, server
25 -from cubicweb.schema import VIRTUAL_RTYPES
26 -from cubicweb.server.sqlutils import SQL_PREFIX
27 +from cubicweb.server import SOURCE_TYPES
28  from cubicweb.server.edition import EditedEntity
29 
30 
31  def dbg_st_search(uri, union, varmap, args, cachekey=None, prefix='rql for'):
32      if server.DEBUG & server.DBG_RQL:
@@ -309,27 +305,19 @@
33          """called by the repository after an entity stored here has been
34          inserted in the system table.
35          """
36          pass
37 
38 -    def _load_mapping(self, session=None, **kwargs):
39 +    def _load_mapping(self, cnx, **kwargs):
40          if not 'CWSourceSchemaConfig' in self.schema:
41              self.warning('instance is not mapping ready')
42              return
43 -        if session is None:
44 -            _session = self.repo.internal_session()
45 -        else:
46 -            _session = session
47 -        try:
48 -            for schemacfg in _session.execute(
49 -                'Any CFG,CFGO,S WHERE '
50 -                'CFG options CFGO, CFG cw_schema S, '
51 -                'CFG cw_for_source X, X eid %(x)s', {'x': self.eid}).entities():
52 -                self.add_schema_config(schemacfg, **kwargs)
53 -        finally:
54 -            if session is None:
55 -                _session.close()
56 +        for schemacfg in cnx.execute(
57 +            'Any CFG,CFGO,S WHERE '
58 +            'CFG options CFGO, CFG cw_schema S, '
59 +            'CFG cw_for_source X, X eid %(x)s', {'x': self.eid}).entities():
60 +            self.add_schema_config(schemacfg, **kwargs)
61 
62      def add_schema_config(self, schemacfg, checkonly=False):
63          """added CWSourceSchemaConfig, modify mapping accordingly"""
64          msg = schemacfg._cw._("this source doesn't use a mapping")
65          raise ValidationError(schemacfg.eid, {None: msg})
@@ -370,95 +358,94 @@
66 
67      def get_extid(self, entity):
68          """return the external id for the given newly inserted entity"""
69          raise NotImplementedError(self)
70 
71 -    def add_entity(self, session, entity):
72 +    def add_entity(self, cnx, entity):
73          """add a new entity to the source"""
74          raise NotImplementedError(self)
75 
76 -    def update_entity(self, session, entity):
77 +    def update_entity(self, cnx, entity):
78          """update an entity in the source"""
79          raise NotImplementedError(self)
80 
81 -    def delete_entities(self, session, entities):
82 +    def delete_entities(self, cnx, entities):
83          """delete several entities from the source"""
84          for entity in entities:
85 -            self.delete_entity(session, entity)
86 +            self.delete_entity(cnx, entity)
87 
88 -    def delete_entity(self, session, entity):
89 +    def delete_entity(self, cnx, entity):
90          """delete an entity from the source"""
91          raise NotImplementedError(self)
92 
93 -    def add_relation(self, session, subject, rtype, object):
94 +    def add_relation(self, cnx, subject, rtype, object):
95          """add a relation to the source"""
96          raise NotImplementedError(self)
97 
98 -    def add_relations(self, session,  rtype, subj_obj_list):
99 +    def add_relations(self, cnx,  rtype, subj_obj_list):
100          """add a relations to the source"""
101          # override in derived classes if you feel you can
102          # optimize
103          for subject, object in subj_obj_list:
104 -            self.add_relation(session, subject, rtype, object)
105 +            self.add_relation(cnx, subject, rtype, object)
106 
107      def delete_relation(self, session, subject, rtype, object):
108          """delete a relation from the source"""
109          raise NotImplementedError(self)
110 
111      # system source interface #################################################
112 
113 -    def eid_type_source(self, session, eid):
114 +    def eid_type_source(self, cnx, eid):
115          """return a tuple (type, source, extid) for the entity with id <eid>"""
116          raise NotImplementedError(self)
117 
118 -    def create_eid(self, session):
119 +    def create_eid(self, cnx):
120          raise NotImplementedError(self)
121 
122 -    def add_info(self, session, entity, source, extid):
123 +    def add_info(self, cnx, entity, source, extid):
124          """add type and source info for an eid into the system table"""
125          raise NotImplementedError(self)
126 
127 -    def update_info(self, session, entity, need_fti_update):
128 +    def update_info(self, cnx, entity, need_fti_update):
129          """mark entity as being modified, fulltext reindex if needed"""
130          raise NotImplementedError(self)
131 
132 -    def index_entity(self, session, entity):
133 +    def index_entity(self, cnx, entity):
134          """create an operation to [re]index textual content of the given entity
135          on commit
136          """
137          raise NotImplementedError(self)
138 
139 -    def fti_unindex_entities(self, session, entities):
140 +    def fti_unindex_entities(self, cnx, entities):
141          """remove text content for entities from the full text index
142          """
143          raise NotImplementedError(self)
144 
145 -    def fti_index_entities(self, session, entities):
146 +    def fti_index_entities(self, cnx, entities):
147          """add text content of created/modified entities to the full text index
148          """
149          raise NotImplementedError(self)
150 
151      # sql system source interface #############################################
152 
153 -    def sqlexec(self, session, sql, args=None):
154 +    def sqlexec(self, cnx, sql, args=None):
155          """execute the query and return its result"""
156          raise NotImplementedError(self)
157 
158 -    def create_index(self, session, table, column, unique=False):
159 +    def create_index(self, cnx, table, column, unique=False):
160          raise NotImplementedError(self)
161 
162 -    def drop_index(self, session, table, column, unique=False):
163 +    def drop_index(self, cnx, table, column, unique=False):
164          raise NotImplementedError(self)
165 
166 
167 -    @deprecated('[3.13] use extid2eid(source, value, etype, session, **kwargs)')
168 -    def extid2eid(self, value, etype, session, **kwargs):
169 -        return self.repo.extid2eid(self, value, etype, session, **kwargs)
170 +    @deprecated('[3.13] use extid2eid(source, value, etype, cnx, **kwargs)')
171 +    def extid2eid(self, value, etype, cnx, **kwargs):
172 +        return self.repo.extid2eid(self, value, etype, cnx, **kwargs)
173 
174 
175 
176 -from cubicweb.server import SOURCE_TYPES
177 
178  def source_adapter(source_type):
179      try:
180          return SOURCE_TYPES[source_type]
181      except KeyError:
diff --git a/server/sources/native.py b/server/sources/native.py
@@ -36,11 +36,10 @@
182  import itertools
183  import zipfile
184  import logging
185  import sys
186 
187 -from logilab.common.compat import any
188  from logilab.common.decorators import cached, clear_cache
189  from logilab.common.configuration import Method
190  from logilab.common.shellutils import getlogin
191  from logilab.database import get_db_helper, sqlgen
192 
@@ -559,11 +558,11 @@
193                  self.warning("trying to reconnect")
194                  cnx.cnxset.reconnect()
195                  cursor = self.doexec(cnx, sql, args)
196              else:
197                  raise
198 -        results = self.process_result(cursor, cbs, session=cnx)
199 +        results = self.process_result(cursor, cnx, cbs)
200          assert dbg_results(results)
201          return results
202 
203      @contextmanager
204      def _storage_handler(self, entity, event):
diff --git a/server/sqlutils.py b/server/sqlutils.py
@@ -365,29 +365,29 @@
205              # should not collide
206              newargs.update(query_args)
207              return newargs
208          return query_args
209 
210 -    def process_result(self, cursor, column_callbacks=None, session=None):
211 +    def process_result(self, cursor, cnx=None, column_callbacks=None):
212          """return a list of CubicWeb compliant values from data in the given cursor
213          """
214 -        return list(self.iter_process_result(cursor, column_callbacks, session))
215 +        return list(self.iter_process_result(cursor, cnx, column_callbacks))
216 
217 -    def iter_process_result(self, cursor, column_callbacks=None, session=None):
218 +    def iter_process_result(self, cursor, cnx, column_callbacks=None):
219          """return a iterator on tuples of CubicWeb compliant values from data
220          in the given cursor
221          """
222          # use two different implementations to avoid paying the price of
223          # callback lookup for each *cell* in results when there is nothing to
224          # lookup
225          if not column_callbacks:
226              return self.dbhelper.dbapi_module.process_cursor(cursor, self._dbencoding,
227                                                               Binary)
228 -        assert session
229 -        return self._cb_process_result(cursor, column_callbacks, session)
230 +        assert cnx
231 +        return self._cb_process_result(cursor, column_callbacks, cnx)
232 
233 -    def _cb_process_result(self, cursor, column_callbacks, session):
234 +    def _cb_process_result(self, cursor, column_callbacks, cnx):
235          # begin bind to locals for optimization
236          descr = cursor.description
237          encoding = self._dbencoding
238          process_value = self._process_value
239          binary = Binary
@@ -406,11 +406,11 @@
240                      cbstack = column_callbacks.get(col, None)
241                      if cbstack is None:
242                          value = process_value(value, descr[col], encoding, binary)
243                      else:
244                          for cb in cbstack:
245 -                            value = cb(self, session, value)
246 +                            value = cb(self, cnx, value)
247                      result.append(value)
248                  yield result
249 
250      def preprocess_entity(self, entity):
251          """return a dictionary to use as extra argument to cursor.execute