1. pombredanne
  2. htsql2

Commits

Kirill Simonov  committed c35cb90

Implemented `unwrap()` probe.

  • Participants
  • Parent commits 53c752d
  • Branches default

Comments (0)

Files changed (1)

File src/htsql/core/tr/lookup.py

View file
     pass
 
 
+class UnwrapProbe(Probe):
+
+    def __init__(self, binding_class, is_deep=True):
+        assert (isinstance(binding_class, type) and
+                issubclass(binding_class, Binding))
+        assert isinstance(is_deep, bool)
+        self.binding_class = binding_class
+        self.is_deep = is_deep
+
+
 class Prescribe(Adapter):
 
     adapt(Arc)
         return None
 
 
+class Unwrap(Lookup):
+
+    adapt(Binding, UnwrapProbe)
+
+    def __call__(self):
+        if isinstance(self.binding, self.probe.binding_class):
+            return self.binding
+        return None
+
+
 class LookupReferenceInScoping(Lookup):
     # Find a reference in a scoping node.
 
         return lookup(self.binding.base, self.probe)
 
 
+class UnwrapChaining(Lookup):
+
+    adapt(ChainingBinding, UnwrapProbe)
+
+    def __call__(self):
+        if isinstance(self.binding, self.probe.binding_class):
+            return self.binding
+        if self.probe.is_deep:
+            return lookup(self.binding.base, self.probe)
+        return None
+
+
 class LookupInImplicitCast(Lookup):
 
     adapt_many((ImplicitCastBinding, GuessNameProbe),
         return lookup(self.binding.base, self.probe)
 
 
+class UnwrapImplicitCast(Lookup):
+
+    adapt(ImplicitCastBinding, UnwrapProbe)
+
+    def __call__(self):
+        if isinstance(self.binding, self.probe.binding_class):
+            return self.binding
+        if self.probe.is_deep:
+            return lookup(self.binding.base, self.probe)
+        return None
+
+
 class LookupCommandInWrapping(Lookup):
 
     adapt(WrappingBinding, CommandProbe)
         return lookup(self.binding.base, self.probe)
 
 
+class UnwrapWrapping(Lookup):
+
+    adapt(WrappingBinding, UnwrapProbe)
+
+    def __call__(self):
+        if isinstance(self.binding, self.probe.binding_class):
+            return self.binding
+        return lookup(self.binding.base, self.probe)
+
+
 class GuessNameFromSegment(Lookup):
     # Generate an attribute name from a segment binding.
 
         return lookup(self.binding.seed, self.probe)
 
 
+class UnwrapSegment(Lookup):
+
+    adapt(SegmentBinding, UnwrapProbe)
+
+    def __call__(self):
+        if isinstance(self.binding, self.probe.binding_class):
+            return self.binding
+        if self.probe.is_deep:
+            return lookup(self.binding.seed, self.probe)
+        return None
+
+
 class LookupCommandInCommand(Lookup):
 
     adapt(CommandBinding, CommandProbe)
     return lookup(binding, probe)
 
 
+def unwrap(binding, binding_class, is_deep=True):
+    probe = UnwrapProbe(binding_class, is_deep)
+    return lookup(binding, probe)
+
+