Kirill Simonov avatar Kirill Simonov committed ee4d46f

Added `tweak.shell.default` - installs `shell()` as the default command.

Comments (0)

Files changed (3)

         'tweak.sqlalchemy = htsql_tweak.sqlalchemy:TweakSQLAlchemyAddon',
         'tweak.meta = htsql_tweak.meta:TweakMetaAddon',
         'tweak.meta.slave = htsql_tweak.meta.slave:TweakMetaSlaveAddon',
+        'tweak.resource = htsql_tweak.resource:TweakResourceAddon',
         'tweak.shell = htsql_tweak.shell:TweakShellAddon',
-        'tweak.resource = htsql_tweak.resource:TweakResourceAddon',
+        'tweak.shell.default = htsql_tweak.shell.default:TweakShellDefaultAddon',
     ],
 }
 INSTALL_REQUIRES = [

src/htsql_tweak/shell/default/__init__.py

+#
+# Copyright (c) 2006-2011, Prometheus Research, LLC
+# See `LICENSE` for license information, `AUTHORS` for the list of authors.
+#
+
+
+from . import act
+from htsql.addon import Addon, Parameter
+from htsql.validator import BoolVal
+
+
+class TweakShellDefaultAddon(Addon):
+
+    name = 'tweak.shell.default'
+
+    parameters = [
+            Parameter('on_root', BoolVal(), default=True),
+            Parameter('on_default', BoolVal(), default=True),
+            Parameter('on_error', BoolVal(), default=True),
+    ]
+
+

src/htsql_tweak/shell/default/act.py

+#
+# Copyright (c) 2006-2011, Prometheus Research, LLC
+# See `LICENSE` for license information, `AUTHORS` for the list of authors.
+#
+
+
+from htsql.context import context
+from htsql.adapter import adapts
+from htsql.cmd.act import Act, RenderAction, act
+from htsql.cmd.command import UniversalCmd, DefaultCmd
+from ..command import ShellCmd
+from htsql.tr.error import TranslateError
+from htsql.tr.parse import parse
+from htsql.tr.bind import bind
+from htsql.tr.lookup import lookup_command
+import re
+
+
+escape_pattern = r"""%(?:(?P<code>[0-9A-Fa-f]{2})|..)"""
+escape_regexp = re.compile(escape_pattern)
+
+def unquote(query):
+    def replace(match):
+        code = match.group('code')
+        if not code:
+            return match.group()
+        code = int(code, 16)
+        if code < 0x20 or code == 0x25 or code == 0x7F:
+            return match.group()
+        return chr(code)
+    return escape_regexp.sub(replace, query)
+
+
+class ShellRenderUniversal(Act):
+
+    adapts(UniversalCmd, RenderAction)
+
+    def __call__(self):
+        addon = context.app.tweak.shell.default
+        command = None
+        accept = set()
+        if 'HTTP_ACCEPT' in self.action.environ:
+            for name in self.action.environ['HTTP_ACCEPT'].split(','):
+                if ';' in name:
+                    name = name.split(';', 1)[0]
+                name = name.strip()
+                accept.add(name)
+        if 'text/html' not in accept:
+            return super(ShellRenderUniversal, self).__call__()
+        try:
+            syntax = parse(self.command.query)
+            if addon.on_root and syntax.segment.branch is None:
+                command = ShellCmd()
+            else:
+                binding = bind(syntax)
+                command = lookup_command(binding)
+                if command is None:
+                    if (addon.on_default and
+                            syntax.segment.branch is not None):
+                        query = unquote(self.command.query)
+                        command = ShellCmd(query)
+                    else:
+                        command = DefaultCmd(binding)
+            return act(command, self.action)
+        except TranslateError:
+            if not addon.on_error:
+                raise
+            query = unquote(self.command.query)
+            command = ShellCmd(query)
+            return act(command, self.action)
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.