Commit ca0b1508 authored by sylvain.thenault@logilab.fr's avatar sylvain.thenault@logilab.fr
Browse files

delete-trailing-whitespaces

--HG--
branch : tls-sprint
parent 717dea3362c0
......@@ -62,7 +62,7 @@ try:
except ImportError:
AutomaticEntityForm = None
def dispatch_rtags(*args):
pass
......@@ -74,7 +74,7 @@ def _get_etype(bases, classdict):
etype = getattr(base, 'id', None)
if etype and etype != 'Any':
return etype
def _get_defs(attr, name, bases, classdict):
try:
yield name, classdict.pop(attr)
......@@ -86,7 +86,7 @@ def _get_defs(attr, name, bases, classdict):
yield base.__name__, value
except AttributeError:
continue
class metaentity(type):
"""this metaclass sets the relation tags on the entity class
and deals with the `widgets` attribute
......@@ -138,7 +138,7 @@ class metaentity(type):
class Entity(AppRsetObject, dict):
"""an entity instance has e_schema automagically set on
the class and instances has access to their issuing cursor.
A property is set for each attribute and relation on each entity's type
class. Becare that among attributes, 'eid' is *NEITHER* stored in the
dict containment (which acts as a cache for other attributes dynamically
......@@ -151,7 +151,7 @@ class Entity(AppRsetObject, dict):
:cvar rest_var: indicates which attribute should be used to build REST urls
If None is specified, the first non-meta attribute will
be used
:type skip_copy_for: list
:cvar skip_copy_for: a list of relations that should be skipped when copying
this kind of entity. Note that some relations such
......@@ -162,14 +162,14 @@ class Entity(AppRsetObject, dict):
__registry__ = 'etypes'
__select__ = yes()
# class attributes that must be set in class definition
# class attributes that must be set in class definition
id = None
rest_attr = None
fetch_attrs = None
skip_copy_for = ()
# class attributes set automatically at registration time
e_schema = None
@classmethod
def registered(cls, registry):
"""build class using descriptor at registration time"""
......@@ -178,7 +178,7 @@ class Entity(AppRsetObject, dict):
if cls.id != 'Any':
cls.__initialize__()
return cls
MODE_TAGS = set(('link', 'create'))
CATEGORY_TAGS = set(('primary', 'secondary', 'generic', 'generated')) # , 'metadata'))
@classmethod
......@@ -210,7 +210,7 @@ class Entity(AppRsetObject, dict):
if mixins:
cls.__bases__ = tuple(mixins + [p for p in cls.__bases__ if not p is object])
cls.debug('plugged %s mixins on %s', mixins, etype)
@classmethod
def fetch_rql(cls, user, restriction=None, fetchattrs=None, mainvar='X',
settype=True, ordermethod='fetch_order'):
......@@ -231,7 +231,7 @@ class Entity(AppRsetObject, dict):
rql += ' ORDERBY %s' % ','.join(orderby)
rql += ' WHERE %s' % ', '.join(restrictions)
return rql
@classmethod
def _fetch_restrictions(cls, mainvar, varmaker, fetchattrs,
selection, orderby, restrictions, user,
......@@ -293,7 +293,7 @@ class Entity(AppRsetObject, dict):
else:
self.eid = None
self._is_saved = True
def __repr__(self):
return '<Entity %s %s %s at %s>' % (
self.e_schema, self.eid, self.keys(), id(self))
......@@ -308,11 +308,11 @@ class Entity(AppRsetObject, dict):
"""hook called by the repository before doing anything to add the entity
(before_add entity hooks have not been called yet). This give the
occasion to do weird stuff such as autocast (File -> Image for instance).
This method must return the actual entity to be added.
"""
return self
def set_eid(self, eid):
self.eid = self['eid'] = eid
......@@ -333,7 +333,7 @@ class Entity(AppRsetObject, dict):
saved in its source.
"""
return self.has_eid() and self._is_saved
@cached
def metainformation(self):
res = dict(zip(('type', 'source', 'extid'), self.req.describe(self.eid)))
......@@ -349,7 +349,7 @@ class Entity(AppRsetObject, dict):
def has_perm(self, action):
return self.e_schema.has_perm(self.req, action, self.eid)
def view(self, vid, __registry='views', **kwargs):
"""shortcut to apply a view on this entity"""
return self.vreg.render(__registry, vid, self.req, rset=self.rset,
......@@ -452,9 +452,9 @@ class Entity(AppRsetObject, dict):
trdata = TransformData(data, format, encoding, appobject=self)
data = _engine.convert(trdata, target_format).decode()
if format == 'text/html':
data = soup2xhtml(data, self.req.encoding)
data = soup2xhtml(data, self.req.encoding)
return data
# entity cloning ##########################################################
def copy_relations(self, ceid):
......@@ -517,7 +517,7 @@ class Entity(AppRsetObject, dict):
rset = ResultSet([(self.eid,)], 'Any X WHERE X eid %(x)s',
{'x': self.eid}, [(self.id,)])
return self.req.decorate_rset(rset)
def to_complete_relations(self):
"""by default complete final relations to when calling .complete()"""
for rschema in self.e_schema.subject_relations():
......@@ -533,7 +533,7 @@ class Entity(AppRsetObject, dict):
all(matching_groups(es.get_groups('read'))
for es in rschema.objects(self.e_schema)):
yield rschema, 'subject'
def to_complete_attributes(self, skip_bytes=True):
for rschema, attrschema in self.e_schema.attribute_definitions():
# skip binary data by default
......@@ -548,7 +548,7 @@ class Entity(AppRsetObject, dict):
self[attr] = None
continue
yield attr
def complete(self, attributes=None, skip_bytes=True):
"""complete this entity by adding missing attributes (i.e. query the
repository to fill the entity)
......@@ -618,7 +618,7 @@ class Entity(AppRsetObject, dict):
else:
rrset = self.req.eid_rset(value)
self.set_related_cache(rtype, x, rrset)
def get_value(self, name):
"""get value for the attribute relation <name>, query the repository
to get the value if necessary.
......@@ -654,7 +654,7 @@ class Entity(AppRsetObject, dict):
def related(self, rtype, role='subject', limit=None, entities=False):
"""returns a resultset of related entities
:param role: is the role played by 'self' in the relation ('subject' or 'object')
:param limit: resultset's maximum size
:param entities: if True, the entites are returned; if False, a result set is returned
......@@ -700,7 +700,7 @@ class Entity(AppRsetObject, dict):
args = tuple(rql.split(' WHERE ', 1))
rql = '%s ORDERBY Z DESC WHERE X modification_date Z, %s' % args
return rql
# generic vocabulary methods ##############################################
@obsolete('see new form api')
......@@ -708,7 +708,7 @@ class Entity(AppRsetObject, dict):
"""vocabulary functions must return a list of couples
(label, eid) that will typically be used to fill the
edition view's combobox.
If `eid` is None in one of these couples, it should be
interpreted as a separator in case vocabulary results are grouped
"""
......@@ -717,7 +717,7 @@ class Entity(AppRsetObject, dict):
form = EntityFieldsForm(self.req, entity=self)
field = mock_object(name=rtype, role=role)
return form.form_field_vocabulary(field, limit)
def unrelated_rql(self, rtype, targettype, role, ordermethod=None,
vocabconstraints=True):
"""build a rql to fetch `targettype` entities unrelated to this entity
......@@ -753,7 +753,7 @@ class Entity(AppRsetObject, dict):
before, after = rql.split(' WHERE ', 1)
rql = '%s ORDERBY %s WHERE %s' % (before, searchedvar, after)
return rql
def unrelated(self, rtype, targettype, role='subject', limit=None,
ordermethod=None):
"""return a result set of target type objects that may be related
......@@ -766,14 +766,14 @@ class Entity(AppRsetObject, dict):
if self.has_eid():
return self.req.execute(rql, {'x': self.eid})
return self.req.execute(rql)
# relations cache handling ################################################
def relation_cached(self, rtype, role):
"""return true if the given relation is already cached on the instance
"""
return '%s_%s' % (rtype, role) in self._related_cache
def related_cache(self, rtype, role, entities=True, limit=None):
"""return values for the given relation if it's cached on the instance,
else raise `KeyError`
......@@ -785,7 +785,7 @@ class Entity(AppRsetObject, dict):
else:
res = res.limit(limit)
return res
def set_related_cache(self, rtype, role, rset, col=0):
"""set cached values for the given relation"""
if rset:
......@@ -805,7 +805,7 @@ class Entity(AppRsetObject, dict):
else:
related = []
self._related_cache['%s_%s' % (rtype, role)] = (rset, related)
def clear_related_cache(self, rtype=None, role=None):
"""clear cached values for the given relation or the entire cache if
no relation is given
......@@ -815,9 +815,9 @@ class Entity(AppRsetObject, dict):
else:
assert role
self._related_cache.pop('%s_%s' % (rtype, role), None)
# raw edition utilities ###################################################
def set_attributes(self, **kwargs):
assert kwargs
relations = []
......@@ -829,14 +829,14 @@ class Entity(AppRsetObject, dict):
kwargs['x'] = self.eid
self.req.execute('SET %s WHERE X eid %%(x)s' % ','.join(relations),
kwargs, 'x')
def delete(self):
assert self.has_eid(), self.eid
self.req.execute('DELETE %s X WHERE X eid %%(x)s' % self.e_schema,
{'x': self.eid})
# server side utilities ###################################################
def set_defaults(self):
"""set default values according to the schema"""
self._default_set = set()
......@@ -879,13 +879,13 @@ class Entity(AppRsetObject, dict):
yield self
else:
yield self
def get_words(self):
"""used by the full text indexer to get words to index
this method should only be used on the repository side since it depends
on the indexer package
:rtype: list
:return: the list of indexable word of this entity
"""
......@@ -902,7 +902,7 @@ class Entity(AppRsetObject, dict):
continue
if value:
words += tokenize(value)
for rschema, role in self.e_schema.fulltext_relations():
if role == 'subject':
for entity in getattr(self, rschema.type):
......@@ -947,7 +947,7 @@ class Relation(object):
raise AttributeError('%s cannot be only be accessed from instances'
% self._rtype)
return eobj.related(self._rtype, self._role, entities=True)
def __set__(self, eobj, value):
raise NotImplementedError
......@@ -955,7 +955,7 @@ class Relation(object):
class SubjectRelation(Relation):
"""descriptor that controls schema relation access"""
_role = 'subject'
class ObjectRelation(Relation):
"""descriptor that controls schema relation access"""
_role = 'object'
......
......@@ -33,7 +33,7 @@ above by::
selectors.TRACED_OIDS = ('calendar',)
self.view('calendar', myrset)
selectors.TRACED_OIDS = ()
:organization: Logilab
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
......@@ -97,11 +97,11 @@ class traced_selection(object):
>>> with traced_selection( ('oid1', 'oid2') ):
... # some code in which you want to debug selectors
... # for objects with id 'oid1' and 'oid2'
"""
def __init__(self, traced='all'):
self.traced = traced
def __enter__(self):
global TRACED_OIDS
TRACED_OIDS = self.traced
......@@ -139,7 +139,7 @@ def score_interface(cls_or_inst, cls, iface):
class PartialSelectorMixIn(object):
"""convenience mix-in for selectors that will look into the containing
class to find missing information.
cf. `cubicweb.web.action.LinkToEntityAction` for instance
"""
def __call__(self, cls, *args, **kwargs):
......@@ -157,7 +157,7 @@ class ImplementsMixIn(object):
def __str__(self):
return '%s(%s)' % (self.__class__.__name__,
','.join(str(s) for s in self.expected_ifaces))
def score_interfaces(self, cls_or_inst, cls):
score = 0
vreg, eschema = cls_or_inst.vreg, cls_or_inst.e_schema
......@@ -187,7 +187,7 @@ class EClassSelector(Selector):
"""
def __init__(self, once_is_enough=False):
self.once_is_enough = once_is_enough
@lltrace
def __call__(self, cls, req, rset, row=None, col=0, **kwargs):
if not rset:
......@@ -213,7 +213,7 @@ class EClassSelector(Selector):
if etype in BASE_TYPES:
return 0
return self.score_class(cls.vreg.etype_class(etype), req)
def score_class(self, eclass, req):
raise NotImplementedError()
......@@ -236,7 +236,7 @@ class EntitySelector(EClassSelector):
note: None values (resulting from some outer join in the query) are not
considered.
"""
@lltrace
def __call__(self, cls, req, rset, row=None, col=0, **kwargs):
if not rset and not kwargs.get('entity'):
......@@ -265,7 +265,7 @@ class EntitySelector(EClassSelector):
return self.score_entity(rset.get_entity(row, col))
except NotAnEntity:
return 0
def score_entity(self, entity):
raise NotImplementedError()
......@@ -302,7 +302,7 @@ def nonempty_rset(cls, req, rset, *args, **kwargs):
if rset is not None and rset.rowcount:
return 1
return 0
@objectify_selector
@lltrace
def empty_rset(cls, req, rset, *args, **kwargs):
......@@ -341,7 +341,7 @@ def two_cols_rset(cls, req, rset, *args, **kwargs):
@lltrace
def paginated_rset(cls, req, rset, *args, **kwargs):
"""accept result set with more lines than the page size.
Page size is searched in (respecting order):
* a page_size argument
* a page_size form parameters
......@@ -442,7 +442,7 @@ def match_context_prop(cls, req, rset, row=None, col=0, context=None,
class match_search_state(Selector):
"""accept if the current request search state is in one of the expected
states given to the initializer
:param expected: either 'normal' or 'linksearch' (eg searching for an
object to create a relation with another)
"""
......@@ -453,7 +453,7 @@ class match_search_state(Selector):
def __str__(self):
return '%s(%s)' % (self.__class__.__name__,
','.join(sorted(str(s) for s in self.expected)))
@lltrace
def __call__(self, cls, req, rset, row=None, col=0, **kwargs):
try:
......@@ -467,7 +467,7 @@ class match_search_state(Selector):
class match_form_params(match_search_state):
"""accept if parameters specified as initializer arguments are specified
in request's form parameters
:param *expected: parameters (eg `basestring`) which are expected to be
found in request's form parameters
"""
......@@ -486,7 +486,7 @@ class match_form_params(match_search_state):
class match_kwargs(match_search_state):
"""accept if parameters specified as initializer arguments are specified
in named arguments given to the selector
:param *expected: parameters (eg `basestring`) which are expected to be
found in named arguments (kwargs)
"""
......@@ -502,7 +502,7 @@ class match_kwargs(match_search_state):
class match_user_groups(match_search_state):
"""accept if logged users is in at least one of the given groups. Returned
score is the number of groups in which the user is.
If the special 'owners' group is given:
* if row is specified check the entity at the given row/col is owned by the
logged user
......@@ -512,7 +512,7 @@ class match_user_groups(match_search_state):
:param *required_groups: name of groups (`basestring`) in which the logged
user should be
"""
@lltrace
def __call__(self, cls, req, rset=None, row=None, col=0, **kwargs):
user = req.user
......@@ -550,7 +550,7 @@ class appobject_selectable(Selector):
def __init__(self, registry, oid):
self.registry = registry
self.oid = oid
def __call__(self, cls, req, rset, *args, **kwargs):
try:
cls.vreg.select_object(self.registry, self.oid, req, rset, *args, **kwargs)
......@@ -572,7 +572,7 @@ class implements(ImplementsMixIn, EClassSelector):
or an entity type (e.g. `basestring`) in which case
the associated class will be searched in the
registry (at selection time)
note: when interface is an entity class, the score will reflect class
proximity so the most specific object'll be selected
"""
......@@ -589,11 +589,11 @@ class specified_etype_implements(implements):
or an entity type (e.g. `basestring`) in which case
the associated class will be searched in the
registry (at selection time)
note: when interface is an entity class, the score will reflect class
proximity so the most specific object'll be selected
"""
@lltrace
def __call__(self, cls, req, *args, **kwargs):
try:
......@@ -617,10 +617,10 @@ class entity_implements(ImplementsMixIn, EntitySelector):
or an entity type (e.g. `basestring`) in which case
the associated class will be searched in the
registry (at selection time)
note: when interface is an entity class, the score will reflect class
proximity so the most specific object'll be selected
"""
"""
def score_entity(self, entity):
return self.score_interfaces(entity, entity.__class__)
......@@ -655,7 +655,7 @@ class relation_possible(EClassSelector):
return 0
score = super(relation_possible, self).__call__(cls, req, *args, **kwargs)
return score
def score_class(self, eclass, req):
eschema = eclass.e_schema
try:
......@@ -684,12 +684,12 @@ class partial_relation_possible(PartialSelectorMixIn, relation_possible):
for this selector are:
- `rtype`: same as `rtype` parameter of the `relation_possible` selector
- `role`: this attribute will be passed to the `cubicweb.role` function
to determine the role of class in the relation
- `etype` (optional): the entity type on the other side of the relation
:param action: a relation schema action (one of 'read', 'add', 'delete')
which must be granted to the logged user, else a 0 score will
be returned
......@@ -707,19 +707,19 @@ class partial_relation_possible(PartialSelectorMixIn, relation_possible):
class may_add_relation(EntitySelector):
"""accept if the relation can be added to an entity found in the result set
by the logged user.
See `EntitySelector` documentation for behaviour when row is not specified.
:param rtype: a relation type (`basestring`)
:param role: the role of the result set entity in the relation. 'subject' or
'object', default to 'subject'.
"""
def __init__(self, rtype, role='subject', once_is_enough=False):
super(may_add_relation, self).__init__(once_is_enough)
self.rtype = rtype
self.role = role
def score_entity(self, entity):
rschema = entity.schema.rschema(self.rtype)
if self.role == 'subject':
......@@ -738,10 +738,10 @@ class partial_may_add_relation(PartialSelectorMixIn, may_add_relation):
for this selector are:
- `rtype`: same as `rtype` parameter of the `relation_possible` selector
- `role`: this attribute will be passed to the `cubicweb.role` function
to determine the role of class in the relation.
:param action: a relation schema action (one of 'read', 'add', 'delete')
which must be granted to the logged user, else a 0 score will
be returned
......@@ -753,12 +753,12 @@ class partial_may_add_relation(PartialSelectorMixIn, may_add_relation):
self.rtype = cls.rtype
self.role = role(cls)
class has_related_entities(EntitySelector):
"""accept if entity found in the result set has some linked entities using
the specified relation (optionaly filtered according to the specified target
type). Checks first if the relation is possible.
See `EntitySelector` documentation for behaviour when row is not specified.
:param rtype: a relation type (`basestring`)
......@@ -773,7 +773,7 @@ class has_related_entities(EntitySelector):
self.rtype = rtype
self.role = role
self.target_etype = target_etype
def score_entity(self, entity):
relpossel = relation_possible(self.rtype, self.role, self.target_etype)
if not relpossel.score_class(entity.__class__, entity.req):
......@@ -792,12 +792,12 @@ class partial_has_related_entities(PartialSelectorMixIn, has_related_entities):
for this selector are:
- `rtype`: same as `rtype` parameter of the `relation_possible` selector
- `role`: this attribute will be passed to the `cubicweb.role` function
to determine the role of class in the relation.
- `etype` (optional): the entity type on the other side of the relation
:param action: a relation schema action (one of 'read', 'add', 'delete')
which must be granted to the logged user, else a 0 score will
be returned
......@@ -822,13 +822,13 @@ class has_permission(EntitySelector):
note: None values (resulting from some outer join in the query) are not
considered.
:param action: an entity schema action (eg 'read'/'add'/'delete'/'update')
"""
def __init__(self, action, once_is_enough=False):
super(has_permission, self).__init__(once_is_enough)
self.action = action
@lltrace
def __call__(self, cls, req, rset, row=None, col=0, **kwargs):
if rset is None:
......@@ -837,7 +837,7 @@ class has_permission(EntitySelector):
action = self.action
if row is None:
score = 0
need_local_check = []
need_local_check = []
geteschema = cls.schema.eschema
for etype in rset.column_types(0):
if etype in BASE_TYPES:
......@@ -862,7 +862,7 @@ class has_permission(EntitySelector):
score += 1
return score
return self.score(req, rset, row, col)
def score_entity(self, entity):
if entity.has_perm(self.action):
return 1
......@@ -887,7 +887,7 @@ class rql_condition(EntitySelector):
"""accept if an arbitrary rql return some results for an eid found in the
result set. Returned score is the number of items returned by the rql
condition.
See `EntitySelector` documentation for behaviour when row is not specified.
:param expression: basestring containing an rql expression, which should use
......@@ -904,7 +904,7 @@ class rql_condition(EntitySelector):
else:
rql = 'Any X WHERE X eid %%(x)s, %s' % expression
self.rql = rql
def score(self, req, rset, row, col):
try:
return len(req.execute(self.rql, {'x': rset[row][col],
......@@ -912,32 +912,32 @@ class rql_condition(EntitySelector):
except Unauthorized:
return 0
class but_etype(EntitySelector):
"""accept if the given entity types are not found in the result set.
See `EntitySelector` documentation for behaviour when row is not specified.
:param *etypes: entity types (`basestring`) which should be refused