Commit 90cd4087 authored by Denis Laxalde's avatar Denis Laxalde
Browse files

Factor out computation of target types of a relation in mappers._utils module

parent 275fcbaa2fe4
# copyright 2018 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr -- mailto:contact@logilab.fr
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Utilities for Yams to JSON Schema mappers."""
def relation_target_types(schema, rtype, role, preset_types=None):
"""Yield target entity types for (rtype, role) relation, possibly filtered
out from values not in `preset_types`.
"""
rschema = schema[rtype]
for target_eschema in sorted(rschema.targets(role=role)):
if target_eschema.final:
continue
target_type = target_eschema.type
if preset_types is not None and target_type not in preset_types:
continue
yield target_type
......@@ -59,6 +59,8 @@ from .predicates import (
yams_final_rtype,
yams_match,
)
from . import _utils
__all__ = [
'BaseRelationMapper',
......@@ -101,17 +103,9 @@ class BaseRelationMapper(JSONSchemaMapper):
#: role of `etype` in relation
self.role = kwargs.pop('role')
#: possible target types of the relation (empty for attribute relations)
self.target_types = []
rschema = _cw.vreg.schema[self.rtype]
target_types = kwargs.pop('target_types', None)
for target_eschema in sorted(rschema.targets(role=self.role)):
if target_eschema.final:
continue
target_type = target_eschema.type
if target_types is not None and target_type not in target_types:
continue
self.target_types.append(target_type)
self.target_types = list(_utils.relation_target_types(
_cw.vreg.schema, self.rtype, self.role,
kwargs.pop('target_types', None)))
super(BaseRelationMapper, self).__init__(_cw, **kwargs)
def __repr__(self):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment