[buildobj] Allow to define specific parameters in _make_type function , closes #124342

authorVincent Michel <vincent.michel@logilab.fr>
changesetf84e4fafad0c
branchdefault
phasedraft
hiddenyes
parent revision#b739d650ba6a add a script which compares two yams schemas (closes #112914)
child revision#80e47a2da33a [schema] Add a new _TYPE_PROPERTIES for adding new data types on the fly
files modified by this revision
buildobjs.py
# HG changeset patch
# User Vincent Michel <vincent.michel@logilab.fr>
# Date 1364393949 -3600
# Wed Mar 27 15:19:09 2013 +0100
# Node ID f84e4fafad0cb03c02eacc0aa6a400ed80feeb5a
# Parent b739d650ba6aff7b6bfa49faf950b16b55e6a646
[buildobj] Allow to define specific parameters in _make_type function , closes #124342

diff --git a/buildobjs.py b/buildobjs.py
@@ -595,29 +595,33 @@
1 
2  class ObjectRelation(Relation):
3      cardinality = MARKER
4      constraints = MARKER
5 
6 -    def __init__(self, etype, **kwargs):
7 +    def __init__(self, etype, add_params=(), **kwargs):
8          if self.__class__.__name__ == 'ObjectRelation':
9              warn('[yams 0.29] ObjectRelation is deprecated, '
10                   'use RelationDefinition subclass', DeprecationWarning,
11                   stacklevel=2)
12          global CREATION_RANK
13          CREATION_RANK += 1
14          self.creation_rank = CREATION_RANK
15          self.name = '<undefined>'
16          self.etype = etype
17 +        # Add user-defined parameters if needed
18 +        self.add_params = add_params
19          if self.constraints:
20              self.constraints = list(self.constraints)
21          self.override = kwargs.pop('override', False)
22          if 'symetric' in kwargs:
23              kwargs['symmetric'] = kwargs.pop('symetric')
24              warn('[yams 0.27.0] symetric has been respelled symmetric',
25                   DeprecationWarning, stacklevel=2)
26          try:
27 -            _check_kwargs(kwargs, REL_PROPERTIES)
28 +            # Add additional parameters for custom base types
29 +            rdef_properties = REL_PROPERTIES + ('description',) + self.add_params
30 +            _check_kwargs(kwargs, rdef_properties)
31          except BadSchemaDefinition, bad:
32              # XXX (auc) bad field name + required attribute can lead there instead of schema.py ~ 920
33              bsd_ex = BadSchemaDefinition(('%s in relation to entity %r (also is %r defined ? (check two '
34                                            'lines above in the backtrace))') % (bad.args, etype, etype))
35              bsd_ex.tb_offset = 2
@@ -680,14 +684,14 @@
36          # transform unique into UniqueConstraint
37          unique = kwargs.pop('unique', None)
38          if unique:
39              _add_constraint(kwargs, UniqueConstraint())
40          # use the etype attribute provided by subclasses
41 -        super(AbstractTypedAttribute, self).__init__(self.etype, **kwargs)
42 +        super(AbstractTypedAttribute, self).__init__(self.etype, self.add_params, **kwargs)
43          # reassign creation rank
44          #
45 -        # Main attribute are marked as created before it's metadata.
46 +        # Main attribute are marked as created before its metadata.
47          # order in meta data is preserved.
48          if self.metadata:
49              meta = sorted(metadata.values(), key= lambda x: x.creation_rank)
50              if meta[0].creation_rank < self.creation_rank:
51                  m_iter = iter(meta)
@@ -709,12 +713,22 @@
52 
53      def __repr__(self):
54          return '<%(name)s(%(etype)s)>' % self.__dict__
55 
56  # build a specific class for each base type
57 -def _make_type(etype):
58 -    return type(etype, (AbstractTypedAttribute,), {'etype' : etype})
59 +def _make_type(etype, add_params=()):
60 +    """Add params is a tuple of user defined / custom type parameters name.
61 +    It is now possible to create a specific type with user-defined params, e.g.:
62 +
63 +    etype = 'Geometry' (c.f. postgis)
64 +    add_params = ('srid', 'geom_type','coord_dimension')
65 +
66 +    This will allow the use of :
67 +        Geometry(geom_type='POINT', srid=-1, coord_dimension=2)
68 +    in a Yams schema.
69 +    """
70 +    return type(etype, (AbstractTypedAttribute,), {'etype' : etype, 'add_params': add_params})
71 
72  String = _make_type('String')
73  Password = _make_type('Password')
74  Bytes = _make_type('Bytes')
75  Int = _make_type('Int')