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

authorVincent Michel <vincent.michel@logilab.fr>
changesetc47bc83c252c
branchdefault
phasedraft
hiddenyes
parent revision#b739d650ba6a add a script which compares two yams schemas (closes #112914)
child revision#335a216b4c9c [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 1365082715 -7200
# Thu Apr 04 15:38:35 2013 +0200
# Node ID c47bc83c252cfe512f09eb9b37ca50ca91a9155f
# Parent b739d650ba6aff7b6bfa49faf950b16b55e6a646
[buildobj] Allow to define specific parameters in _make_type function , closes #124342

diff --git a/buildobjs.py b/buildobjs.py
@@ -613,11 +613,13 @@
1          if 'symetric' in kwargs:
2              kwargs['symmetric'] = kwargs.pop('symetric')
3              warn('[yams 0.27.0] symetric has been respelled symmetric',
4                   DeprecationWarning, stacklevel=2)
5          try:
6 -            _check_kwargs(kwargs, REL_PROPERTIES)
7 +            # Add additional parameters for custom base types
8 +            rdef_properties = REL_PROPERTIES + ('description',) + self.etype.extra_parameters
9 +            _check_kwargs(kwargs, rdef_properties)
10          except BadSchemaDefinition, bad:
11              # XXX (auc) bad field name + required attribute can lead there instead of schema.py ~ 920
12              bsd_ex = BadSchemaDefinition(('%s in relation to entity %r (also is %r defined ? (check two '
13                                            'lines above in the backtrace))') % (bad.args, etype, etype))
14              bsd_ex.tb_offset = 2
@@ -683,11 +685,11 @@
15              _add_constraint(kwargs, UniqueConstraint())
16          # use the etype attribute provided by subclasses
17          super(AbstractTypedAttribute, self).__init__(self.etype, **kwargs)
18          # reassign creation rank
19          #
20 -        # Main attribute are marked as created before it's metadata.
21 +        # Main attribute are marked as created before its metadata.
22          # order in meta data is preserved.
23          if self.metadata:
24              meta = sorted(metadata.values(), key= lambda x: x.creation_rank)
25              if meta[0].creation_rank < self.creation_rank:
26                  m_iter = iter(meta)
@@ -709,12 +711,22 @@
27 
28      def __repr__(self):
29          return '<%(name)s(%(etype)s)>' % self.__dict__
30 
31  # build a specific class for each base type
32 -def _make_type(etype):
33 -    return type(etype, (AbstractTypedAttribute,), {'etype' : etype})
34 +def _make_type(etype, extra_parameters=()):
35 +    """Add params is a tuple of user defined / custom type parameters name.
36 +    It is now possible to create a specific type with user-defined params, e.g.:
37 +
38 +    etype = 'Geometry' (c.f. postgis)
39 +    extra_parameters = ('srid', 'geom_type','coord_dimension')
40 +
41 +    This will allow the use of :
42 +        Geometry(geom_type='POINT', srid=-1, coord_dimension=2)
43 +    in a Yams schema.
44 +    """
45 +    return type(etype, (AbstractTypedAttribute,), {'etype' : etype, 'extra_parameters': extra_parameters})
46 
47  String = _make_type('String')
48  Password = _make_type('Password')
49  Bytes = _make_type('Bytes')
50  Int = _make_type('Int')