Very minor improvements of cubicweb/server/repository.py

authorNicola Spanti <nicola.spanti@logilab.fr>
changeset02128bd76295
branchdefault
phasedraft
hiddenno
parent revision#3c66c31ebdd2 fix: UX when migractions failed to get its connection
child revision<not specified>
files modified by this revision
cubicweb/server/repository.py
# HG changeset patch
# User Nicola Spanti <nicola.spanti@logilab.fr>
# Date 1607700978 -3600
# Fri Dec 11 16:36:18 2020 +0100
# Node ID 02128bd76295483a72fea1fd525985821d0fa037
# Parent 3c66c31ebdd2394ea6a254563c8bb662004aaf7e
Very minor improvements of cubicweb/server/repository.py

diff --git a/cubicweb/server/repository.py b/cubicweb/server/repository.py
@@ -1,7 +1,7 @@
1 -# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2 -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
3 +# copyright 2003-2020 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
4 +# contact https://www.logilab.fr/ -- mailto:contact@logilab.fr
5  #
6  # This file is part of CubicWeb.
7  #
8  # CubicWeb is free software: you can redistribute it and/or modify it under the
9  # terms of the GNU Lesser General Public License as published by the Free
@@ -12,11 +12,12 @@
10  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
12  # details.
13  #
14  # You should have received a copy of the GNU Lesser General Public License along
15 -# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
16 +# with CubicWeb.  If not, see <https://www.gnu.org/licenses/>.
17 +
18  """Defines the central class for the CubicWeb RQL server: the repository.
19 
20  The repository is an abstraction allowing execution of rql queries against
21  data sources. Most of the work is actually done in helper classes. The
22  repository mainly:
@@ -30,10 +31,11 @@
23  from contextlib import contextmanager
24  from logging import getLogger
25  import queue
26  import threading
27  import time
28 +from typing import Union
29 
30  from logilab.common.decorators import cached, clear_cache
31 
32  from yams import BadSchemaDefinition
33  from rql.utils import rqlvar_maker
@@ -45,15 +47,15 @@
34  from cubicweb import cwvreg, schema, server
35  from cubicweb.server import utils, hook, querier, sources
36  from cubicweb.server.session import InternalManager, Connection
37 
38 
39 -NO_CACHE_RELATIONS = set([
40 +NO_CACHE_RELATIONS = {
41      ('owned_by', 'object'),
42      ('created_by', 'object'),
43      ('cw_source', 'object'),
44 -])
45 +}
46 
47 
48  def prefill_entity_caches(entity):
49      cnx = entity._cw
50      # prefill entity relation caches
@@ -192,11 +194,11 @@
51      def release(self, cnxset):
52          cnxset.close(True)
53 
54      def __iter__(self):
55          return
56 -        yield
57 +        yield  # needed to be considered as a generator
58 
59      def close(self):
60          pass
61 
62 
@@ -254,18 +256,18 @@
63                  self.debug("[pool] load is low, close a connection")
64                  cnxset.close(True)
65                  with self.lock:
66                      self._cnxsets.remove(cnxset)
67 
68 -    def size(self):
69 +    def size(self) -> int:
70          """
71          Return the total number of connections.
72          """
73          with self.lock:
74              return len(self._cnxsets)
75 
76 -    def qsize(self):
77 +    def qsize(self) -> int:
78          """
79          Return the size of the queue.
80          """
81          return self._queue.qsize()
82 
@@ -302,11 +304,11 @@
83                                      'connections) or too much load on the server (in '
84                                      'which case you can try to set a bigger connections pool size '
85                                      'by changing the connections-pool-max-size option in your '
86                                      'configuration file)')
87              else:
88 -                self.debug("[pool] we had to open a new connection, connection number: %s/%s" %
89 +                self.debug("[pool] we had to open a new connection, connection number: %d/%d" %
90                             (self.size(), self.max_size))
91                  return self._new_cnxset()
92 
93      def release(self, cnxset):
94          """
@@ -349,14 +351,12 @@
95          return _BaseCnxSet(source)
96      idle_timeout = config['connections-pool-idle-timeout']
97      if bootstrap or config.quick_start:
98          min_size, max_size = 0, 1
99      else:
100 -        min_size, max_size = (
101 -            config['connections-pool-min-size'],
102 -            config['connections-pool-max-size'],
103 -        )
104 +        min_size = config['connections-pool-min-size']
105 +        max_size = config['connections-pool-max-size']
106      return _CnxSetPool(source, min_size=min_size, max_size=max_size,
107                         idle_timeout=idle_timeout)
108 
109 
110  class Repository(object):
@@ -497,11 +497,11 @@
111          if self.config.quick_start:
112              self.system_source.init_creating()
113              return
114          with self.internal_cnx() as cnx:
115              sourceent = cnx.execute(
116 -                'Any S, SA, SC WHERE S is_instance_of CWSource,'
117 +                'Any S, SA, SC LIMIT 2 WHERE S is_instance_of CWSource,'
118                  ' S name "system", S type SA, S config SC'
119              ).one()
120              self.system_source.eid = sourceent.eid
121              self.system_source.init(sourceent)
122 
@@ -537,11 +537,11 @@
123                  import traceback
124                  traceback.print_exc()
125                  raise Exception('Is the database initialised ? (cause: %s)' % ex)
126          return appschema
127 
128 -    def has_scheduler(self):
129 +    def has_scheduler(self) -> bool:
130          """Return True if the repository has a scheduler attached and is able
131          to register looping tasks.
132          """
133          return self._scheduler is not None
134 
@@ -809,11 +809,11 @@
135                      etype = None
136                  etypes.append(etype)
137          self.querier.clear_caches(eids, etypes)
138          self.system_source.clear_caches(eids, etypes)
139 
140 -    def type_from_eid(self, eid, cnx):
141 +    def type_from_eid(self, eid: Union[int, str], cnx):
142          """Return the type of the entity with id `eid`"""
143          try:
144              eid = int(eid)
145          except ValueError:
146              raise UnknownEid(eid)