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

authorVincent Michel <vincent.michel@logilab.fr>
changeset13cc4ee3396e
branchdefault
phasedraft
hiddenyes
parent revision#b739d650ba6a add a script which compares two yams schemas (closes #112914)
child revision#f9fa1687ea63 [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 1365086974 -7200
# Thu Apr 04 16:49:34 2013 +0200
# Node ID 13cc4ee3396e2e644f04330cef270c97d4ac35eb
# 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,15 @@
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 +            rdef_properties = REL_PROPERTIES + ('description',)
8 +            if getattr(self, 'extra_parameters', None):
9 +                # Add additional parameters for custom base types
10 +                rdef_properties += self.extra_parameters
11 +            _check_kwargs(kwargs, rdef_properties)
12          except BadSchemaDefinition, bad:
13              # XXX (auc) bad field name + required attribute can lead there instead of schema.py ~ 920
14              bsd_ex = BadSchemaDefinition(('%s in relation to entity %r (also is %r defined ? (check two '
15                                            'lines above in the backtrace))') % (bad.args, etype, etype))
16              bsd_ex.tb_offset = 2
@@ -683,11 +687,11 @@
17              _add_constraint(kwargs, UniqueConstraint())
18          # use the etype attribute provided by subclasses
19          super(AbstractTypedAttribute, self).__init__(self.etype, **kwargs)
20          # reassign creation rank
21          #
22 -        # Main attribute are marked as created before it's metadata.
23 +        # Main attribute are marked as created before its metadata.
24          # order in meta data is preserved.
25          if self.metadata:
26              meta = sorted(metadata.values(), key= lambda x: x.creation_rank)
27              if meta[0].creation_rank < self.creation_rank:
28                  m_iter = iter(meta)
@@ -709,12 +713,22 @@
29 
30      def __repr__(self):
31          return '<%(name)s(%(etype)s)>' % self.__dict__
32 
33  # build a specific class for each base type
34 -def _make_type(etype):
35 -    return type(etype, (AbstractTypedAttribute,), {'etype' : etype})
36 +def _make_type(etype, extra_parameters=()):
37 +    """Add params is a tuple of user defined / custom type parameters name.
38 +    It is now possible to create a specific type with user-defined params, e.g.:
39 +
40 +    etype = 'Geometry' (c.f. postgis)
41 +    extra_parameters = ('srid', 'geom_type','coord_dimension')
42 +
43 +    This will allow the use of :
44 +        Geometry(geom_type='POINT', srid=-1, coord_dimension=2)
45 +    in a Yams schema.
46 +    """
47 +    return type(etype, (AbstractTypedAttribute,), {'etype' : etype, 'extra_parameters': extra_parameters})
48 
49  String = _make_type('String')
50  Password = _make_type('Password')
51  Bytes = _make_type('Bytes')
52  Int = _make_type('Int')