Salome HOME
bug
[tools/eficas.git] / Efi2Xsd / introspect.py
1 # Demonstrate alternatives for bindings customization
2 import sys, os
3 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"..")))
4
5 # Introspection-based customization.
6 #from raw.custom import *
7 #import raw.custom as raw_custom
8 from Atmo.raw.atmo_test3 import *
9 import Atmo.raw.atmo_test3 as raw_custom
10
11 #class ta04 (raw_custom.ta04):
12 #    def xa04 (self):
13 #        return 'extend ta04'
14 #raw_custom.ta04._SetSupersedingClass(ta04)
15
16 import inspect
17 def creationAccasSimp(c):
18     print c 
19
20 class toto
21 def __init__(self,*args):
22     print dir(self)
23     mro = type(self).mro()
24     for next_class in mro[mro.index(ChildB) + 1:] :
25         if hasattr(next_class, '__init__'):
26            next_class.__init__(self,args)
27
28 # Utility function to identify classes of interest
29 def _isSupersedable (cls):
30     return inspect.isclass(cls) and issubclass(cls, pyxb.binding.basis._DynamicCreate_mixin)
31
32 def _injectClasses ():
33     import sys
34     import pyxb.binding.basis
35     
36     # All PyXB complex type definitions in the original module
37     raw_classes = set([_o for (_, _o) in inspect.getmembers(raw_custom) if _isSupersedable(_o)])
38     raw_classes_compo=set()
39     raw_classes_simp=set()
40     for c in raw_classes :
41       if issubclass(c,pyxb.binding.basis.complexTypeDefinition) : raw_classes_compo.add(c)
42       else : raw_classes_simp.add(c)
43     #print 'Original classes complex type: %s' % (raw_classes_compo,)
44     print 'Original classes simple type: %s' % (raw_classes_simp,)
45     for c in raw_classes_simp:
46         setattr(c,'creationAccasSimp',creationAccasSimp)
47         oldInit=c.__init__
48         print c.__class__
49         #setattr(c,'__init__',__init__)
50         print c.__mro__
51     
52     # PyXB complex type definitions in this module that did not come
53     # from the original import *.
54     this_module = sys.modules[__name__]
55     this_classes = set([_o for (_, _o) in inspect.getmembers(this_module) if _isSupersedable(_o) and _o not in raw_classes])
56     this_classes_tuple = tuple(this_classes)
57     #print 'This classes: %s' % (this_classes,)
58     
59     # Raw classes superseded by something in this module
60     superseded_classes = set([ _o for _o in raw_classes if _o._SupersedingClass() in this_classes ])
61     superseded_classes_tuple = tuple(superseded_classes)
62     print 'Superseded classes: %s' % (superseded_classes,)
63
64     # Raw classes that are subclasses of something superseded by this
65     # module, but that are not themselves superseded by this module
66     need_supersedure_classes = set([_o for _o in raw_classes if issubclass(_o, superseded_classes_tuple) and _o not in superseded_classes])
67     print 'Need supersedure classes: %s' % (need_supersedure_classes,)
68
69     # Add local definitions to supersede classes all of whose
70     # ancestors have been superseded as necessary.
71     while need_supersedure_classes:
72         did_replacement = False
73         new_need_supersedure_classes = set()
74         for o in need_supersedure_classes:
75             candidate = True
76             # Build the new sequence of base classes while we check them.
77             new_mro = []
78             for super_o in o.__mro__:
79                 if super_o == o:
80                     # Put the superseded class in its original position (probably first)
81                     new_mro.append(o)
82                     continue
83                 if super_o in need_supersedure_classes:
84                     # Subclass of a class we haven't gotten to yet; put it off
85                     candidate = False
86                     break
87                 # Append the replacement or the original, as needed
88                 if super_o in superseded_classes:
89                     new_mro.append(super_o._SupersedingClass())
90                 else:
91                     new_mro.append(super_o)
92             if not candidate:
93                 new_need_supersedure_classes.add(o)
94                 continue
95             # Create a new class that subclasses the replacements
96             name = o.__name__
97             new_o = type(name, tuple(new_mro), o.__dict__.copy())
98             # Install it in the module
99             setattr(this_module, name, new_o)
100             # Tell PyXB to use it as the superseding class
101             o._SetSupersedingClass(new_o)
102             # Record it so future passes will find it
103             superseded_classes.add(o)
104         assert need_supersedure_classes != new_need_supersedure_classes
105         need_supersedure_classes = new_need_supersedure_classes
106
107 _injectClasses()
108 m=T_Unit1(1)
109 print m