]> git.cworth.org Git - turbot/commitdiff
Move top-level flask app from turbot/turbot.py to turbot_flask/turbot.py
authorCarl Worth <cworth@cworth.org>
Sun, 11 Oct 2020 20:59:19 +0000 (13:59 -0700)
committerCarl Worth <cworth@cworth.org>
Sun, 11 Oct 2020 21:06:59 +0000 (14:06 -0700)
This is a first baby step toward teasing apart true "library" code in
turbot, (which can be shared by both the Lambda and Flask
implementations), from code that is Flask-specific.

(For symmetry, the lambda directory is renamed turbot_lambda here as well.)

Makefile
lambda/lambda_function.py [deleted file]
turbot.wsgi.in
turbot/turbot.py [deleted file]
turbot_flask/__init__.py [new file with mode: 0644]
turbot_flask/turbot.py [new file with mode: 0644]
turbot_lambda/lambda_function.py [new file with mode: 0644]

index f3c46a18adbb00697b35c6baf01f29802924f3c2..8aaad75091914c2f7dbf074a6a1076d7e8307991 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -77,7 +77,7 @@ deploy-lambda:
        rm -rf .deploy-lambda-source
        git clone . .deploy-lambda-source
        rm -rf .deploy-lambda-source/.git
-       (cd .deploy-lambda-source/lambda; zip ../turbot.zip lambda_function.py)
+       (cd .deploy-lambda-source/turbot_lambda; zip ../turbot.zip lambda_function.py)
        (cd .deploy-lambda-source; \
                aws lambda update-function-code \
                --profile halibut \
diff --git a/lambda/lambda_function.py b/lambda/lambda_function.py
deleted file mode 100644 (file)
index fe545b8..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-import re
-
-def rot_string(str, n=13):
-    """Return a rotated version of a string
-
-    Specifically, this functions returns a version of the input string
-    where each uppercase letter has been advanced 'n' positions in the
-    alphabet (wrapping around). Lowercase letters and any non-alphabetic
-    characters will be unchanged."""
-
-    result = ''
-    for letter in str:
-        if letter.isupper():
-            result += chr(ord("A") + (ord(letter) - ord("A") + n) % 26)
-        else:
-            result += letter
-    return result
-
-def rot(args):
-    """Implements logic for /rot slash command in Slack, returning a string
-
-    This implements the /rot command of our Slack bot. The format of this
-    command is as follows:
-
-        /rot [count|*] String to be rotated
-
-    The optional count indicates an amount to rotate each character in the
-    string. If the count is '*' or is not present, then the string will
-    be rotated through all possible 25 values.
-
-    The result of the rotation is returned as a string (with Slack
-    formatting)."""
-
-    match = re.match(r'^([0-9]+|\*) (.*)$', args)
-    if (match):
-        try:
-            count = int(match.group(1))
-        except ValueError:
-            count = None
-        text = match.group(2)
-    else:
-        count = None
-        text = args
-
-    text = text.upper()
-
-    reply = "```/rot {} {}\n".format(count if count else '*', text)
-
-    if count:
-        reply += rot_string(text, count)
-    else:
-        reply += "\n".join(["{:02d} ".format(count) + rot_string(text, count)
-                            for count in range(1, 26)])
-
-    reply += "```"
-
-    return reply
-
-def lambda_handler(event, context):
-    """Top-level entry point for our lambda function.
-
-    Currently only calls into the rot() function but may become more
-    sophisticated later on."""
-
-    result = rot(event['args'])
-
-    return {
-        'statusCode': 200,
-        'body': result
-    }
index 5a7c37b375db20d1a40e29615b78ab99f58b53e5..71349343c71778c30ebc54a6ab9042a8a65c2c74 100644 (file)
@@ -7,4 +7,4 @@ load_dotenv('${DEPLOY_DIR}/.slack-creds.env')
 
 os.environ['TURBOT_DEPLOY_DIR']='${DEPLOY_DIR}'
 
-from turbot.turbot import app as application
+from turbot_flask.turbot import app as application
diff --git a/turbot/turbot.py b/turbot/turbot.py
deleted file mode 100755 (executable)
index df7d51e..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-#!/usr/bin/env python3
-
-from flask import Flask
-from slackeventsapi import SlackEventAdapter
-import os
-import threading
-from turbot.rot import rot_route
-from turbot.slack import slack_send_message
-from turbot.sheets import sheets_create
-
-app = Flask(__name__)
-app.register_blueprint(rot_route)
-
-slack_signing_secret = os.environ['SLACK_SIGNING_SECRET']
-slack_events = SlackEventAdapter(slack_signing_secret, "/slack/events", app)
-
-@slack_events.on("channel_created")
-def handle_channel_created(event_data):
-    def later(channel):
-        sheet_url = sheets_create(channel["name"])
-        slack_send_message(channel["id"],
-                           "Auto-created a sheet for this channel: {}"
-                           .format(sheet_url))
-
-    event = event_data["event"]
-    channel = event["channel"]
-    thread = threading.Thread(target=later, kwargs={'channel': channel})
-    thread.start()
-    return
-
-@slack_events.on("error")
-def handle_error(error):
-    app.logger.error("Error from Slack: " + str(error))
diff --git a/turbot_flask/__init__.py b/turbot_flask/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/turbot_flask/turbot.py b/turbot_flask/turbot.py
new file mode 100644 (file)
index 0000000..df7d51e
--- /dev/null
@@ -0,0 +1,33 @@
+#!/usr/bin/env python3
+
+from flask import Flask
+from slackeventsapi import SlackEventAdapter
+import os
+import threading
+from turbot.rot import rot_route
+from turbot.slack import slack_send_message
+from turbot.sheets import sheets_create
+
+app = Flask(__name__)
+app.register_blueprint(rot_route)
+
+slack_signing_secret = os.environ['SLACK_SIGNING_SECRET']
+slack_events = SlackEventAdapter(slack_signing_secret, "/slack/events", app)
+
+@slack_events.on("channel_created")
+def handle_channel_created(event_data):
+    def later(channel):
+        sheet_url = sheets_create(channel["name"])
+        slack_send_message(channel["id"],
+                           "Auto-created a sheet for this channel: {}"
+                           .format(sheet_url))
+
+    event = event_data["event"]
+    channel = event["channel"]
+    thread = threading.Thread(target=later, kwargs={'channel': channel})
+    thread.start()
+    return
+
+@slack_events.on("error")
+def handle_error(error):
+    app.logger.error("Error from Slack: " + str(error))
diff --git a/turbot_lambda/lambda_function.py b/turbot_lambda/lambda_function.py
new file mode 100644 (file)
index 0000000..fe545b8
--- /dev/null
@@ -0,0 +1,70 @@
+import re
+
+def rot_string(str, n=13):
+    """Return a rotated version of a string
+
+    Specifically, this functions returns a version of the input string
+    where each uppercase letter has been advanced 'n' positions in the
+    alphabet (wrapping around). Lowercase letters and any non-alphabetic
+    characters will be unchanged."""
+
+    result = ''
+    for letter in str:
+        if letter.isupper():
+            result += chr(ord("A") + (ord(letter) - ord("A") + n) % 26)
+        else:
+            result += letter
+    return result
+
+def rot(args):
+    """Implements logic for /rot slash command in Slack, returning a string
+
+    This implements the /rot command of our Slack bot. The format of this
+    command is as follows:
+
+        /rot [count|*] String to be rotated
+
+    The optional count indicates an amount to rotate each character in the
+    string. If the count is '*' or is not present, then the string will
+    be rotated through all possible 25 values.
+
+    The result of the rotation is returned as a string (with Slack
+    formatting)."""
+
+    match = re.match(r'^([0-9]+|\*) (.*)$', args)
+    if (match):
+        try:
+            count = int(match.group(1))
+        except ValueError:
+            count = None
+        text = match.group(2)
+    else:
+        count = None
+        text = args
+
+    text = text.upper()
+
+    reply = "```/rot {} {}\n".format(count if count else '*', text)
+
+    if count:
+        reply += rot_string(text, count)
+    else:
+        reply += "\n".join(["{:02d} ".format(count) + rot_string(text, count)
+                            for count in range(1, 26)])
+
+    reply += "```"
+
+    return reply
+
+def lambda_handler(event, context):
+    """Top-level entry point for our lambda function.
+
+    Currently only calls into the rot() function but may become more
+    sophisticated later on."""
+
+    result = rot(event['args'])
+
+    return {
+        'statusCode': 200,
+        'body': result
+    }