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

Nicolas Chauvat nicolas.chauvat at logilab.fr
Thu Feb 27 20:08:47 CET 2020


Hi Nicola,

On Thu, Feb 27, 2020 at 06:01:38PM +0100, nicola.spanti at logilab.fr wrote:
> [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.

Ok.
 
> +class NoResultError(Exception):
> +    pass
> +
> +
> +class NoUniqueEntity(Exception):
> +    pass
> +

I would suggest to set docstrings instead of "pass":

class NoResultError(Exception):
    """raised when CWProxy.find*() called but resulset is empty"""

class NoUniqueEntity(Exception):
    """raised when CWProxy.find_one() called but resulset contains
    multiple rows
    """

Did you notice that the equivalent exception raised by
cubicweb.rset.ResultSet.one() is named MultipleResultsError ?

See https://hg.logilab.org/master/cubicweb/file/tip/cubicweb/_exceptions.py#l173

Would it be better to call this one by the same name ?

> +    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]]

These methods seem to duplicate methods from rql.RQLGenerator.

See https://hg.logilab.org/master/rql/file/tip/rql/rqlgen.py#l26

Would not it be better to import this library and use that class instead ?

> +    def exist(self, entity_type, **kwargs):
> ...
> +        return eid[0]

1) Do you have tests for these methods in the private project you mention ?

2) Would it be better to have the same API both in CubicWeb and cwclientlib ?

https://hg.logilab.org/master/cubicweb/file/tip/cubicweb/rset.py#l426

https://hg.logilab.org/master/cubicweb/file/tip/cubicweb/req.py#l176

find_one() is cubicweb.req.RequestSessionBase.find() -> cubicweb.rset.ResultSet.one()

there are no find_last_created() and no exist() in RequestSessionBase and ResultSet

I am not sure what is the best thing to do here, just trying to point out the issue.

Thoughts anyone ?

-- 
Nicolas Chauvat

logilab.fr - services en informatique scientifique et gestion de connaissances  



More information about the cubicweb-devel mailing list