1 # Demonstrate alternatives for bindings customization
3 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"..")))
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
11 #class ta04 (raw_custom.ta04):
13 # return 'extend ta04'
14 #raw_custom.ta04._SetSupersedingClass(ta04)
17 def creationAccasSimp(c):
21 def __init__(self,*args):
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)
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)
32 def _injectClasses ():
34 import pyxb.binding.basis
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)
49 #setattr(c,'__init__',__init__)
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,)
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,)
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,)
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:
76 # Build the new sequence of base classes while we check them.
78 for super_o in o.__mro__:
80 # Put the superseded class in its original position (probably first)
83 if super_o in need_supersedure_classes:
84 # Subclass of a class we haven't gotten to yet; put it off
87 # Append the replacement or the original, as needed
88 if super_o in superseded_classes:
89 new_mro.append(super_o._SupersedingClass())
91 new_mro.append(super_o)
93 new_need_supersedure_classes.add(o)
95 # Create a new class that subclasses the replacements
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