[PATCH cwclientlib] [cwproxy] Add helpers like in CubicWeb

nicola.spanti at logilab.fr nicola.spanti at logilab.fr
Thu Feb 27 18:01:38 CET 2020


# HG changeset patch
# User Nicola Spanti <nicola.spanti at logilab.fr>
# Date 1582821536 -3600
#      Thu Feb 27 17:38:56 2020 +0100
# Node ID 64676e892eb3e97a474f53a87ecbd69c9e6fec5d
# Parent  8c393cd5772de6a405b5567fb1cbea360c0c7374
# EXP-Topic cwproxy-helpers
# Available At https://hg.logilab.org/review/cwclientlib
#              hg pull https://hg.logilab.org/review/cwclientlib -r 64676e892eb3
[cwproxy] Add helpers like in CubicWeb

- It is helpful not to have to write RQL request.
- It is an improved version of code already in production for a
  private project.

diff -r 8c393cd5772d -r 64676e892eb3 cwclientlib/cwproxy.py
--- a/cwclientlib/cwproxy.py	Tue Jun 12 16:32:21 2018 +0200
+++ b/cwclientlib/cwproxy.py	Thu Feb 27 17:38:56 2020 +0100
@@ -1,5 +1,5 @@
-# copyright 2014-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
-# contact http://www.logilab.fr/ -- mailto:contact at logilab.fr
+# copyright 2014-2020 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact https://www.logilab.fr/ -- mailto:contact at logilab.fr
 #
 # This file is part of cwclientlib.
 #
@@ -14,7 +14,7 @@
 # for more details.
 #
 # You should have received a copy of the GNU Lesser General Public License
-# along with cwclientlib. If not, see <http://www.gnu.org/licenses/>.
+# along with cwclientlib. If not, see <https://www.gnu.org/licenses/>.
 
 """A CWProxy class wraps a CubicWeb repository.
 
@@ -84,6 +84,14 @@
     pass
 
 
+class NoResultError(Exception):
+    pass
+
+
+class NoUniqueEntity(Exception):
+    pass
+
+
 class CWProxy(object):
     """CWProxy: A simple helper class to ease building CubicWeb_
     clients. It allows to:
@@ -325,3 +333,40 @@
                     args[k] = v.isoformat()
         files['json'] = ('json', json.dumps(queries), 'application/json')
         return files
+
+    def _set_rql_request(self, rql_request, kwargs, sep=','):
+        args = ['X {property_name:s} %({property_name:s})s'.format(
+            property_name=property_name) for property_name in kwargs]
+        if args:
+            rql_request = u'{:s}{:s} {:s}'.format(
+                rql_request, sep, ', '.join(args))
+        return rql_request
+
+    def _rql_args_select(self, rql_request, kwargs, sep=','):
+        rql_request = self._set_rql_request(rql_request, kwargs, sep)
+        return [row[0] for row in self.rqlio([(rql_request, kwargs)])[0]]
+
+    def exist(self, entity_type, **kwargs):
+        return bool(self._rql_args_select(
+            'Any 1 LIMIT 1 WHERE X is {:s}'.format(entity_type), kwargs))
+
+    def find(self, entity_type, **kwargs):
+        return self._rql_args_select(
+            'Any X WHERE X is {:s}'.format(entity_type), kwargs)
+
+    def find_one(self, entity_type, **kwargs):
+        eids = self._rql_args_select(
+            'Any X LIMIT 2 WHERE X is {:s}'.format(entity_type), kwargs)
+        if len(eids) == 0:
+            raise NoResultError('No result for {:s}'.format(entity_type))
+        if len(eids) > 1:
+            raise NoUniqueEntity('Cannot find unique {:s}'.format(entity_type))
+        return eids[0]
+
+    def find_last_created(self, entity_type, **kwargs):
+        eid = self._rql_args_select(
+            'Any X ORDERBY D DESC LIMIT 1 '
+            'WHERE X is {:s}, X creation_date D'.format(entity_type), kwargs)
+        if not eid:
+            raise NoResultError('No result for {:s}'.format(entity_type))
+        return eid[0]



More information about the cubicweb-devel mailing list