Commits

Kirill Simonov  committed 9d55307

Fixed `tweak.hello` addon.

  • Participants
  • Parent commits 016293f

Comments (0)

Files changed (7)

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

 from ...core.context import context
 from ...core.adapter import adapt, call
 from ...core.addon import Addon, Parameter
-from ...core.tr.fn.bind import BindCommand
-from ...core.tr.binding import CommandBinding
-from ...core.tr.signature import NullarySig
+from ...core.validator import PIntVal, ChoiceVal
+from ...core.error import Error
 from ...core.cmd.command import Command
+from ...core.cmd.summon import Summon
 from ...core.cmd.act import Act, RenderAction
-from ...core.validator import PIntVal, ChoiceVal
 
 
 class HelloCmd(Command):
     pass
 
 
-class BindHello(BindCommand):
+class SummonHello(Summon):
 
     call('hello')
-    signature = NullarySig
 
-    def expand(self):
-        command = HelloCmd()
-        return CommandBinding(self.state.scope, command, self.syntax)
+    def __call__(self):
+        if self.arguments:
+            raise Error("Expected no arguments")
+        return HelloCmd()
 
 
 class RenderHello(Act):
         repeat = context.app.tweak.hello.repeat
         line = "Hello, " + address.capitalize() + "!\n"
         headers = [('Content-Type', "text/plain; charset=UTF-8")]
-        body = [line * repeat]
+        body = [line]*repeat
         return (status, headers, body)
 
 
 class TweakHelloAddon(Addon):
     
     name = 'tweak.hello'
-    hint = """example plugin and command"""
+    hint = """'Hello, World!' plugin"""
     help = """
-      This is an example plugin and command.  It has two parameters
-      ``address`` which defaults to 'world' and ``repeat`` which
-      defaults to 1.  The plugin registers a command ``/hello()``
-      that prints "Hello, X!" several times.  It can be started 
-      using a command line::
+    This plugin registers command `/hello()`, which displays "Hello, World!".
 
-       htsql-ctl shell htsql_regress -E tweak.hello:address=mom,repeat=3
-
+    The plugin has two parameters: `address` (default: 'world') and
+    `repeat` (default: 1).
     """
 
     parameters = [
-            Parameter('repeat', PIntVal(), default=1),
-            Parameter('address', ChoiceVal(['mom','home','world']), 
-                                 default='world')
+            Parameter('repeat',
+                      PIntVal(), default=1),
+            Parameter('address',
+                      ChoiceVal(['mom', 'home', 'world']),
+                      default='world')
     ]
 
+

File test/input/addon.yaml

   - uri: /sqlite_gw(/nothing)
     expect: 400
 
+# TWEAK.HELLO - 'Hello, World!'
+- title: tweak.hello
+  tests:
+  # Addon description
+  - ctl: [ext, tweak.hello]
+
+  # Load the addon
+  - load: demo
+    extensions:
+      tweak.hello: {}
+
+  # Run the command
+  - uri: /hello()
+
+  # Invalid invocation
+  - uri: /hello('world')
+    expect: 400
+
+  # Plugin parameters
+  - load: demo
+    extensions:
+      tweak.hello:
+        address: home
+        repeat: 3
+  - uri: /hello()
+
 # TWEAK.INET - IPv4 data type
 - title: tweak.inet
   ifdef: pgsql

File test/output/mssql.yaml

                     [school].[campus]
              FROM [ad].[school]
              ORDER BY 1 ASC
+      - id: tweak.hello
+        tests:
+        - ctl: [ext, tweak.hello]
+          stdout: |+
+            TWEAK.HELLO - 'Hello, World!' plugin
+
+            This plugin registers command `/hello()`, which displays "Hello, World!".
+
+            The plugin has two parameters: `address` (default: 'world') and
+            `repeat` (default: 1).
+
+            Parameters:
+              repeat=REPEAT
+              address=ADDRESS
+
+          exit: 0
+        - uri: /hello()
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Hello, World!
+        - uri: /hello('world')
+          status: 400 Bad Request
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Expected no arguments
+            While parsing:
+                /hello('world')
+                 ^^^^^^^^^^^^^^
+        - uri: /hello()
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Hello, Home!
+            Hello, Home!
+            Hello, Home!
       - id: tweak.meta
         tests:
         - ctl: [ext, tweak.meta]

File test/output/mysql.yaml

             While translating:
                 /sqlite_gw(/nothing)
                             ^^^^^^^
+      - id: tweak.hello
+        tests:
+        - ctl: [ext, tweak.hello]
+          stdout: |+
+            TWEAK.HELLO - 'Hello, World!' plugin
+
+            This plugin registers command `/hello()`, which displays "Hello, World!".
+
+            The plugin has two parameters: `address` (default: 'world') and
+            `repeat` (default: 1).
+
+            Parameters:
+              repeat=REPEAT
+              address=ADDRESS
+
+          exit: 0
+        - uri: /hello()
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Hello, World!
+        - uri: /hello('world')
+          status: 400 Bad Request
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Expected no arguments
+            While parsing:
+                /hello('world')
+                 ^^^^^^^^^^^^^^
+        - uri: /hello()
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Hello, Home!
+            Hello, Home!
+            Hello, Home!
       - id: tweak.meta
         tests:
         - ctl: [ext, tweak.meta]

File test/output/oracle.yaml

              ORDER BY "POLLS_CHOICE"."ID" ASC
         - py: remove-module-path
           stdout: ''
+      - id: tweak.hello
+        tests:
+        - ctl: [ext, tweak.hello]
+          stdout: |+
+            TWEAK.HELLO - 'Hello, World!' plugin
+
+            This plugin registers command `/hello()`, which displays "Hello, World!".
+
+            The plugin has two parameters: `address` (default: 'world') and
+            `repeat` (default: 1).
+
+            Parameters:
+              repeat=REPEAT
+              address=ADDRESS
+
+          exit: 0
+        - uri: /hello()
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Hello, World!
+        - uri: /hello('world')
+          status: 400 Bad Request
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Expected no arguments
+            While parsing:
+                /hello('world')
+                 ^^^^^^^^^^^^^^
+        - uri: /hello()
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Hello, Home!
+            Hello, Home!
+            Hello, Home!
       - id: tweak.meta
         tests:
         - ctl: [ext, tweak.meta]

File test/output/pgsql.yaml

              ORDER BY "polls_choice"."id" ASC
         - py: remove-module-path
           stdout: ''
+      - id: tweak.hello
+        tests:
+        - ctl: [ext, tweak.hello]
+          stdout: |+
+            TWEAK.HELLO - 'Hello, World!' plugin
+
+            This plugin registers command `/hello()`, which displays "Hello, World!".
+
+            The plugin has two parameters: `address` (default: 'world') and
+            `repeat` (default: 1).
+
+            Parameters:
+              repeat=REPEAT
+              address=ADDRESS
+
+          exit: 0
+        - uri: /hello()
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Hello, World!
+        - uri: /hello('world')
+          status: 400 Bad Request
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Expected no arguments
+            While parsing:
+                /hello('world')
+                 ^^^^^^^^^^^^^^
+        - uri: /hello()
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Hello, Home!
+            Hello, Home!
+            Hello, Home!
       - id: tweak.inet
         tests:
         - ctl: [ext, tweak.inet]

File test/output/sqlite.yaml

                                    FROM "!source"
                                    WHERE ("!source"."name" = 'volatile')) AS "!source_2"
                                   ON 1
+      - id: tweak.hello
+        tests:
+        - ctl: [ext, tweak.hello]
+          stdout: |+
+            TWEAK.HELLO - 'Hello, World!' plugin
+
+            This plugin registers command `/hello()`, which displays "Hello, World!".
+
+            The plugin has two parameters: `address` (default: 'world') and
+            `repeat` (default: 1).
+
+            Parameters:
+              repeat=REPEAT
+              address=ADDRESS
+
+          exit: 0
+        - uri: /hello()
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Hello, World!
+        - uri: /hello('world')
+          status: 400 Bad Request
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Expected no arguments
+            While parsing:
+                /hello('world')
+                 ^^^^^^^^^^^^^^
+        - uri: /hello()
+          status: 200 OK
+          headers:
+          - [Content-Type, text/plain; charset=UTF-8]
+          body: |
+            Hello, Home!
+            Hello, Home!
+            Hello, Home!
       - id: tweak.meta
         tests:
         - ctl: [ext, tweak.meta]