+# with many thanks to jsbueno on stackexchange for this one
+# https://stackoverflow.com/questions/57273070/
+
+class Meta(ABCMeta):
+ registry = {}
+ recursing = threading.local()
+ recursing.check = False
+ mlock = threading.Lock()
+
+ def __call__(cls, *args, **kw):
+ mcls = cls.__class__
+ if mcls.recursing.check:
+ return super().__call__(*args, **kw)
+ spec = args[0]
+ base = spec.pipekls
+
+ if (cls, base) not in mcls.registry:
+ print ("__call__", args, kw, cls, base, base.__bases__, cls.__bases__)
+ mcls.registry[cls, base] = type(
+ cls.__name__,
+ (cls, base) + cls.__bases__[1:],
+ {}
+ )
+ real_cls = mcls.registry[cls, base]
+
+ with mcls.mlock:
+ mcls.recursing.check = True
+ instance = real_cls.__class__.__call__(real_cls, *args, **kw)
+ mcls.recursing.check = False
+ return instance
+
+
+class DynamicPipe(metaclass=Meta):
+ def __init__(self, *args):
+ print ("DynamicPipe init", super(), args)
+ super().__init__(self, *args)
+