Commits

Kirill Simonov  committed 3273c8d

tweak.gateway: permit full plugin configuration for gateways.

  • Participants
  • Parent commits 4cbc81f

Comments (0)

Files changed (2)

File src/htsql/core/validator.py

         return value
 
 
+class UnionVal(Validator):
+
+    def __init__(self, validators, is_nullable=False):
+        assert isinstance(validators, listof(Validator))
+        assert isinstance(is_nullable, bool)
+        self.validators = validators
+        self.is_nullable = is_nullable
+
+    def __call__(self, value):
+        # `None` is allowed if the `is_nullable` flag is set.
+        if value is None:
+            if self.is_nullable:
+                return None
+            else:
+                raise ValueError("the null value is not permitted")
+
+        messages = []
+        for validator in self.validators:
+            try:
+                return validator(value)
+            except ValueError, exc:
+                messages.append(str(exc))
+        raise ValueError("; ".join(messages))
+
+
 class ExtensionVal(Validator):
 
     pattern = r"""

File src/htsql/tweak/gateway/__init__.py

 
 
 from . import command
+from ...core.util import DB
 from ...core.addon import Addon, Parameter
-from ...core.validator import MapVal, NameVal, DBVal
+from ...core.validator import AnyVal, UnionVal, MapVal, NameVal, StrVal, DBVal
 from .command import BindGateway
 
 
     """
 
     parameters = [
-            Parameter('gateways', MapVal(NameVal(), DBVal()),
+            Parameter('gateways',
+                      MapVal(NameVal(),
+                          UnionVal([
+                              DBVal(),
+                              MapVal(StrVal(), AnyVal())])),
                       default={},
                       value_name="{NAME:DB}",
                       hint="""gateway definitions"""),
         self.functions = {}
         for name in sorted(self.gateways):
             db = self.gateways[name]
-            instance = app.__class__(db)
+            if isinstance(db, DB):
+                instance = app.__class__(db)
+            else:
+                instance = app.__class__(None, db)
             class_name = "Bind%s" % name.title().replace('_', '').encode('utf-8')
             namespace = {
                 '__names__': [(name.encode('utf-8'), 1)],